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

Fix PATH on Mac systems #2619

Merged
merged 5 commits into from
Jan 20, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/utils/spawnAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import * as cp from 'child_process';
import { CancellationToken, Disposable } from 'vscode';
import { UserCancelledError } from 'vscode-azureextensionui';
import { ext } from '../extensionVariables';
import { localize } from '../localize';
import { isMac } from './osUtils';

const DEFAULT_BUFFER_SIZE = 10 * 1024; // The default Node.js `exec` buffer size is 1 MB, our actual usage is far less

Expand All @@ -33,6 +35,8 @@ export async function spawnAsync(
options = options || {};
options.shell = true;

fixPathForMacIfNeeded(options);

const subprocess = cp.spawn(command, options);

subprocess.on('error', (err) => {
Expand Down Expand Up @@ -240,3 +244,26 @@ export function bufferToString(buffer: Buffer): string {
// eslint-disable-next-line no-control-regex
return buffer.toString().replace(/[\x00-\x09\x0B-\x0C\x0E-\x1F]|\r?\n$/g, '');
}

function fixPathForMacIfNeeded(options: cp.SpawnOptions): void {
if (!isMac()) {
// Do nothing: not Mac
return;
}

// Looks for `/usr/local/bin` in the PATH.
// Must be whole, i.e. the left side must be the beginning of the string or :, and the right side must be the end of the string or :
// Case-insensitive, because Mac is
if (/(?<=^|:)\/usr\/local\/bin(?=$|:)/i.test(options?.env?.PATH)) {
// Do nothing: PATH already contains `/usr/local/bin`
return;
}

options = options ?? {};
options.env = options.env ?? process.env;
bwateratmsft marked this conversation as resolved.
Show resolved Hide resolved

ext.outputChannel.appendLine(localize('vscode-docker.utils.spawn.fixedPath', 'WARNING: Adding \'/usr/local/bin\' to the PATH because it is missing.'));

// Put `/usr/local/bin` on the PATH at the end
options.env.PATH = `${options.env.PATH}:/usr/local/bin`;
}