Skip to content

Commit

Permalink
Fix/changes current working dir when using a dbt project dir (#9596)
Browse files Browse the repository at this point in the history
* made class changing directory a context manager.

* add change log

* fix conflict

* made base as a context manager

* add assertion

* Remove index.html

* add it test to testDbtRunner

* fix deps args order

* fix test

---------

Co-authored-by: Doug Beatty <doug.beatty@dbtlabs.com>
Co-authored-by: Chenyu Li <chenyu.li@dbtlabs.com>
  • Loading branch information
3 people authored Sep 3, 2024
1 parent 37d382c commit b56d96d
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Breaking Changes-20231206-192442.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Breaking Changes
body: Fix changing the current working directory when using dpt deps, clean and init.
time: 2023-12-06T19:24:42.575372+09:00
custom:
Author: rariyama
Issue: "8997"
20 changes: 9 additions & 11 deletions core/dbt/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,9 @@ def clean(ctx, **kwargs):
"""Delete all folders in the clean-targets list (usually the dbt_packages and target directories.)"""
from dbt.task.clean import CleanTask

task = CleanTask(ctx.obj["flags"], ctx.obj["project"])

results = task.run()
success = task.interpret_results(results)
with CleanTask(ctx.obj["flags"], ctx.obj["project"]) as task:
results = task.run()
success = task.interpret_results(results)
return results, success


Expand Down Expand Up @@ -437,9 +436,9 @@ def deps(ctx, **kwargs):
message=f"Version is required in --add-package when a package when source is {flags.SOURCE}",
option_name="--add-package",
)
task = DepsTask(flags, ctx.obj["project"])
results = task.run()
success = task.interpret_results(results)
with DepsTask(flags, ctx.obj["project"]) as task:
results = task.run()
success = task.interpret_results(results)
return results, success


Expand All @@ -459,10 +458,9 @@ def init(ctx, **kwargs):
"""Initialize a new dbt project."""
from dbt.task.init import InitTask

task = InitTask(ctx.obj["flags"])

results = task.run()
success = task.interpret_results(results)
with InitTask(ctx.obj["flags"]) as task:
results = task.run()
success = task.interpret_results(results)
return results, success


Expand Down
7 changes: 7 additions & 0 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ class BaseTask(metaclass=ABCMeta):
def __init__(self, args: Flags) -> None:
self.args = args

def __enter__(self):
self.orig_dir = os.getcwd()
return self

def __exit__(self, exc_type, exc_value, traceback):
os.chdir(self.orig_dir)

@abstractmethod
def run(self):
raise dbt_common.exceptions.base.NotImplementedError("Not Implemented")
Expand Down
3 changes: 1 addition & 2 deletions core/dbt/task/deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ def __init__(self, args: Any, project: Project) -> None:
# See GH-7615
project.project_root = str(Path(project.project_root).resolve())
self.project = project

move_to_nearest_project_dir(project.project_root)
self.cli_vars = args.vars

def track_package_install(
Expand Down Expand Up @@ -202,6 +200,7 @@ def lock(self) -> None:
fire_event(DepsLockUpdating(lock_filepath=lock_filepath))

def run(self) -> None:
move_to_nearest_project_dir(self.args.project_dir)
if self.args.add_package:
self.add()

Expand Down
3 changes: 2 additions & 1 deletion core/dbt/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def run_dbt(
if profiles_dir and "--profiles-dir" not in args:
args.extend(["--profiles-dir", profiles_dir])
dbt = dbtRunner()

res = dbt.invoke(args)

# the exception is immediately raised to be caught in tests
Expand Down Expand Up @@ -148,7 +149,7 @@ def get_manifest(project_root) -> Optional[Manifest]:
if os.path.exists(path):
with open(path, "rb") as fp:
manifest_mp = fp.read()
manifest: Manifest = Manifest.from_msgpack(manifest_mp)
manifest: Manifest = Manifest.from_msgpack(manifest_mp) # type: ignore[attr-defined]
return manifest
else:
return None
Expand Down
16 changes: 16 additions & 0 deletions tests/functional/dbt_runner/test_dbt_runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from unittest import mock

import pytest
Expand Down Expand Up @@ -103,6 +104,21 @@ def test_pass_in_args_variable(self, dbt):
dbt.invoke(args)
assert args == args_before

def test_directory_does_not_change(self, project, dbt: dbtRunner) -> None:
project_dir = os.getcwd() # The directory where dbt_project.yml exists.
os.chdir("../")
cmd_execution_dir = os.getcwd() # The directory where dbt command will be run

commands = ["init", "deps", "clean"]
for command in commands:
args = [command, "--project-dir", project_dir]
if command == "init":
args.append("--skip-profile-setup")
res = dbt.invoke(args)
after_dir = os.getcwd()
assert res.success is True
assert cmd_execution_dir == after_dir


class TestDbtRunnerQueryComments:
@pytest.fixture(scope="class")
Expand Down

0 comments on commit b56d96d

Please sign in to comment.