Skip to content

Commit

Permalink
Fix environment variables passing to tasks and debug configs (#646)
Browse files Browse the repository at this point in the history
* Fix environment variables passing to tasks and debug configs so that terminal selection and profiles work as expected

* make sure we parse correctly
  • Loading branch information
gcampbell-msft authored Oct 11, 2024
1 parent 0c90ab8 commit a7b6ae3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## 0.11

Improvements:

- More wisely pass the modified environment variables, if present, from the pre/post-configure scripts to tasks and debugging options. [#540](https://github.com/microsoft/vscode-makefile-tools/issues/540), [#493](https://github.com/microsoft/vscode-makefile-tools/issues/493), [#554](https://github.com/microsoft/vscode-makefile-tools/issues/554)

Bug Fixes:

- Ensure that we append line endings, which fixes a recent regression. [#641](https://github.com/microsoft/vscode-makefile-tools/issues/641)
Expand Down
8 changes: 7 additions & 1 deletion src/launch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,13 @@ export class Launcher implements vscode.Disposable {
request: "launch",
cwd: this.getLaunchTargetDirectory(),
args,
env: util.mergeEnvironment(process.env as util.EnvironmentVariables),
// Only pass a defined environment if there are modified environment variables.
env:
Object.keys(util.modifiedEnvironmentVariables).length > 0
? util.mergeEnvironment(
util.modifiedEnvironmentVariables as util.EnvironmentVariables
)
: undefined,
program: this.getLaunchTargetPath(),
MIMode: miMode,
miDebuggerPath: miDebuggerPath,
Expand Down
14 changes: 13 additions & 1 deletion src/make.ts
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,13 @@ export async function doBuildTarget(
}

let myTaskOptions: vscode.ShellExecutionOptions = {
env: util.mergeEnvironment(process.env as util.EnvironmentVariables),
// Only pass a defined environment if there are modified environment variables.
env:
Object.keys(util.modifiedEnvironmentVariables).length > 0
? util.mergeEnvironment(
util.modifiedEnvironmentVariables as util.EnvironmentVariables
)
: undefined,
cwd,
};

Expand Down Expand Up @@ -1090,6 +1096,12 @@ async function applyEnvironment(content: string | undefined): Promise<void> {
if (eqPos !== -1) {
let envVarName: string = line.substring(0, eqPos);
let envVarValue: string = line.substring(eqPos + 1, line.length);

// Only save to the modified environment values if it's different than the process.env.
if (!process.env[envVarName] || envVarValue !== process.env[envVarName]) {
util.modifiedEnvironmentVariables[envVarName] = envVarValue;
}

process.env[envVarName] = envVarValue;
}
});
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,8 @@ export function mergeEnvironment(
}, {});
}

export let modifiedEnvironmentVariables: EnvironmentVariables = {};

export interface SpawnProcessResult {
returnCode: number;
signal: string;
Expand Down Expand Up @@ -388,6 +390,10 @@ export function spawnChildProcess(
`integrated.automationShell.${shellPlatform}`
); // and replaced with automationProfile

if (typeof shellType === "object") {
shellType = shellType["path"];
}

// Final quoting decisions for process name and args before being executed.
let qProcessName: string = ensureQuoted
? quoteStringIfNeeded(processName)
Expand Down

0 comments on commit a7b6ae3

Please sign in to comment.