Skip to content

fix!: pass macros to the last audit in load_multiple_audits - #6120

Open
markussagens wants to merge 1 commit into
SQLMesh:mainfrom
markussagens:fix/standalone-audit-last-in-file-macros
Open

markussagens wants to merge 1 commit into
SQLMesh:mainfrom
markussagens:fix/standalone-audit-last-in-file-macros

Conversation

@markussagens

Copy link
Copy Markdown

Description

Fixes #6119.

load_multiple_audits passed module_path, macros and jinja_macros to load_audit inside its loop, but not in the final yield load_audit(...). So the last AUDIT in a file (the only one, for a one-audit file) was loaded with an empty python_env and an empty Jinja macro registry. Rendering a standalone audit that used a project macro then failed with MacroEvalError: Macro '<name>' does not exist.

The final call now passes the same three arguments as the call inside the loop. The other arguments already matched.

Fingerprint impact (!): an affected standalone audit now stores the macros it references, so its metadata hash and fingerprint change. After upgrading, sqlmesh plan will list it as a metadata-only change. These audits failed to render before, so nothing that previously worked changes behavior.

Test Plan

Three regression tests in tests/core/test_audit.py. All three fail without the fix and pass with it:

  • test_load_multiple_single_standalone_with_macros: one-audit file using a project Python macro.
  • test_load_multiple_last_standalone_with_macros: two-audit file where only the last audit uses the macro.
  • test_load_multiple_last_standalone_with_jinja_macros: last/only audit using a project Jinja macro.

pytest tests/core/test_audit.py passes (46 tests). Also ran the issue's reproduction project end to end: Context(...).standalone_audits["only_allowed"].render_audit_query() now renders.

Checklist

  • I have run make style and fixed any issues (ran ruff check, ruff format and mypy on the changed files)
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test) (only tests/core/test_audit.py was run locally)
  • My commits are signed off (git commit -s) per the DCO

🤖 Generated with Claude Code

The final load_audit call in load_multiple_audits did not pass
module_path, macros or jinja_macros, so the last (or only) AUDIT in a
file was loaded with an empty python_env and no project jinja macros.
Rendering a standalone audit that used a project macro then failed with
"MacroEvalError: Macro '<name>' does not exist."

Affected standalone audits now include the macros they reference, which
changes their metadata hash and fingerprint.

Fixes SQLMesh#6119

Signed-off-by: Markus Sagen <markus@studio555.io>
@mday-io mday-io self-assigned this Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Project macros missing in the last standalone audit of a file

2 participants