dbt-core icon indicating copy to clipboard operation
dbt-core copied to clipboard

[Regression] Unable to access environment variables

Open DanMawdsleyBA opened this issue 1 year ago • 15 comments

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

DanMawdsleyBA avatar Jul 09 '24 10:07 DanMawdsleyBA

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.

dbeatty10 avatar Jul 09 '24 16:07 dbeatty10

``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'}

DanMawdsleyBA avatar Jul 10 '24 09:07 DanMawdsleyBA

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

dbeatty10 avatar Jul 10 '24 14:07 dbeatty10

Here is a screenshot with an echo to the local user here: image

DanMawdsleyBA avatar Jul 10 '24 15:07 DanMawdsleyBA

@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}")

dbeatty10 avatar Jul 11 '24 03:07 dbeatty10

Running the first version here: image

Running the second script here: image

DanMawdsleyBA avatar Jul 11 '24 08:07 DanMawdsleyBA

@DanMawdsleyBA Could it be something to do with the casing? Does "{{ env_var('LOCAL_USER') }}" work?

aranke avatar Jul 11 '24 20:07 aranke

That seems to work: image

Although strange this behavior only changed between 1.7 and 1.8 .

DanMawdsleyBA avatar Jul 12 '24 11:07 DanMawdsleyBA

@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")}
  1. {{ env_var("local_user") }} yields "dan"
  2. {{ env_var("LOCAL_USER") }} yields "dan"
  3. {{ env_var("lOcAl_UsEr") }} yields "dan"

Unix-like:

export local_user=dan
  1. {{ env_var("local_user") }} yields "dan"
  2. {{ env_var("LOCAL_USER") }} yields ""
  3. {{ env_var("lOcAl_UsEr") }} yields ""

dbeatty10 avatar Jul 12 '24 14:07 dbeatty10

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?

dvanbolt avatar Nov 04 '24 15:11 dvanbolt

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!

rupert160 avatar Mar 15 '25 22:03 rupert160

Workaround

I don't know the root cause, but here's a workaround in the meantime:

  • use uppercase names whenever using the env_var macro

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") }}

dbeatty10 avatar Apr 22 '25 05:04 dbeatty10

I'm having a similar issue as well on mac.

Image

Setup:

  • macos 15
  • python 3.12
  • dbt-core 1.8.9

kanenorman avatar Apr 30 '25 23:04 kanenorman

@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.

dbeatty10 avatar May 01 '25 13:05 dbeatty10

Same issue here, on Windows. Wanted to upgrade from 1.7.x to 1.9.x but this happens. Using lowercase variable names.

holcd avatar May 23 '25 14:05 holcd