Skip to content

Commit a3c94cf

Browse files
committed
[DCV-3760] Track all profiles.yml paths and re-resolve profiles_dir dynamically
1 parent f0657c3 commit a3c94cf

2 files changed

Lines changed: 33 additions & 7 deletions

File tree

src/dbt_core_interface/project.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,9 @@ def args(self, value: DbtConfiguration | dict[str, t.Any]) -> None: # pyright:
375375
"""Set the args for the DbtProject instance and update runtime config."""
376376
if isinstance(value, dict):
377377
value = dc_replace(self._args, **value)
378+
self._args = value
378379
set_from_args(value, None) # pyright: ignore[reportArgumentType]
379380
self.parse_project(write_manifest=True, reparse_configuration=True)
380-
self._args = value
381381

382382
def set_args(self, **kwargs: t.Any) -> None:
383383
"""Set the args for the DbtProject instance."""
@@ -519,10 +519,19 @@ def parse_project(
519519
) -> None:
520520
"""Parse the dbt project and load manifest."""
521521
if reparse_configuration:
522-
self._args = dc_replace(
523-
self._args,
524-
profiles_dir=_get_profiles_dir(self.project_root),
525-
)
522+
current = Path(self._args.profiles_dir).resolve()
523+
standard_dirs = {
524+
self.project_root.resolve(),
525+
(Path.home() / ".dbt").resolve(),
526+
}
527+
env_dir = os.environ.get("DBT_PROFILES_DIR")
528+
if env_dir:
529+
standard_dirs.add(Path(env_dir).expanduser().resolve())
530+
if current in standard_dirs:
531+
self._args = dc_replace(
532+
self._args,
533+
profiles_dir=_get_profiles_dir(self.project_root),
534+
)
526535
self.runtime_config = RuntimeConfig.from_args(self._args)
527536
self.__manifest_loader = ManifestLoader(
528537
self.runtime_config,

src/dbt_core_interface/server.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
DbtConfiguration,
3939
DbtProject,
4040
ExecutionResult,
41+
_get_profiles_dir,
4142
)
4243
from dbt_core_interface.watcher import DbtProjectWatcher
4344

@@ -136,10 +137,26 @@ def _load_saved_state(runners: DbtProjectContainer) -> None:
136137
project_name = t.cast(str, entry.get("project"))
137138
if not project_name:
138139
continue
140+
project_dir = entry.get("project_dir")
141+
saved_profiles_dir = entry.get("profiles_dir")
142+
if saved_profiles_dir:
143+
saved_path = Path(saved_profiles_dir).resolve()
144+
standard_dirs: set[Path] = {(Path.home() / ".dbt").resolve()}
145+
if project_dir:
146+
standard_dirs.add(Path(project_dir).resolve())
147+
env_dir = os.environ.get("DBT_PROFILES_DIR")
148+
if env_dir:
149+
standard_dirs.add(Path(env_dir).expanduser().resolve())
150+
if saved_path in standard_dirs:
151+
profiles_dir = _get_profiles_dir(project_dir)
152+
else:
153+
profiles_dir = saved_profiles_dir
154+
else:
155+
profiles_dir = _get_profiles_dir(project_dir)
139156
kwargs: dict[str, t.Any] = {
140157
"target": entry.get("target"),
141-
"profiles_dir": entry.get("profiles_dir"),
142-
"project_dir": entry.get("project_dir"),
158+
"project_dir": project_dir,
159+
"profiles_dir": profiles_dir,
143160
"threads": entry.get("threads", 1),
144161
"vars": entry.get("vars", {}),
145162
}

0 commit comments

Comments
 (0)