diff --git a/sqlmesh/utils/jinja.py b/sqlmesh/utils/jinja.py index 9764e625a4..74d498be38 100644 --- a/sqlmesh/utils/jinja.py +++ b/sqlmesh/utils/jinja.py @@ -330,13 +330,16 @@ def build_environment(self, **kwargs: t.Any) -> Environment: if macro.is_top_level and macro_name not in root_macros: root_macros[macro_name] = macro_wrapper + top_level_packages = self.top_level_packages.copy() + if self.root_package_name is not None: package_macros[self.root_package_name].update(root_macros) + top_level_packages.append(self.root_package_name) env = environment() builtin_globals = self._create_builtin_globals(kwargs) - for top_level_package_name in self.top_level_packages: + for top_level_package_name in top_level_packages: # Make sure that the top-level package doesn't fully override the same builtin package. package_macros[top_level_package_name] = AttributeDict( { diff --git a/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/dbt_project.yml b/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/dbt_project.yml new file mode 100644 index 0000000000..f0386b4e57 --- /dev/null +++ b/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/dbt_project.yml @@ -0,0 +1,17 @@ +name: 'my_macros' +version: '1.0.0' +config-version: 2 + +profile: 'my_macros' + +model-paths: ["models"] +analysis-paths: ["analyses"] +test-paths: ["tests"] +seed-paths: ["seeds"] +macro-paths: ["macros"] +snapshot-paths: ["snapshots"] + +target-path: "target" +clean-targets: + - "target" + - "dbt_packages" diff --git a/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/macros/log_value_alt.sql b/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/macros/log_value_alt.sql new file mode 100644 index 0000000000..a88f316d3e --- /dev/null +++ b/tests/fixtures/dbt/sushi_test/dbt_packages/my_macros/macros/log_value_alt.sql @@ -0,0 +1,3 @@ +{% macro log_value_alt(v) %} + {{ log("Entered value is: " ~ v) }} +{% endmacro %} diff --git a/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/dbt_project.yml b/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/dbt_project.yml new file mode 100644 index 0000000000..9c4797ebe0 --- /dev/null +++ b/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/dbt_project.yml @@ -0,0 +1,17 @@ +name: 'my_project' +version: '1.0.0' +config-version: 2 + +profile: 'my_project' + +model-paths: ["models"] +analysis-paths: ["analyses"] +test-paths: ["tests"] +seed-paths: ["seeds"] +macro-paths: ["macros"] +snapshot-paths: ["snapshots"] + +target-path: "target" +clean-targets: + - "target" + - "dbt_packages" diff --git a/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/models/dummy_model.sql b/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/models/dummy_model.sql new file mode 100644 index 0000000000..ff815fe55d --- /dev/null +++ b/tests/fixtures/dbt/sushi_test/dbt_packages/my_package/models/dummy_model.sql @@ -0,0 +1,4 @@ +{{ log_value_alt(1) }} + +SELECT + 1 AS c diff --git a/tests/fixtures/dbt/sushi_test/dbt_project.yml b/tests/fixtures/dbt/sushi_test/dbt_project.yml index ea0041d107..2a25389e43 100644 --- a/tests/fixtures/dbt/sushi_test/dbt_project.yml +++ b/tests/fixtures/dbt/sushi_test/dbt_project.yml @@ -8,7 +8,10 @@ model-paths: ["models"] analysis-paths: ["analyses"] test-paths: ["tests"] seed-paths: ["seeds"] -macro-paths: ["macros"] +macro-paths: [ + "macros", + "dbt_packages/my_macros/macros", +] snapshot-paths: ["snapshots"] target-path: "target" # directory which will store compiled SQL files