Skip to content

Commit b003e7d

Browse files
Fix using project-dir with list command and path selector (#8388) (#8427)
(cherry picked from commit 048553d) Co-authored-by: Gerda Shank <[email protected]>
1 parent 0f9f5dd commit b003e7d

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
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 using list command with path selector and project-dir
3+
time: 2023-08-14T14:57:02.02816-04:00
4+
custom:
5+
Author: gshank
6+
Issue: "8385"

core/dbt/task/list.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
ListCmdOut,
1616
)
1717
from dbt.exceptions import DbtRuntimeError, DbtInternalError
18+
from dbt.events.contextvars import task_contextvars
1819

1920

2021
class ListTask(GraphRunnableTask):
@@ -123,20 +124,23 @@ def generate_paths(self):
123124
yield node.original_file_path
124125

125126
def run(self):
126-
self.compile_manifest()
127-
output = self.args.output
128-
if output == "selector":
129-
generator = self.generate_selectors
130-
elif output == "name":
131-
generator = self.generate_names
132-
elif output == "json":
133-
generator = self.generate_json
134-
elif output == "path":
135-
generator = self.generate_paths
136-
else:
137-
raise DbtInternalError("Invalid output {}".format(output))
127+
# We set up a context manager here with "task_contextvars" because we
128+
# we need the project_root in compile_manifest.
129+
with task_contextvars(project_root=self.config.project_root):
130+
self.compile_manifest()
131+
output = self.args.output
132+
if output == "selector":
133+
generator = self.generate_selectors
134+
elif output == "name":
135+
generator = self.generate_names
136+
elif output == "json":
137+
generator = self.generate_json
138+
elif output == "path":
139+
generator = self.generate_paths
140+
else:
141+
raise DbtInternalError("Invalid output {}".format(output))
138142

139-
return self.output_results(generator())
143+
return self.output_results(generator())
140144

141145
def output_results(self, results):
142146
"""Log, or output a plain, newline-delimited, and ready-to-pipe list of nodes found."""

tests/functional/graph_selection/test_graph_selection.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,24 @@ def test_locally_qualified_name(self, project):
142142
check_result_nodes_by_name(results, ["subdir"])
143143
assert_correct_schemas(project)
144144

145+
# Check that list command works
146+
os.chdir(
147+
project.profiles_dir
148+
) # Change to random directory to test that Path selector works with project-dir
149+
results = run_dbt(
150+
[
151+
"-q",
152+
"ls",
153+
"-s",
154+
"path:models/test/subdir.sql",
155+
"--project-dir",
156+
str(project.project_root),
157+
]
158+
# ["list", "--project-dir", str(project.project_root), "--select", "models/test/subdir*"]
159+
)
160+
print(f"--- results: {results}")
161+
assert len(results) == 1
162+
145163
def test_locally_qualified_name_model_with_dots(self, project):
146164
results = run_dbt(["run", "--select", "alternative.users"], expect_pass=False)
147165
check_result_nodes_by_name(results, ["alternative.users"])
@@ -268,3 +286,22 @@ def test_exposure_parents(self, project):
268286
"users",
269287
],
270288
)
289+
290+
291+
class TestListPathGraphSelection(SelectionFixtures):
292+
def test_list_select_with_project_dir(self, project):
293+
# Check that list command works
294+
os.chdir(
295+
project.profiles_dir
296+
) # Change to random directory to test that Path selector works with project-dir
297+
results = run_dbt(
298+
[
299+
"-q",
300+
"ls",
301+
"-s",
302+
"path:models/test/subdir.sql",
303+
"--project-dir",
304+
str(project.project_root),
305+
]
306+
)
307+
assert results == ["test.test.subdir"]

0 commit comments

Comments
 (0)