[Regression] Unable to access environment variables
Is this a new bug in dbt-core?
- [X] I believe this is a new bug in dbt-core
- [X] I have searched the existing issues, and I could not find an existing issue for this bug
Current Behavior
Environment variables are no longer working. I was using dbt-core 1.7.3 previously where I use environment varaibles for my profiles.yml file. Since upgrading to 1.8.3 dbt is no longer able to access these same environement variables.
I have my environement varaibles setup in vscode under the settings json file example below:
"terminal.integrated.env.windows": {
"local_user":"dan")}
Expected Behavior
Expect DBT to be able to read my environement variables like in the previous version of dbt 1.7 .
Steps To Reproduce
Upgrade to dbt core 1.8 . Use environment variables as part of dbt. In the profiles file example below:
dbt_profile:
target: snowflake_local
outputs:
snowflake_local:
type: snowflake
account: ba.eu-west-1
user: "{{ env_var('local_user') }}"
authenticator: externalbrowser
role: "{{ env_var('local_role') }}"
database: "{{ env_var('local_database') }}"
schema: "{{ env_var('local_schema') }}"
threads: 1
warehouse: DEVELOPER
Relevant log output
dbt parse
10:16:51 Running with dbt=1.8.3
10:16:51 Encountered an error:
Parsing Error
Env var required but not provided: 'local_user'
Environment
- OS: Windows
- Python: 3.11
- dbt: 1.8.3
Which database adapter are you using with dbt?
snowflake
Additional Context
No response
Thanks for reaching out @DanMawdsleyBA !
Are you sure that the local_user environment variable was defined when you ran dbt 1.8?
I tried this in both 1.7 and 1.8, and I got the same error when it was not defined:
16:54:38 Encountered an error:
Parsing Error
Env var required but not provided: 'local_user'
But things worked as expected for me in both 1.8 and 1.7 when it was defined.
``Yes all of my environment variables were setup before and working in dbt 1.7 .
I have setup a new venv running with dbt 1.7 which does work so it only seems to only be introduced when I switch to 1.8.
Logs of 1.7:
dbt parse
08:51:39 Running with dbt=1.7.0
08:51:41 Registered adapter: snowflake=1.7.0
08:51:41 Performance info: C:\Users\u236618\Documents\Github\olympus-utils\tests\sql\snowflake\dbt\jaffle_shop\target\perf_info.json
I'm wondering if the OS or something else is different in your testing. I'm currently running on Windows .
Logs of command on 1.7:
[0m09:58:45.380152 [debug] [MainThread]: running dbt with arguments {'printer_width': '80', 'indirect_selection': 'eager', 'log_cache_events': 'False', 'write_json': 'True', 'partial_parse': 'True', 'cache_selected_only': 'False', 'warn_error': 'None', 'debug': 'False', 'fail_fast': 'False', 'log_path': 'C:\\Users\\u236618\\Documents\\Github\\olympus-utils\\tests\\sql\\snowflake\\dbt\\jaffle_shop\\logs', 'version_check': 'True', 'profiles_dir': 'C:\\Users\\u236618\\Documents\\Github\\olympus-utils\\tests\\sql\\snowflake\\dbt\\jaffle_shop', 'use_colors': 'True', 'use_experimental_parser': 'False', 'no_print': 'None', 'quiet': 'False', 'warn_error_options': 'WarnErrorOptions(include=[], exclude=[])', 'static_parser': 'True', 'invocation_command': 'dbt parse', 'introspect': 'True', 'target_path': 'None', 'log_format': 'default', 'send_anonymous_usage_stats': 'True'}
Same command on 1.8:
[0m09:55:30.356674 [debug] [MainThread]: running dbt with arguments {'printer_width': '80', 'indirect_selection': 'eager', 'write_json': 'True', 'log_cache_events': 'False', 'partial_parse': 'True', 'cache_selected_only': 'False', 'warn_error': 'None', 'debug': 'False', 'version_check': 'True', 'log_path': 'C:\\Users\\u236618\\Documents\\Github\\olympus-utils\\tests\\sql\\snowflake\\dbt\\jaffle_shop\\logs', 'fail_fast': 'False', 'profiles_dir': 'C:\\Users\\u236618\\Documents\\Github\\olympus-utils\\tests\\sql\\snowflake\\dbt\\jaffle_shop', 'use_colors': 'True', 'use_experimental_parser': 'False', 'empty': 'None', 'quiet': 'False', 'no_print': 'None', 'log_format': 'default', 'introspect': 'True', 'warn_error_options': 'WarnErrorOptions(include=[], exclude=[])', 'invocation_command': 'dbt parse', 'target_path': 'None', 'static_parser': 'True', 'send_anonymous_usage_stats': 'True'}
I'm using the zsh shell in macOS to compare v1.7 and v1.8, and loading my environment variables using direnv. It is working the same way in both environments for me.
For clarity, you can see the see the value of the environment variable in both your v1.7 and v1.8 environments when you print out the value using one of the following methods?
I'm wondering if local_user is set in your v1.7 environment, but somehow not set in your v1.8 environment.
Using Command Prompt (cmd):
echo %local_user%
Using PowerShell:
echo $env:local_user
Here is a screenshot with an echo to the local user here:
@DanMawdsleyBA I'm not sure what's going on here. We did make an update within env_var a handful of months ago within https://github.com/dbt-labs/dbt-core/pull/9489, but nothing is standing out to me that would affect only Windows.
Could you try running each of the following Python scripts to see if each recognizes your local_user environment variable?
Show just one environment variable:
import os
var = "local_user"
print("\nSpecific variable:\n")
if var in os.environ:
print(f"Environment variable '{var}': {os.environ[var]}")
else:
print(f"Environment variable '{var}' not found")
Show all environment variables:
import os
print("\nAll environment variables:\n")
for key, value in os.environ.items():
print(f"{key}: {value}")
Running the first version here:
Running the second script here:
@DanMawdsleyBA Could it be something to do with the casing?
Does "{{ env_var('LOCAL_USER') }}" work?
That seems to work:
Although strange this behavior only changed between 1.7 and 1.8 .
@DanMawdsleyBA glad that we discovered a workaround by changing the variable name to uppercase.
We're wondering if something about the caching implementation of https://github.com/dbt-labs/dbt-core/pull/9489 lost Windows' case-insensitivity of environment variable names and that explains the difference between v1.7 and v1.8.
In terms of testing, @aranke is going to try something like the following:
Windows:
"terminal.integrated.env.windows": {
"local_user":"dan")}
{{ env_var("local_user") }}yields "dan"{{ env_var("LOCAL_USER") }}yields "dan"{{ env_var("lOcAl_UsEr") }}yields "dan"
Unix-like:
export local_user=dan
{{ env_var("local_user") }}yields "dan"{{ env_var("LOCAL_USER") }}yields ""{{ env_var("lOcAl_UsEr") }}yields ""
I am experiencing the same issue and having to roll back all projects to 1.7. Confirmed with echo $env:x that the variables are loaded into the environment. Is there any path forward or workaround for this?
I can confirm regression also, as I have reverted back to dbt-core 1.7.19 on Windows Server 2019 to get environment substitution The regression occurred between 1.7.19 and 1.8.0 & as at 1.9.3 has still not been fixed (for windows at least).
(dbt) PS C:\Users\a-baileyrc\DevOps\SCHHS_DataOps> dbt --version Core:
- installed: 1.7.19
- latest: 1.9.3 - Update available!
Your version of dbt-core is out of date! You can find instructions for upgrading here: https://docs.getdbt.com/docs/installation
Plugins:
- fabric: 1.7.4 - Update available!
- synapse: 1.7.4 - Update available!
Workaround
I don't know the root cause, but here's a workaround in the meantime:
- use uppercase names whenever using the
env_varmacro
This workaround was successful for me using dbt-core 1.9.4 with Windows PowerShell.
To the best of my knowledge, this only affects Windows (where environment variables are not case-sensitive). I believe this does not affect Unix-like operating systems where environment variables are case-sensitive.
Example
Suppose I set an environment variable within Windows PowerShell like this:
$env:lOcAl_UsEr = "Dan"
Then I needed to do this:
{{ env_var("LOCAL_USER") }}
instead of this:
{{ env_var("lOcAl_UsEr") }}
I'm having a similar issue as well on mac.
Setup:
- macos 15
- python 3.12
- dbt-core 1.8.9
@kanenorman I'm pretty sure there's something that is affecting Windows users specifically, and I'd like to keep this issue focused on that.
Since you are experiencing the same error message (but likely different root cause), would you be willing to open a separate issue here with the details of what you are experiencing? The most important thing will be for you to share all the details we'd need to try to reproduce the issue ourselves: file contents + commands + log output.
Same issue here, on Windows. Wanted to upgrade from 1.7.x to 1.9.x but this happens. Using lowercase variable names.