Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 1.5.latest] Use project directory in path selector instead of cwd #7850

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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)
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