Skip to content

Commit 9dbb7b8

Browse files
authored
Fix: ensure paths exist before treating them as relative (metaprogramming) (#4539)
1 parent bf3354a commit 9dbb7b8

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

sqlmesh/utils/metaprogramming.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _is_relative_to(path: t.Optional[Path | str], other: t.Optional[Path | str])
3737
if isinstance(other, str):
3838
other = Path(other)
3939

40-
if "site-packages" in str(path):
40+
if "site-packages" in str(path) or not path.exists() or not other.exists():
4141
return False
4242

4343
try:

tests/core/test_model.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10129,3 +10129,24 @@ def check_self_schema(evaluator):
1012910129

1013010130
context = Context(paths=tmp_path, config=config)
1013110131
context.plan(no_prompts=True, auto_apply=True)
10132+
10133+
10134+
def test_model_relies_on_os_getenv(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
10135+
init_example_project(tmp_path, dialect="duckdb", template=ProjectTemplate.EMPTY)
10136+
10137+
(tmp_path / "macros" / "getenv_macro.py").write_text(
10138+
"""
10139+
from os import getenv
10140+
from sqlmesh import macro
10141+
10142+
@macro()
10143+
def getenv_macro(evaluator):
10144+
getenv("foo", None)
10145+
return 1"""
10146+
)
10147+
(tmp_path / "models" / "model.sql").write_text(
10148+
"MODEL (name test); SELECT @getenv_macro() AS foo"
10149+
)
10150+
10151+
monkeypatch.chdir(tmp_path)
10152+
ctx = Context(paths=tmp_path)

0 commit comments

Comments
 (0)