From 8887c0ca0b356a1758b76c2d879725f13cae9e78 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 23 May 2023 19:30:51 -0700 Subject: [PATCH] bugfix: Deps hangs when using relative paths via `--project-dir` (#7628) (#7643) (cherry picked from commit dcb5acdf2923ded7f441fc2edc31d17575b92f41) Co-authored-by: Ian Knox <81931810+iknox-fa@users.noreply.github.com> Co-authored-by: leahwicz <60146280+leahwicz@users.noreply.github.com> --- .../unreleased/Fixes-20230515-123654.yaml | 6 ++++ core/dbt/clients/system.py | 2 +- core/dbt/task/deps.py | 8 ++++- .../dependencies/test_local_dependency.py | 30 +++++++------------ 4 files changed, 24 insertions(+), 22 deletions(-) create mode 100644 .changes/unreleased/Fixes-20230515-123654.yaml diff --git a/.changes/unreleased/Fixes-20230515-123654.yaml b/.changes/unreleased/Fixes-20230515-123654.yaml new file mode 100644 index 00000000000..001bf7ac3b9 --- /dev/null +++ b/.changes/unreleased/Fixes-20230515-123654.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: 'Fix: Relative project paths weren''t working with deps' +time: 2023-05-15T12:36:54.807413-05:00 +custom: + Author: iknox-fa + Issue: "7491" diff --git a/core/dbt/clients/system.py b/core/dbt/clients/system.py index 24e1d59fd25..66c59354b4f 100644 --- a/core/dbt/clients/system.py +++ b/core/dbt/clients/system.py @@ -211,7 +211,7 @@ def _windows_rmdir_readonly(func: Callable[[str], Any], path: str, exc: Tuple[An def resolve_path_from_base(path_to_resolve: str, base_path: str) -> str: """ - If path-to_resolve is a relative path, create an absolute path + If path_to_resolve is a relative path, create an absolute path with base_path as the base. If path_to_resolve is an absolute path or a user path (~), just diff --git a/core/dbt/task/deps.py b/core/dbt/task/deps.py index 3f194db01d6..849c17b5779 100644 --- a/core/dbt/task/deps.py +++ b/core/dbt/task/deps.py @@ -1,5 +1,5 @@ from typing import Any, Optional - +from pathlib import Path import dbt.utils import dbt.deprecations import dbt.exceptions @@ -29,6 +29,12 @@ class DepsTask(BaseTask): def __init__(self, args: Any, project: Project): + # N.B. This is a temporary fix for a bug when using relative paths via + # --project-dir with deps. A larger overhaul of our path handling methods + # is needed to fix this the "right" way. + # See GH-7615 + project.project_root = str(Path(project.project_root).resolve()) + move_to_nearest_project_dir(project.project_root) super().__init__(args=args, config=None, project=project) self.cli_vars = args.vars diff --git a/tests/functional/dependencies/test_local_dependency.py b/tests/functional/dependencies/test_local_dependency.py index 559a1b25586..92bb4b31dfd 100644 --- a/tests/functional/dependencies/test_local_dependency.py +++ b/tests/functional/dependencies/test_local_dependency.py @@ -13,10 +13,7 @@ import dbt.config import dbt.exceptions -from dbt.tests.util import ( - check_relations_equal, - run_dbt, -) +from dbt.tests.util import check_relations_equal, run_dbt, run_dbt_and_capture models__dep_source = """ {# If our dependency source didn't exist, this would be an errror #} @@ -155,23 +152,16 @@ def test_local_dependency(self, project): [f"{project.test_schema}.dep_source_model", f"{project.test_schema}.seed"], ) - def test_no_dependency_paths(self, project): - run_dbt(["deps"]) - run_dbt(["seed"]) - - # prove dependency does not exist as model in project - dep_path = os.path.join("models_local", "model_to_import.sql") - results = run_dbt( - ["run", "--models", f"+{dep_path}"], - ) - assert len(results) == 0 - # prove model can run when importing that dependency - local_path = Path("models") / "my_model.sql" - results = run_dbt( - ["run", "--models", f"+{local_path}"], - ) - assert len(results) == 2 +class TestSimpleDependencyRelativePath(BaseDependencyTest): + def test_local_dependency_relative_path(self, project): + last_dir = Path(project.project_root).name + os.chdir("../") + _, stdout = run_dbt_and_capture(["deps", "--project-dir", last_dir]) + assert ( + "Installed from " in stdout + ), "Test output didn't contain expected string" + os.chdir(project.project_root) class TestMissingDependency(object):