Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty string environment variables are not included during tests #22337

Closed
Zhong-z opened this issue Oct 25, 2023 · 11 comments
Closed

Empty string environment variables are not included during tests #22337

Zhong-z opened this issue Oct 25, 2023 · 11 comments
Assignees
Labels
area-testing triage-needed Needs assignment to the proper sub-team

Comments

@Zhong-z
Copy link

Zhong-z commented Oct 25, 2023

Behaviour

Expected vs. Actual

Expected:

Empty string environment variables are loaded/included when running tests using the extension.

Actual

Empty string environment variables lost completely.

Steps to reproduce:

echo 'export HELLO_WORLD_NOT_EMPTY="123"' >> ~/.zshrc
echo 'export HELLO_WORLD_EMPTY=""' >> ~/.zshrc
source ~/.zshrc

Run the following tests (defined in file test_os_environ.py) in vscode 'testing', configured with pytest,

import os

def test_empty_env_exist():
  assert "HELLO_WORLD_EMPTY" in os.environ.keys()

def test_non_empty_env_exist():
  assert "HELLO_WORLD_NOT_EMPTY" in os.environ.keys()

def test_empty_string_env():
  assert os.environ.get("HELLO_WORLD_EMPTY") == ""

def test_non_empty_string_env():
  assert os.environ.get("HELLO_WORLD_NOT_EMPTY") == "123"

tests with empty string environment fails

and run pytest via CLI, all tests passed

Recording 2023-10-26 at 00 37 31

Diagnostic data

  • Python version (& distribution if applicable, e.g. Anaconda): 3.10.5
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2023-10-26 00:24:59.184 [info] > ./.venv/bin/python -m pytest --rootdir . --override-ini junit_family=xunit1 --junit-xml=/var/folders/7c/l24wyqrs77d5y1d_fvqbvg8r0000gn/T/tmp-10329QUwFJk7Mn728.xml
2023-10-26 00:24:59.184 [info] cwd: .

User Settings


languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Oct 25, 2023
@eleanorjboyd
Copy link
Member

Hello! Can you try this on the testing rewrite and let me know if it still exists? To do so add this setting to your users settings.json "python.experiments.optInto": ["pythonTestAdapter"].

You can confirm you have the rewrite enabled by setting "python.analysis.logLevel": "Trace", in your user settings then check for Experiment 'pythonTestAdapter' is active in your python logs.

Let me know if the rewrite fixes your issue. Thanks!

@eleanorjboyd eleanorjboyd self-assigned this Oct 25, 2023
@eleanorjboyd eleanorjboyd added the info-needed Issue requires more information from poster label Oct 25, 2023
@eleanorjboyd
Copy link
Member

Another note, we do not run anything in a shell for testing so we will not pick up the .zshrc during running tests. Instead how you can do it is run the following as running code . from the terminal window with the environment variables will open the given folder with those environment variables as defined in the .zshrc.

echo 'export HELLO_WORLD_NOT_EMPTY="123"' >> ~/.zshrc
echo 'export HELLO_WORLD_EMPTY=""' >> ~/.zshrc
source ~/.zshrc
code .

@Zhong-z
Copy link
Author

Zhong-z commented Oct 25, 2023

Hi @eleanorjboyd, thanks for the responses!

then check for Experiment 'pythonTestAdapter' is active in your python logs.

confirming pythonTestAdapter is active.


we do not run anything in a shell for testing so we will not pick up the .zshrc during running tests.

🤔 I considered this as well, that's why I included test cases for the negative scenario HELLO_WORLD_NOT_EMPTY=123. As you can see from the GIF, somehow HELLO_WORLD_NOT_EMPTY is available (indicated by tests passing). but not HELLO_WORLD_EMPTY


Instead how you can do it is run the following as running code . from the terminal window with the environment variables will open

Yeah, tried code . as well. Same result.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 25, 2023
@eleanorjboyd
Copy link
Member

hm yes good point, @karrtikr or @karthiknadig thoughts on why one exists and the other doesn't and also why code . provides the same behavior?

Also @Zhong-z was there a reason you were using your .zshrc for these variables? You could also think about putting them in a .env file and then setting that path via "python.envFile": "${workspaceFolder}/.env" in settings

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Oct 25, 2023
@karrtikr
Copy link

That is strange indeed, maybe VS Code filters out the empty variables, logs should indicate which variables are included when launching tests.

@Zhong-z
Copy link
Author

Zhong-z commented Oct 25, 2023

The script/code snippet was simplified for reproducing. In my actual setup/use case, the ENV variables were setup in zshrc/shell is because they are shared between multiple repos/projects (different languages) locally, and they are subjective to change independently.

I could technically use a .env, but it is a pain to manage them separately per repo/copy. shell/.zhsrc is the the simplest way I could think of.

@github-actions github-actions bot removed the info-needed Issue requires more information from poster label Oct 25, 2023
@Zhong-z
Copy link
Author

Zhong-z commented Oct 25, 2023

That is strange indeed, maybe VS Code filters out the empty variables, logs should indicate which variables are included when launching tests.

Thanks @karrtikr . I couldn't see the empty string ENVs are included in the 'printEnvVariables.py` output at all, so I assume they are not included.

2023-10-26 09:01:07.678 [debug] Shell Exec: /Users/zz/.pyenv/versions/3.10.5/bin/python /Users/zz/.vscode/extensions/ms-python.python-2023.18.0/pythonFiles/printEnvVariables.py with options: {
    "shell": "/bin/zsh",
    "timeout": 30000,
    "maxBuffer": 1000000,
    "throwOnStdErr": false,
    "encoding": "utf8",
    "env": {
        .....
        "HELLO_WORLD_NOT_EMPTY": "123",

@karrtikr
Copy link

karrtikr commented Oct 25, 2023

@Zhong-z You could add "python.envFile" in User settings, so it applies to all projects. It could carry the global path to a .env file.

@Zhong-z
Copy link
Author

Zhong-z commented Oct 26, 2023

Thanks all. It appears that the issue is indeed related to VS Code in general and is not specific to the Python extension. 😄
microsoft/vscode#182560

@eleanorjboyd
Copy link
Member

Glad you were able to find the root there- sorry there is nothing we can do to help! Closing this issue as the issue on core will keep track of status and progress. Thanks!

@eleanorjboyd eleanorjboyd closed this as not planned Won't fix, can't repro, duplicate, stale Oct 26, 2023
@karrtikr
Copy link

Great job finding the upstream issue! FYI #22337 (comment) should still help your case.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-testing triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

3 participants