Skip to content

fix(loader): avoid false duplicate macro errors after a failed load and name broken macro files - #6305

Open
connordouglashanna wants to merge 3 commits into
SQLMesh:mainfrom
connordouglashanna:fix/loader-restore-macros-on-error
Open

connordouglashanna wants to merge 3 commits into
SQLMesh:mainfrom
connordouglashanna:fix/loader-restore-macros-on-error

Conversation

@connordouglashanna

@connordouglashanna connordouglashanna commented Dec 3, 2026 •

Copy link
Copy Markdown

Description

This PR contains two small fixes to how the loader handles macros/. Both were issues I found while working on #6297.

  1. A failed macro import no longer breaks the next load: If a macros/*.py file raised during import, macros from files imported before it stayed in the global registry, so the next project loaded in the same process (a notebook, the LSP, the web UI, a test session) could fail with a false Duplicate name error. SqlMeshLoader._load_scripts now restores the registry in a finally.
  2. Tokenizer errors in macros/*.sql name the file: They used to surface as a bare sqlglot TokenError. The loader now raises a ConfigError with the file as its location, e.g. Failed to load macro file macros/finance.sql: <error>. Check the file for syntax errors, such as an unterminated string or comment. MacroExtractor.extract() remains unchanged.

Follow-up Issues

_load_signals has the same pattern, so a broken signals/*.py can leak signals the same way. I left it out to keep this PR focused, but I'd be happy to follow up separately.

Test Plan

The first commit adds two tests to tests/core/test_loader.py that fail on main, and the second commit fixes them.

  • test_failed_macro_load_restores_registry (registry_isolation): after a load fails in the second macro file, the first file's macro is gone, and a second project defining the same macro name with a different body loads.
  • test_untokenizable_macro_file_raises_config_error: the ConfigError names the file and sets location, and the registry is unchanged.

tests/core/test_loader.py passes on Python 3.12 and 3.9. All four make style hooks pass. Every stage of make fast-test passes except 6 mssql/fabric pyodbc tests, which also fail on main locally because unixODBC isn't installed.

Checklist

  • I have run make style and fixed any issues
  • I have added tests for my changes (if applicable)
  • All existing tests pass (make fast-test)
  • My commits are signed off (git commit -s) per the DCO

…file token errors

Signed-off-by: Connor Hanna <connordouglashanna@gmail.com>
… macro syntax errors

Signed-off-by: Connor Hanna <connordouglashanna@gmail.com>
@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Fixes two macros/ loader issues in SqlMeshLoader._load_scripts.

  • Restores the macro registry in a finally block. A failed macros/*.py import no longer leaks macros into later loads in the same process.
  • Wraps TokenError from macros/*.sql files in a ConfigError. The error names the file and sets its location.

Adds tests for both cases in tests/core/test_loader.py. MacroExtractor.extract() is unchanged.

Scope: +40/-28 lines. Changed file under sqlmesh/core: sqlmesh/core/loader.py.

The loader now restores the standard macro registry after script-loading failures and reports SQL macro tokenization errors as path-specific configuration errors. Tests cover registry restoration, error details, and direct extraction behavior.

Changes: Macro loading

Layer / File(s) Summary
Macro loading and error handling
sqlmesh/core/loader.py, tests/core/test_loader.py
The loader restores the standard macro registry even when script loading fails. It converts SQL macro TokenError exceptions to path-specific ConfigError exceptions. Tests check registry restoration, error location, and direct extraction behavior.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: UX

Suggested reviewers: tobymao, georgesittas

Merge Risk: Minimal · up to 29484

Failed macro loads no longer leak registry entries that cause false duplicate-name errors. Untokenizable SQL macro files now report their file path. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two changes in the PR: restoring the macro registry after a failed load, which prevents false duplicate-name errors, and wrapping macro tokenizer errors in a ConfigError that names…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the repository template: it has a Description section that explains both fixes, a Test Plan section that names the two new tests, the test results, and the style and test runs,…
Full details: Title check

Explanation

The title names the two changes in the PR: restoring the macro registry after a failed load, which prevents false duplicate-name errors, and wrapping macro tokenizer errors in a ConfigError that names the file. The title is accurate and specific. It uses the conventional fix(loader) prefix. At 95 characters it is long, but it is still a single descriptive sentence.

Full details: Description check

Explanation

The description follows the repository template: it has a Description section that explains both fixes, a Test Plan section that names the two new tests, the test results, and the style and test runs, and a Checklist with all four items marked. It also adds a Follow-up Issues section about the analogous _load_signals gap. The description is specific and complete. The only gap is that the checklist marks 'All existing tests pass (make fast-test)' as done, while the test plan says six mssql/fabric pyodbc tests fail locally. The author explains this, so it is a minor point, not a missing section.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 9fa0b6e and 2948472.

Files selected for processing: 2

  • sqlmesh/core/loader.py
  • tests/core/test_loader.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@connordouglashanna connordouglashanna changed the title Fix/loader restore macros on error fix(loader): avoid false duplicate macro errors after a failed load and name broken macro files Dec 3, 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.

1 participant