Skip to content

Commit 11d9f99

Browse files
Use project directory in path selector instead of cwd (#7829) (#7850)
(cherry picked from commit ca73a2a) Co-authored-by: Gerda Shank <[email protected]>
1 parent 27eb48c commit 11d9f99

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Fixes
2+
body: Fix path selector when using project-dir
3+
time: 2023-06-08T13:59:52.95775-04:00
4+
custom:
5+
Author: gshank
6+
Issue: "7819"

core/dbt/graph/selector_methods.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
DbtRuntimeError,
2727
)
2828
from dbt.node_types import NodeType
29+
from dbt.task.contextvars import cv_project_root
2930

3031

3132
SELECTOR_GLOB = "*"
@@ -313,8 +314,8 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu
313314
class PathSelectorMethod(SelectorMethod):
314315
def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]:
315316
"""Yields nodes from included that match the given path."""
316-
# use '.' and not 'root' for easy comparison
317-
root = Path.cwd()
317+
# get project root from contextvar
318+
root = Path(cv_project_root.get())
318319
paths = set(p.relative_to(root) for p in root.glob(selector))
319320
for node, real_node in self.all_nodes(included_nodes):
320321
ofp = Path(real_node.original_file_path)

core/dbt/task/base.py

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from dbt.graph import Graph
4646
from dbt.logger import log_manager
4747
from .printer import print_run_result_error
48+
from dbt.task.contextvars import cv_project_root
4849

4950

5051
class NoneConfig:
@@ -75,6 +76,8 @@ def __init__(self, args, config, project=None):
7576
self.args = args
7677
self.config = config
7778
self.project = config if isinstance(config, Project) else project
79+
if self.config:
80+
cv_project_root.set(self.config.project_root)
7881

7982
@classmethod
8083
def pre_init_hook(cls, args):

core/dbt/task/contextvars.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from contextvars import ContextVar
2+
3+
# This is a place to hold common contextvars used in tasks so that we can
4+
# avoid circular imports.
5+
6+
cv_project_root: ContextVar = ContextVar("project_root")

tests/functional/graph_selection/test_graph_selection.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,24 @@ def test_locally_qualified_name(self, project):
121121
check_result_nodes_by_name(results, ["nested_users", "subdir", "versioned"])
122122
assert_correct_schemas(project)
123123

124-
results = run_dbt(["run", "--select", "models/test/subdir*"])
124+
os.chdir(
125+
project.profiles_dir
126+
) # Change to random directory to test that Path selector works with project-dir
127+
results = run_dbt(
128+
["run", "--project-dir", str(project.project_root), "--select", "models/test/subdir*"]
129+
)
125130
check_result_nodes_by_name(results, ["nested_users", "subdir", "versioned"])
126131
assert_correct_schemas(project)
127132

128-
results = run_dbt(["build", "--select", "models/patch_path_selection_schema.yml"])
133+
results = run_dbt(
134+
[
135+
"build",
136+
"--project-dir",
137+
str(project.project_root),
138+
"--select",
139+
"models/patch_path_selection_schema.yml",
140+
]
141+
)
129142
check_result_nodes_by_name(results, ["subdir"])
130143
assert_correct_schemas(project)
131144

0 commit comments

Comments
 (0)