Skip to content

Commit be40445

Browse files
authored
Fix: ensure root package is treated top-level in dbt (#5290)
1 parent 110d7f2 commit be40445

File tree

6 files changed

+49
-2
lines changed

6 files changed

+49
-2
lines changed

sqlmesh/utils/jinja.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,13 +330,16 @@ def build_environment(self, **kwargs: t.Any) -> Environment:
330330
if macro.is_top_level and macro_name not in root_macros:
331331
root_macros[macro_name] = macro_wrapper
332332

333+
top_level_packages = self.top_level_packages.copy()
334+
333335
if self.root_package_name is not None:
334336
package_macros[self.root_package_name].update(root_macros)
337+
top_level_packages.append(self.root_package_name)
335338

336339
env = environment()
337340

338341
builtin_globals = self._create_builtin_globals(kwargs)
339-
for top_level_package_name in self.top_level_packages:
342+
for top_level_package_name in top_level_packages:
340343
# Make sure that the top-level package doesn't fully override the same builtin package.
341344
package_macros[top_level_package_name] = AttributeDict(
342345
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: 'my_macros'
2+
version: '1.0.0'
3+
config-version: 2
4+
5+
profile: 'my_macros'
6+
7+
model-paths: ["models"]
8+
analysis-paths: ["analyses"]
9+
test-paths: ["tests"]
10+
seed-paths: ["seeds"]
11+
macro-paths: ["macros"]
12+
snapshot-paths: ["snapshots"]
13+
14+
target-path: "target"
15+
clean-targets:
16+
- "target"
17+
- "dbt_packages"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{% macro log_value_alt(v) %}
2+
{{ log("Entered value is: " ~ v) }}
3+
{% endmacro %}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: 'my_project'
2+
version: '1.0.0'
3+
config-version: 2
4+
5+
profile: 'my_project'
6+
7+
model-paths: ["models"]
8+
analysis-paths: ["analyses"]
9+
test-paths: ["tests"]
10+
seed-paths: ["seeds"]
11+
macro-paths: ["macros"]
12+
snapshot-paths: ["snapshots"]
13+
14+
target-path: "target"
15+
clean-targets:
16+
- "target"
17+
- "dbt_packages"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{{ log_value_alt(1) }}
2+
3+
SELECT
4+
1 AS c

tests/fixtures/dbt/sushi_test/dbt_project.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ model-paths: ["models"]
88
analysis-paths: ["analyses"]
99
test-paths: ["tests"]
1010
seed-paths: ["seeds"]
11-
macro-paths: ["macros"]
11+
macro-paths: [
12+
"macros",
13+
"dbt_packages/my_macros/macros",
14+
]
1215
snapshot-paths: ["snapshots"]
1316

1417
target-path: "target" # directory which will store compiled SQL files

0 commit comments

Comments
 (0)