Skip to content

Commit

Permalink
determine shell for .cmd, .bat, .ps1 (#4044)
Browse files Browse the repository at this point in the history
* determine shell for .cmd, .bat, .ps1

* update changelog

* only apply fix on windows, as necessary

* undefined -> false

* use concrete if statement

* update changelog and change if statement order

* nit: remove extra space
  • Loading branch information
gcampbell-msft authored Oct 10, 2024
1 parent 1818a7e commit 0786aa8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ Features:

- Add support for Presets v9, which enables more macro expansion for the `include` field. [#3946](https://github.com/microsoft/vscode-cmake-tools/issues/3946)

Improvements:

- Ensure that any uses of `proc.spawn` work, especially for .bat and .cmd files, due to VS Code updating to Node 20. [#4037](https://github.com/microsoft/vscode-cmake-tools/issues/4037)

Bug Fixes:

- Fix our setting of `isUserPreset` for presets, only set it to `true` if it's defined in a user presets file. [#4059](https://github.com/microsoft/vscode-cmake-tools/issues/4059)
Expand Down
19 changes: 18 additions & 1 deletion src/proc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface ExecutionResult {

export interface ExecutionOptions {
environment?: Environment;
shell?: boolean;
shell?: boolean | string;
silent?: boolean;
cwd?: string;
encoding?: BufferEncoding;
Expand All @@ -113,6 +113,18 @@ export function buildCmdStr(command: string, args?: string[]): string {
return cmdarr.map(a => /[ \n\r\f;\t]/.test(a) ? `"${a}"` : a).join(' ');
}

export function determineShell(command: string): string | boolean {
if (command.endsWith('.cmd') || command.endsWith('.bat')) {
return 'cmd';
}

if (command.endsWith('.ps1')) {
return 'powershell';
}

return false;
}

/**
* Execute a command and return the result
* @param command The binary to execute
Expand Down Expand Up @@ -146,6 +158,11 @@ export function execute(command: string, args?: string[], outputConsumer?: Outpu
log.debug(localize('execution.environment', ' with environment: {0}', JSON.stringify(final_env)));
}
}

if (process.platform === "win32" && options.shell === undefined) {
options.shell = determineShell(command);
}

const spawn_opts: proc.SpawnOptions = {
env: final_env,
shell: !!options.shell
Expand Down

0 comments on commit 0786aa8

Please sign in to comment.