Skip to content

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

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

markussagens wants to merge 2 commits 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

@mday-io mday-io left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clear write-up and repro, this looks good.

  • The fix is right: the final load_audit(...) call now gets the same module_path / macros / jinja_macros as the call inside the loop, so the last (or only) standalone audit in a file gets project macros like the others.
  • I checked out the branch and ran the new tests against the old definition.py: all three fail there and pass with the fix. I also confirmed end to end that a one-audit file using a project Python macro now renders.
  • On the !: agreed. python_env feeds into the standalone audit's metadata_hash, so affected audits show up as a metadata-only change on the next plan. There's no backfill and no migration, and only audits that couldn't render before are affected. This matches what we did in #5758.

Approving.

@mday-io

mday-io commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

for context of release notes: Standalone audits that are the last (or only) AUDIT in their file can now use project Python and Jinja macros. Affected audits show up as a metadata-only change on the next sqlmesh plan (#6120).

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