Skip to content

Commit

Permalink
add env DBT_PROJECT_DIR support dbt-labs#6078
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-schick committed Jan 19, 2023
1 parent 07a004b commit 1e6e33b
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230119-141156.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: add support for DBT_PROJECT_DIR env var
time: 2023-01-19T14:11:56.638325919+01:00
custom:
Author: leo-schick
Issue: "6078"
2 changes: 1 addition & 1 deletion core/dbt/cli/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@

project_dir = click.option(
"--project-dir",
envvar=None,
envvar="DBT_PROJECT_DIR",
help="Which directory to look in for the dbt_project.yml file. Default is the current working directory and its parents.",
default=default_project_dir(),
type=click.Path(exists=True),
Expand Down
2 changes: 1 addition & 1 deletion core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def collect_project(
project_renderer: Optional[DbtProjectYamlRenderer] = None,
) -> Union[Project, PartialProject]:

project_root = args.project_dir if args.project_dir else os.getcwd()
project_root = flags.PROJECT_DIR if flags.PROJECT_DIR else os.getcwd()
version_check = bool(flags.VERSION_CHECK)
partial = Project.partial_load(project_root, verify_version=version_check)
if project_renderer is None:
Expand Down
7 changes: 6 additions & 1 deletion core/dbt/flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
CACHE_SELECTED_ONLY = None
TARGET_PATH = None
LOG_PATH = None
PROJECT_DIR = None

_NON_BOOLEAN_FLAGS = [
"LOG_FORMAT",
Expand All @@ -56,6 +57,7 @@
"TARGET_PATH",
"LOG_PATH",
"WARN_ERROR_OPTIONS",
"PROJECT_DIR",
]

_NON_DBT_ENV_FLAGS = ["DO_NOT_TRACK"]
Expand Down Expand Up @@ -86,6 +88,7 @@
"CACHE_SELECTED_ONLY": False,
"TARGET_PATH": None,
"LOG_PATH": None,
"PROJECT_DIR": None,
}


Expand Down Expand Up @@ -137,7 +140,7 @@ def set_from_args(args, user_config):
global WRITE_JSON, PARTIAL_PARSE, USE_COLORS, STORE_FAILURES, PROFILES_DIR, DEBUG, LOG_FORMAT
global INDIRECT_SELECTION, VERSION_CHECK, FAIL_FAST, SEND_ANONYMOUS_USAGE_STATS
global PRINTER_WIDTH, WHICH, LOG_CACHE_EVENTS, QUIET, NO_PRINT, CACHE_SELECTED_ONLY
global TARGET_PATH, LOG_PATH
global TARGET_PATH, LOG_PATH, PROJECT_DIR

STRICT_MODE = False # backwards compatibility
# cli args without user_config or env var option
Expand Down Expand Up @@ -168,6 +171,7 @@ def set_from_args(args, user_config):
CACHE_SELECTED_ONLY = get_flag_value("CACHE_SELECTED_ONLY", args, user_config)
TARGET_PATH = get_flag_value("TARGET_PATH", args, user_config)
LOG_PATH = get_flag_value("LOG_PATH", args, user_config)
PROJECT_DIR = get_flag_value("PROJECT_DIR", args, user_config)

_set_overrides_from_env()

Expand Down Expand Up @@ -267,6 +271,7 @@ def get_flag_dict():
"cache_selected_only": CACHE_SELECTED_ONLY,
"target_path": TARGET_PATH,
"log_path": LOG_PATH,
"project_dir": PROJECT_DIR,
}


Expand Down
6 changes: 3 additions & 3 deletions core/dbt/task/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ def interpret_results(self, results):
def get_nearest_project_dir(args):
# If the user provides an explicit project directory, use that
# but don't look at parent directories.
if args.project_dir:
project_file = os.path.join(args.project_dir, "dbt_project.yml")
if flags.PROJECT_DIR:
project_file = os.path.join(flags.PROJECT_DIR, "dbt_project.yml")
if os.path.exists(project_file):
return args.project_dir
return flags.PROJECT_DIR
else:
raise dbt.exceptions.DbtRuntimeError(
"fatal: Invalid --project-dir flag. Not a dbt project. "
Expand Down
4 changes: 2 additions & 2 deletions core/dbt/task/debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def __init__(self, args, config):
except dbt.exceptions.Exception:
# we probably couldn't find a project directory. Set project dir
# to whatever was given, or default to the current directory.
if args.project_dir:
self.project_dir = args.project_dir
if flags.PROJECT_DIR:
self.project_dir = flags.PROJECT_DIR
else:
self.project_dir = os.getcwd()
self.project_path = os.path.join(self.project_dir, "dbt_project.yml")
Expand Down
13 changes: 13 additions & 0 deletions test/unit/test_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ def test__flags(self):
os.environ.pop('DBT_LOG_PATH')
delattr(self.args, 'log_path')

# project_dir
flags.set_from_args(self.args, self.user_config)
self.assertIsNone(flags.PROJECT_DIR)
os.environ['DBT_PROJECT_DIR'] = 'a/b/c'
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.PROJECT_DIR, 'a/b/c')
setattr(self.args, 'project_dir', 'd/e/f')
flags.set_from_args(self.args, self.user_config)
self.assertEqual(flags.PROJECT_DIR, 'd/e/f')
# cleanup
os.environ.pop('DBT_PROJECT_DIR')
delattr(self.args, 'project_dir')

def test__flags_are_mutually_exclusive(self):
# options from user config
self.user_config.warn_error = False
Expand Down

0 comments on commit 1e6e33b

Please sign in to comment.