Skip to content

Commit

Permalink
fix(run): Fix script running in paths with special chars (#5484)
Browse files Browse the repository at this point in the history
**Summary**

Fixes #5481.

With #5133, we have stopped passing the command/script name to the shell properly. This patch makes
sure the proper escaping is done when not using the `script-shell` option.

**Test plan**

Added a new integration test.
  • Loading branch information
BYK authored Mar 9, 2018
1 parent f0acf60 commit e123a58
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
21 changes: 20 additions & 1 deletion __tests__/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,12 +392,31 @@ test('yarn run <script> <strings that need escaping>', async () => {

const options = {cwd, env: {YARN_SILENT: 1}};

const trickyStrings = ['$PWD', '%CD%', '^', '!', '\\', '>', '<', '|', '&', "'", '"', '`', ' '];
const trickyStrings = ['$PWD', '%CD%', '^', '!', '\\', '>', '<', '|', '&', "'", '"', '`', ' ', '(', ')'];
const [stdout] = await runYarn(['stringify', ...trickyStrings], options);

expect(stdout.toString().trim()).toEqual(JSON.stringify(trickyStrings));
});

test('yarn run in path need escaping', async () => {
const cwd = await makeTemp('special (chars)');

await fs.writeFile(path.join(cwd, 'package.json'), '{}');
const binDir = path.join(cwd, 'node_modules', '.bin');
await fs.mkdirp(binDir);
const executablePath = path.join(binDir, 'yolo');
await fs.writeFile(executablePath, 'echo yolo');
await fs.chmod(executablePath, 0o755);
// For Windows
await fs.writeFile(`${executablePath}.cmd`, '@ECHO off\necho yolo');

const options = {cwd, env: {YARN_SILENT: 1}};

const [stdout] = await runYarn(['yolo'], options);

expect(stdout.toString().trim()).toEqual('yolo');
});

test('cache folder fallback', async () => {
const cwd = await makeTemp();
const cacheFolder = path.join(cwd, '.cache');
Expand Down
17 changes: 4 additions & 13 deletions src/util/execute-lifecycle-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,7 @@ export async function executeLifecycleScript(

await checkForGypIfNeeded(config, cmd, env[constants.ENV_PATH_KEY].split(path.delimiter));

// get shell
let sh = 'sh';
let shFlag = '-c';

let windowsVerbatimArguments = undefined;

if (customShell) {
sh = customShell;
} else if (process.platform === 'win32') {
sh = process.env.comspec || 'cmd';
shFlag = '/d /s /c';
windowsVerbatimArguments = true;
if (process.platform === 'win32' && (!customShell || customShell === 'cmd')) {
// handle windows run scripts starting with a relative path
cmd = fixCmdWinSlashes(cmd);
}
Expand All @@ -219,7 +208,9 @@ export async function executeLifecycleScript(
}
};
}
const stdout = await child.spawn(sh, [shFlag, cmd], {cwd, env, stdio, windowsVerbatimArguments}, updateProgress);
const stdout = customShell
? await child.spawn(customShell, [cmd], {cwd, env, stdio, windowsVerbatimArguments: true}, updateProgress)
: await child.spawn(cmd, [], {cwd, env, stdio, shell: true}, updateProgress);

return {cwd, command: cmd, stdout};
}
Expand Down

0 comments on commit e123a58

Please sign in to comment.