Skip to content

Commit a251e26

Browse files
authored
Launch: env takes priority over envFile (#5537)
This PR changes the way debug/no-debug launch works with the C# extension so that environment variables added via `env` will take precedence over environment variables added via `envFile`. This resolves #5413
1 parent a60f494 commit a251e26

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

src/coreclr-debug/ParsedEnvironmentFile.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,19 @@ export class ParsedEnvironmentFile {
2626
}
2727

2828
let parseErrors: string[] = [];
29-
let env = initialEnv ?? {};
29+
let safeInitialEnv = initialEnv ?? {};
30+
let env = {...safeInitialEnv};
3031

3132
content.split("\n").forEach(line => {
3233
// Split the line between key and value
3334
const match = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
3435

3536
if (match !== null) {
3637
const key = match[1];
38+
if (safeInitialEnv[key] !== undefined) {
39+
return;
40+
}
41+
3742
let value = match[2] ?? "";
3843
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
3944
value = value.replace(/\\n/gm, "\n");

test/unitTests/ParsedEnvironmentFile.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,35 @@ MyName2=Value2
4646
result.Env["MyName2"].should.equal("Value2");
4747
});
4848

49-
test("Update variable", () => {
49+
test("Not override variables", () => {
5050
const content = `
51-
MyName1=Value1
51+
CommonKey=NewValue
5252
MyName2=Value2
5353
5454
`;
5555
const initialEnv = {
56-
"MyName1": "Value7",
56+
"CommonKey": "InitialValue",
5757
"ThisShouldNotChange": "StillHere"
5858
};
5959
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", initialEnv);
6060

6161
expect(result.Warning).to.be.undefined;
62-
result.Env["MyName1"].should.equal("Value1");
62+
result.Env["CommonKey"].should.equal("InitialValue");
6363
result.Env["MyName2"].should.equal("Value2");
6464
result.Env["ThisShouldNotChange"].should.equal("StillHere");
6565
});
6666

67+
test("Take last value", () => {
68+
const content = `
69+
Key=FirstValue
70+
Key=SecondValue
71+
`;
72+
const result = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", undefined);
73+
74+
expect(result.Warning).to.be.undefined;
75+
result.Env["Key"].should.equal("SecondValue");
76+
});
77+
6778
test("Handle comments", () => {
6879
const content = `# This is an environment file
6980
MyName1=Value1

0 commit comments

Comments
 (0)