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 environment variables passed to vs code are unset #174330

Closed
aidantwoods opened this issue Feb 14, 2023 · 11 comments · Fixed by #175682
Closed

Empty environment variables passed to vs code are unset #174330

aidantwoods opened this issue Feb 14, 2023 · 11 comments · Fixed by #175682
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. verified Verification succeeded
Milestone

Comments

@aidantwoods
Copy link

aidantwoods commented Feb 14, 2023

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.75.1 (Universal)
  • OS Version: macOS 13.2

When VS Code is launched with a set but empty env var, it will drop it and treat it as unset. This breaks workflows which check for the presence of env vars (which may be empty in some cases) when running VS Code integrations, e.g. running tests, running the debugger.

Steps to Reproduce:

  1. Launch vscode using command with FOO="" BAR="test" code .
  2. In a terminal within vscode run: [[ -v FOO ]] && echo "foo set", and then [[ -v BAR ]] && echo "bar set"
  3. Observe that FOO is missing, BAR is set

Compare with running in a terminal manually, with an empty FOO:

$ export FOO=""
$ [[ -v FOO ]] && echo "foo set"
foo set

Of the available historical downloads, I found that vscode 1.73.1 still replicates the above, but does seem to correctly pass the empty env vars down to other processes (e.g. the debugger).

@roblourens
Copy link
Member

I'm seeing the variable make it to the debugger, eg here in the debug console, it's in process.env
image

But I'm not seeing it in the terminal. @Tyriar @meganrogge does one of the terminal processes clear out variables with empty values?

@roblourens roblourens added the info-needed Issue requires more information from poster label Feb 15, 2023
@meganrogge
Copy link
Contributor

yep, looks like that would happen here

function mergeNonNullKeys(env: IProcessEnvironment, other: ITerminalEnvironment | undefined) {
if (!other) {
return;
}
for (const key of Object.keys(other)) {
const value = other[key];
if (value) {
env[key] = value;
}
}
}

@roblourens roblourens assigned Tyriar and meganrogge and unassigned roblourens Feb 16, 2023
@meganrogge meganrogge added bug Issue identified by VS Code Team member as probable bug terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. and removed info-needed Issue requires more information from poster labels Feb 16, 2023
@aidantwoods
Copy link
Author

To give some more context on how I was experiencing the issue primarily: it was when running tests or the debugger (with the Go extension installed). Is it possible a similar (or maybe the same) behaviour to is affecting how env vars get passed down to extensions (as well as the terminal process)?

@roblourens
Copy link
Member

roblourens commented Feb 17, 2023

Maybe if the debuggee is launched from a terminal? Or something particular to that debug extension. It worked for me when debugging node.

@aidantwoods
Copy link
Author

aidantwoods commented Feb 17, 2023

I did think it could be one of those at first, but I can reliably repro the issue in latest VS Code, whilst 1.73.1 works fine with the same setup (although still reproduces the integrated terminal bug).

When I get a moment, I'll put together a minimal repro for the extension issue just so it is testable.

Tyriar added a commit that referenced this issue Feb 28, 2023
@Tyriar Tyriar added this to the March 2023 milestone Feb 28, 2023
@vscodenpa vscodenpa added unreleased Patch has not yet been released in VS Code Insiders insiders-released Patch has been released in VS Code Insiders and removed unreleased Patch has not yet been released in VS Code Insiders labels Feb 28, 2023
@fedorareis
Copy link

I just tested out the insider build with this fix and it looks like the terminal itself now has the environment variables set correctly. But other areas, like the test runners, don't. For example I tried running a golang test using the play button in the gutter and the tests still failed with an error about an environment variable not being set. As @aidantwoods mentioned, this worked fine in 1.73.1.

@Tyriar
Copy link
Member

Tyriar commented Mar 7, 2023

@fedorareis thanks, maybe we have a similar check for terminals created via extensions. Will have a look

@Tyriar Tyriar reopened this Mar 7, 2023
@vscodenpa vscodenpa removed the insiders-released Patch has been released in VS Code Insiders label Mar 7, 2023
@Tyriar
Copy link
Member

Tyriar commented Mar 20, 2023

This is working fine for me. To verify:

  • Test to ensure a variable in terminal.integrated.env.windows/linux with a value of "" comes through to the actual terminal environment
  • Do the same when the environment change is applied via the context.environmentVariableCollection.replace extension API

@Tyriar Tyriar closed this as completed Mar 20, 2023
@connor4312 connor4312 added the verified Verification succeeded label Mar 22, 2023
@fedorareis
Copy link

fedorareis commented Apr 2, 2023

@Tyriar I'm still seeing the issue in the current insider build. I've created a minimal reproduction of the issue.

  1. Create a new go project
  2. Create a new go file ex.go with the following content
package main

import (
	"errors"
	"os"
)

func getVars(env string) (string, error) {
	val, set := os.LookupEnv(env)
	if !set {
		return "", errors.New("can't find variable:" + env)
	}
	return val, nil
}
  1. Create another new go file ex_test.go with the following content
package main

import "testing"

func TestGetVars(t *testing.T) {
	env := "BAR"
	_, err := getVars(env)
	if err != nil {
		t.Fatalf("%s not set", env)
	}

	env = "FOO"
	_, err = getVars(env)
	if err != nil {
		t.Fatalf("%s not set", env)
	}

}
  1. open the project using FOO="" BAR="test" code-insiders
  2. navigate to ex_test.go and run the test
  3. the test will fail on line 15 with the error "FOO not set"

As @aidantwoods mentioned above this works properly in version 1.73.1 and the test passes

@fedorareis
Copy link

@Tyriar should I file a new issue for the scenario I described above or will this issue be reopened to look into it?

@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2023
@Tyriar
Copy link
Member

Tyriar commented May 24, 2023

@fedorareis I think it's tracked in #182560

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue identified by VS Code Team member as probable bug terminal-process Problems launching processes, managing ptys, exiting, process leaks, etc. verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants