diff --git a/.changes/unreleased/Fixes-20230608-135952.yaml b/.changes/unreleased/Fixes-20230608-135952.yaml new file mode 100644 index 00000000000..6c84e05403b --- /dev/null +++ b/.changes/unreleased/Fixes-20230608-135952.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: Fix path selector when using project-dir +time: 2023-06-08T13:59:52.95775-04:00 +custom: + Author: gshank + Issue: "7819" diff --git a/core/dbt/graph/selector_methods.py b/core/dbt/graph/selector_methods.py index e3139355d7a..fad78c3b238 100644 --- a/core/dbt/graph/selector_methods.py +++ b/core/dbt/graph/selector_methods.py @@ -26,6 +26,7 @@ DbtRuntimeError, ) from dbt.node_types import NodeType +from dbt.task.contextvars import cv_project_root SELECTOR_GLOB = "*" @@ -313,8 +314,8 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu class PathSelectorMethod(SelectorMethod): def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]: """Yields nodes from included that match the given path.""" - # use '.' and not 'root' for easy comparison - root = Path.cwd() + # get project root from contextvar + root = Path(cv_project_root.get()) paths = set(p.relative_to(root) for p in root.glob(selector)) for node, real_node in self.all_nodes(included_nodes): ofp = Path(real_node.original_file_path) diff --git a/core/dbt/task/base.py b/core/dbt/task/base.py index 2e33d5769e9..36722cfe3fd 100644 --- a/core/dbt/task/base.py +++ b/core/dbt/task/base.py @@ -45,6 +45,7 @@ from dbt.graph import Graph from dbt.logger import log_manager from .printer import print_run_result_error +from dbt.task.contextvars import cv_project_root class NoneConfig: @@ -75,6 +76,8 @@ def __init__(self, args, config, project=None): self.args = args self.config = config self.project = config if isinstance(config, Project) else project + if self.config: + cv_project_root.set(self.config.project_root) @classmethod def pre_init_hook(cls, args): diff --git a/core/dbt/task/contextvars.py b/core/dbt/task/contextvars.py new file mode 100644 index 00000000000..6524b0935d1 --- /dev/null +++ b/core/dbt/task/contextvars.py @@ -0,0 +1,6 @@ +from contextvars import ContextVar + +# This is a place to hold common contextvars used in tasks so that we can +# avoid circular imports. + +cv_project_root: ContextVar = ContextVar("project_root") diff --git a/tests/functional/graph_selection/test_graph_selection.py b/tests/functional/graph_selection/test_graph_selection.py index 6263c1d2c12..88b45c8bcf5 100644 --- a/tests/functional/graph_selection/test_graph_selection.py +++ b/tests/functional/graph_selection/test_graph_selection.py @@ -121,11 +121,24 @@ def test_locally_qualified_name(self, project): check_result_nodes_by_name(results, ["nested_users", "subdir", "versioned"]) assert_correct_schemas(project) - results = run_dbt(["run", "--select", "models/test/subdir*"]) + os.chdir( + project.profiles_dir + ) # Change to random directory to test that Path selector works with project-dir + results = run_dbt( + ["run", "--project-dir", str(project.project_root), "--select", "models/test/subdir*"] + ) check_result_nodes_by_name(results, ["nested_users", "subdir", "versioned"]) assert_correct_schemas(project) - results = run_dbt(["build", "--select", "models/patch_path_selection_schema.yml"]) + results = run_dbt( + [ + "build", + "--project-dir", + str(project.project_root), + "--select", + "models/patch_path_selection_schema.yml", + ] + ) check_result_nodes_by_name(results, ["subdir"]) assert_correct_schemas(project)