Skip to content

Allow use of DBT_PROJECT_DIR env var as per help text and dbt cli#82

Open
anentropic wants to merge 2 commits intomemiiso:mainfrom
anentropic:patch-1
Open

Allow use of DBT_PROJECT_DIR env var as per help text and dbt cli#82
anentropic wants to merge 2 commits intomemiiso:mainfrom
anentropic:patch-1

Conversation

@anentropic
Copy link
Copy Markdown
Contributor

Fix to make DBT_PROJECT_DIR and DBT_PROFILES_DIR env vars work as cli defaults.

dbt cli allows this, and the help text in opendbt cli args say you can but it doesn't work.

The reason is it currently just takes the argparse result (None) and falls back to else default_project_dir() ...but the dbt default_project_dir function doesn't do the env var handling.

Instead that is a feature of the click interface:
https://github.com/dbt-labs/dbt-core/blob/main/core/dbt/cli/params.py#L476

So for dbt cli the default function is only called if no env var defined.

We can reproduce the same behaviour in argparse with some helper code adapted from:
https://stackoverflow.com/a/10551190/202168

@anentropic
Copy link
Copy Markdown
Contributor Author

I think the errors in the dbt output in the tests are expected cases?

so the failing test here is:

======================================================================
ERROR: test_airflow (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_airflow

...

docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

...i.e. something in testcontainers failing at import time?

Comment thread opendbt/__main__.py
envvar="DBT_PROFILES_DIR",
help="Path to the dbt profiles directory. Defaults to the DBT_PROFILES_DIR environment variable.",
)
ns, args = parser.parse_known_args()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @anentropic adding it, would it be simpler to add if else block instead? what do you think?

if ns.project_dir:
    project_dir = Path(ns.project_dir)
elif os.getenv("DBT_PROJECT_DIR"):
    project_dir = Path(os.getenv("DBT_PROJECT_DIR"))
else:
    project_dir = default_project_dir()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants