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

Fix/changes current working dir when using a dbt project dir #9596

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -237,10 +237,9 @@ def build(ctx, **kwargs):
@requires.project
def clean(ctx, **kwargs):
"""Delete all folders in the clean-targets list (usually the dbt_packages and target directories.)"""
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 @@ -457,9 +456,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 @@ -479,10 +478,9 @@ def deps(ctx, **kwargs):
@requires.preflight
def init(ctx, **kwargs):
"""Initialize a new dbt project."""
task = InitTask(ctx.obj["flags"], None)

results = task.run()
success = task.interpret_results(results)
with InitTask(ctx.obj["flags"], None) 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 @@ -77,6 +77,13 @@ def __init__(self, args, config, project=None) -> None:
self.config = config
self.project = config if isinstance(config, Project) else project

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

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

@classmethod
def pre_init_hook(cls, args):
"""A hook called before the task is initialized."""
Expand Down
4 changes: 1 addition & 3 deletions core/dbt/task/deps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from hashlib import sha1
from typing import Any, Dict, Optional, List
import yaml
from pathlib import Path
import dbt.utils
import dbt.deprecations
import dbt.exceptions
Expand Down Expand Up @@ -98,9 +97,7 @@ def __init__(self, args: Any, project: Project) -> None:
# --project-dir with deps. A larger overhaul of our path handling methods
# is needed to fix this the "right" way.
# See GH-7615
project.project_root = str(Path(project.project_root).resolve())

move_to_nearest_project_dir(project.project_root)
super().__init__(args=args, config=None, project=project)
self.cli_vars = args.vars

Expand Down Expand Up @@ -206,6 +203,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