Repository: dbt-labs/dbt-core

Issue:
[CT-1357] [Feature] DBT_PROJECT_DIR env var
### Is this your first time submitting a feature request?

- [X] I have read the [expectations for open source contributors](https://docs.getdbt.com/docs/contributing/oss-expectations)
- [X] I have searched the existing issues, and I could not find an existing issue for this feature
- [X] I am requesting a straightforward extension of existing dbt functionality, rather than a Big Idea better suited to a discussion

### Describe the feature

_First proposed in https://github.com/dbt-labs/dbt-core/issues/5411. Not included in initial implementation from https://github.com/dbt-labs/dbt-core/pull/5717_

It should be possible to set the `--project-dir` CLI flag via env var instead: `DBT_PROJECT_DIR`

It's _very_ easy to do this using the new CLI framework:
https://github.com/dbt-labs/dbt-core/blob/73aebd815976ddfe09385066dd9faa95e5f49e29/core/dbt/cli/params.py#L220-L234

It's a bit trickier for the old framework. See prior art from `--profiles-dir`:
https://github.com/dbt-labs/dbt-core/blob/73aebd815976ddfe09385066dd9faa95e5f49e29/core/dbt/main.py#L1189-L1201

We could consider waiting until we've cut over to the new CLI. (planned for v1.4 or v1.5)

### Describe alternatives you've considered

Not doing this. It's a convenience (and consistency) thing. Given some recent comments on #5411, it sounds like this would help some people

### Who will this benefit?

People who want to deploy dbt projects nested in subdirectories of larger repos, and want to set an env var once, rather than passing the `--project-dir` flag into every command

### Are you interested in contributing this feature?

I think this could be a good second issue for a motivated community contributor!

### Anything else?

_No response_


Discussion:
> It's very easy to do this using the new CLI framework:

Yeah! Hip, hip, hooray for the new CLI framework! Nothing better than a one-liner 💪 

> We could consider waiting until we've cut over to the new CLI. (planned for v1.4 or v1.5)

Agreed that this would be a good second issue for a motivated community contributor. I would affirm the option to wait until we've cut over to the new CLI If such a person doesn't emerge.
Hi, @dbeatty10, @jtcohen6 - is it correct that it's not possible to use the extension today if `dbt_project.yml` is not in the root directory?

I've tried numerous workarounds with no luck as of yet. Then I found this issue. 😄 

Thanks!
Yep, that sounds right @aaronsteers

Let's say you have an environment variable named `DBT_PROJECT_DIR`.

Then here's a couple options using zsh/bash:
1. `dbt build --project-dir $DBT_PROJECT_DIR`
1. `(cd $DBT_PROJECT_DIR && dbt build)`

Once this issue is resolved and included in a release, then you'd be able to do the following, no matter the current working directory:
```shell
dbt build
```

Resolving patch:
diff --git a/core/dbt/cli/params.py b/core/dbt/cli/params.py
--- a/core/dbt/cli/params.py
+++ b/core/dbt/cli/params.py
@@ -226,7 +226,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),


Test patch: