Skip to content

Commit

Permalink
Use project directory in path selector instead of cwd (#7829)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gshank committed Jun 13, 2023
1 parent 4a833a4 commit ca73a2a
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230608-135952.yaml
Original file line number Diff line number Diff line change
@@ -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"
5 changes: 3 additions & 2 deletions core/dbt/graph/selector_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
DbtRuntimeError,
)
from dbt.node_types import NodeType
from dbt.task.contextvars import cv_project_root


SELECTOR_GLOB = "*"
Expand Down Expand Up @@ -324,8 +325,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)
Expand Down
3 changes: 3 additions & 0 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
6 changes: 6 additions & 0 deletions core/dbt/task/contextvars.py
Original file line number Diff line number Diff line change
@@ -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")
17 changes: 15 additions & 2 deletions tests/functional/graph_selection/test_graph_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit ca73a2a

Please sign in to comment.