Skip to content

Commit ca73a2a

Browse files
authored
Use project directory in path selector instead of cwd (#7829)
* Use contextvar to store and get project_root for path selector method * Changie * Modify test to check Path selector with project-dir * Don't set cv_project_root in base task if no config
1 parent 4a833a4 commit ca73a2a

File tree

5 files changed

+33
-4
lines changed

5 files changed

+33
-4
lines changed
Lines changed: 6 additions & 0 deletions
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

Lines changed: 3 additions & 2 deletions
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 = "*"
@@ -324,8 +325,8 @@ def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[Uniqu
324325
class PathSelectorMethod(SelectorMethod):
325326
def search(self, included_nodes: Set[UniqueId], selector: str) -> Iterator[UniqueId]:
326327
"""Yields nodes from included that match the given path."""
327-
# use '.' and not 'root' for easy comparison
328-
root = Path.cwd()
328+
# get project root from contextvar
329+
root = Path(cv_project_root.get())
329330
paths = set(p.relative_to(root) for p in root.glob(selector))
330331
for node, real_node in self.all_nodes(included_nodes):
331332
ofp = Path(real_node.original_file_path)

core/dbt/task/base.py

Lines changed: 3 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 15 additions & 2 deletions
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)