Skip to content

Feat(dbt): Add dbt debug macro#5160

Merged
themisvaltinos merged 1 commit intomainfrom
themis/debugdbt
Aug 15, 2025
Merged

Feat(dbt): Add dbt debug macro#5160
themisvaltinos merged 1 commit intomainfrom
themis/debugdbt

Conversation

@themisvaltinos
Copy link
Contributor

This adds support for dbt's debug macro in a dbt project which opens an iPython debugger in the context of a macro

{% macro my_macro() %}
  {% set something_complex = my_complicated_macro() %}
  {{ debug() }}
{% endmacro %}

This is enabled both by using the dbt DBT_MACRO_DEBUGGING environment variable or the sqlmesh native: SQLMESH_DEBUG or running with --debug flag.

@themisvaltinos themisvaltinos requested a review from a team August 14, 2025 20:08

# Add debug function conditionally both with dbt or sqlmesh equivalent flag
if os.environ.get("DBT_MACRO_DEBUGGING") or debug_mode_enabled():
BUILTIN_GLOBALS["debug"] = debug
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does dbt do with a debug() call where DBT_MACRO_DEBUGGING is not set?

Does it just no-op, or does it throw an error? Because if it no-op's then wouldnt we need to always include it in the builtin globals and move the "is enabled?" check to within the implementation of debug()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes the latter, they have it under a conditional as well so that it throws an error (maybe since they warn "Do not deploy code to production that uses the debug macro" to ensure this is only for debugging. This is why I wanted to exactly mirror their approach to get the same result.

so running dbt when the flag is not set you get an error it is undefined:

06:44:48  Encountered an error:
Compilation Error in operation jaffle_shop-on-run-end-0 (./dbt_project.yml)
  'debug' is undefined

and for sqlmesh you'd get similarly the undefined error:

Error: An error occurred during rendering of the 'after_all' statements:
Could not render or parse jinja at 'None'.
'debug' is undefined

@themisvaltinos themisvaltinos merged commit 622a36f into main Aug 15, 2025
27 of 28 checks passed
@themisvaltinos themisvaltinos deleted the themis/debugdbt branch August 15, 2025 08:27
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.

2 participants