@@ -47,26 +47,77 @@ def get_automation_condition(
4747 )
4848else :
4949 current_file = Path (__file__ ).resolve ()
50+ # Try to find macro_agents package root (where dbt_project is copied during deployment)
51+ # Go up from: macro_agents/defs/transformation/dbt.py -> macro_agents/defs/transformation -> macro_agents/defs -> macro_agents
52+ macro_agents_root = current_file .parent .parent .parent
5053 repo_root = current_file .parent .parent .parent .parent .parent
54+ cwd = Path .cwd ()
55+
56+ # In Dagster Cloud, the working_directory is set to ./macro_agents
57+ # The dbt_project should be copied there during deployment
58+ # The working directory at runtime is typically working_directory/root
59+ # So dbt_project should be at working_directory/root/dbt_project (i.e., cwd/dbt_project)
5160 possible_dbt_project_paths = [
52- Path .cwd () / "dbt_project" ,
61+ # First priority: current working directory (Dagster Cloud runtime: working_directory/root)
62+ # This is where the working_directory contents are extracted
63+ cwd / "dbt_project" ,
64+ # Second: if we're in a "root" subdirectory, check parent
65+ cwd .parent / "dbt_project" if cwd .name == "root" else None ,
66+ # Third: check if dbt_project is in the parent of root (working_directory level)
67+ cwd .parent .parent / "dbt_project" if cwd .parent .name == "root" else None ,
68+ # Fourth: check relative to macro_agents package location (if bundled in wheel)
69+ macro_agents_root / "dbt_project" ,
70+ # Fifth: check if dbt_project is alongside the package
71+ macro_agents_root .parent / "dbt_project" ,
72+ # Sixth: check in macro_agents subdirectory (if working directory is repo root)
73+ cwd / "macro_agents" / "dbt_project" ,
74+ # Seventh: check in repo root (fallback)
5375 repo_root / "dbt_project" ,
54- Path .cwd ().parent / "dbt_project" ,
76+ # Eighth: check parent of working directory
77+ cwd .parent / "dbt_project" ,
78+ ]
79+
80+ # Filter out None values
81+ possible_dbt_project_paths = [
82+ p for p in possible_dbt_project_paths if p is not None
5583 ]
5684
5785 dbt_project_dir = None
5886 for path in possible_dbt_project_paths :
59- abs_path = path .resolve ()
60- if abs_path .exists () and abs_path .is_dir ():
61- dbt_project_dir = abs_path
62- break
87+ try :
88+ abs_path = path .resolve ()
89+ if abs_path .exists () and abs_path .is_dir ():
90+ # Verify it's actually a dbt project by checking for dbt_project.yml
91+ if (abs_path / "dbt_project.yml" ).exists () or (
92+ abs_path / "dbt_project.yaml"
93+ ).exists ():
94+ dbt_project_dir = abs_path
95+ break
96+ except (OSError , RuntimeError ):
97+ # Skip paths that can't be resolved (e.g., broken symlinks)
98+ continue
6399
64100 if dbt_project_dir is None :
65- tried_paths = [str (p .resolve ()) for p in possible_dbt_project_paths ]
101+ tried_paths = []
102+ for p in possible_dbt_project_paths :
103+ try :
104+ tried_paths .append (str (p .resolve ()))
105+ except (OSError , RuntimeError ):
106+ tried_paths .append (str (p ))
107+
108+ # Also list what actually exists in the working directory for debugging
109+ cwd_contents = []
110+ try :
111+ cwd_contents = [str (p ) for p in cwd .iterdir ()] if cwd .exists () else []
112+ except (OSError , PermissionError ):
113+ pass
114+
66115 raise FileNotFoundError (
67116 f"Could not find dbt_project directory.\n "
68117 f"Current working directory: { Path .cwd ().resolve ()} \n "
118+ f"Working directory contents: { cwd_contents } \n "
69119 f"Module file location: { current_file } \n "
120+ f"Macro agents root (calculated): { macro_agents_root } \n "
70121 f"Repo root (calculated): { repo_root } \n "
71122 f"Tried paths: { tried_paths } \n "
72123 f"Please ensure dbt_project directory exists relative to the repository root, "
0 commit comments