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(core): add quotes around string to command #23056

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions e2e/nx/src/run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('Nx Running Tests', () => {
'--a.b 1',
'-- a b c --a --a.b=1',
'--ignored -- a b c --a --a.b=1',
'--test="hello world" "abc def"',
])('should forward %s properly', (args) => {
const output = runCLI(`echo ${proj} ${args}`);
expect(output).toContain(`ECHO: ${args.replace(/^.*-- /, '')}`);
Expand Down
4 changes: 0 additions & 4 deletions packages/nx/bin/init-local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ export function rewriteTargetsAndProjects(args: string[]) {
return newArgs;
}

function wrapIntoQuotesIfNeeded(arg: string) {
return arg.indexOf(':') > -1 ? `"${arg}"` : arg;
}

function isKnownCommand(command: string) {
const commands = [
...Object.keys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,56 @@ describe('Run Commands', () => {
)
).toEqual('echo "hello world"');
});

it('should interpolate provided values with spaces', () => {
expect(
interpolateArgsIntoCommand(
'echo',
{
unknownOptions: { hello: 'test 123' },
parsedArgs: { hello: 'test 123' },
} as any,
true
)
).toEqual('echo --hello="test 123"'); // should wrap in quotes

expect(
interpolateArgsIntoCommand(
'echo',
{
unknownOptions: { hello: '"test 123"' },
parsedArgs: { hello: '"test 123"' },
} as any,
true
)
).toEqual('echo --hello="test 123"'); // should leave double quotes

expect(
interpolateArgsIntoCommand(
'echo',
{
unknownOptions: { hello: "'test 123'" },
parsedArgs: { hello: "'test 123'" },
} as any,
true
)
).toEqual("echo --hello='test 123'"); // should leave single quote

expect(
interpolateArgsIntoCommand(
'echo',
{
__unparsed__: [
'--hello=test 123',
'hello world',
'"random config"',
'456',
],
} as any,
true
)
).toEqual(`echo --hello="test 123" "hello world" "random config" 456`); // should wrap aroound __unparsed__ args with key value
});
});

describe('--color', () => {
Expand Down
23 changes: 22 additions & 1 deletion packages/nx/src/executors/run-commands/run-commands.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,7 @@ export function interpolateArgsIntoCommand(
opts.parsedArgs[k] === opts.unknownOptions[k]
)
.map((k) => `--${k}=${opts.unknownOptions[k]}`)
.map((arg) => wrapArgIntoQuotesIfNeeded(arg))
.join(' ');
}
if (opts.args) {
Expand All @@ -505,7 +506,9 @@ export function interpolateArgsIntoCommand(
opts.unparsedCommandArgs
);
if (filterdParsedOptions.length > 0) {
args += ` ${filterdParsedOptions.join(' ')}`;
args += ` ${filterdParsedOptions
.map((arg) => wrapArgIntoQuotesIfNeeded(arg))
.join(' ')}`;
}
}
return `${command}${args}`;
Expand Down Expand Up @@ -627,3 +630,21 @@ function registerProcessListener() {
// will store results to the cache and will terminate this process
});
}

function wrapArgIntoQuotesIfNeeded(arg: string): string {
if (arg.includes('=')) {
const [key, value] = arg.split('=');
if (
key.startsWith('--') &&
value.includes(' ') &&
!(value[0] === "'" || value[0] === '"')
) {
return `${key}="${value}"`;
}
return arg;
} else if (arg.includes(' ') && !(arg[0] === "'" || arg[0] === '"')) {
return `"${arg}"`;
} else {
return arg;
}
}