Skip to content

Commit e9146c7

Browse files
authored
fix(core): prevent args from being split by spaces when executing through nx wrapper (#33362)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> <!-- If this is a particularly complex change or feature addition, you can request a dedicated Nx release for this pull request branch. Mention someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they will confirm if the PR warrants its own release for testing purposes, and generate it for you if appropriate. --> ## Current Behavior Because `./nx` is passing args with `$@`, args are being split by spaces. Reference for this behaviour: https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html#Special-Parameters-1 This results in this set of process args when running `./nx start-ci-run --distribute-on="3 linux-medium-jvm"` ``` [ "<user path>/.nvm/versions/node/v22.19.0/bin/node", "<user path>/Projects/OTW/mm-test/.nx/nxw.js", "start-ci-run", "--distribute-on=3", "linux-medium-jvm", ] ``` ## Expected Behavior the arg with whitespace should not be split before being passed to nx: ``` [ "<user path>/.nvm/versions/node/v22.19.0/bin/node", "<user path>/.nx/nxw.js", "start-ci-run", "--distribute-on=3 linux-medium-jvm", ] ```
1 parent 62d0ad7 commit e9146c7

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

packages/nx/migrations.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,12 @@
135135
"version": "22.0.0-beta.2",
136136
"description": "Consolidates releaseTag* options into nested releaseTag object structure",
137137
"implementation": "./src/migrations/update-22-0-0/consolidate-release-tag-config"
138+
},
139+
"22-1-0-update-nx-wrapper": {
140+
"cli": "nx",
141+
"version": "22.1.0-beta.5",
142+
"description": "Updates the nx wrapper.",
143+
"implementation": "./src/migrations/update-22-1-0/update-nx-wrapper"
138144
}
139145
}
140146
}

packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const SHELL_SCRIPT_CONTENTS = [
4848
// Gets the path to the root of the project
4949
`path_to_root=$(dirname $BASH_SOURCE)`,
5050
// Executes the nx wrapper script
51-
`node ${path.posix.join('$path_to_root', nxWrapperPath(path.posix))} $@`,
51+
`node ${path.posix.join('$path_to_root', nxWrapperPath(path.posix))} "$@"`,
5252
].join('\n');
5353

5454
export function generateDotNxSetup(version?: string) {
@@ -104,6 +104,16 @@ export function getNxWrapperContents() {
104104
);
105105
}
106106

107+
// Gets the contents for the nx bash script
108+
export function getShellScriptContents() {
109+
return SHELL_SCRIPT_CONTENTS;
110+
}
111+
112+
// Gets the contents for the nx.bat batch script
113+
export function getBatchScriptContents() {
114+
return BATCH_SCRIPT_CONTENTS;
115+
}
116+
107117
// Remove any empty comments or comments that start with `//#: ` or eslint-disable comments.
108118
// This removes the sourceMapUrl since it is invalid, as well as any internal comments.
109119
export function sanitizeWrapperScript(input: string) {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import type { Tree } from '../../generators/tree';
2+
import { updateNxw } from '../../utils/update-nxw';
3+
4+
export default async function (tree: Tree) {
5+
updateNxw(tree);
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
11
import {
22
getNxWrapperContents,
33
nxWrapperPath,
4+
getShellScriptContents,
5+
getBatchScriptContents,
46
} from '../command-line/init/implementation/dot-nx/add-nx-scripts';
57
import type { Tree } from '../generators/tree';
68
import { normalizePath } from '../utils/path';
9+
import { constants as FsConstants } from 'fs';
710

811
export function updateNxw(tree: Tree) {
912
const wrapperPath = normalizePath(nxWrapperPath());
1013
if (tree.exists(wrapperPath)) {
1114
tree.write(wrapperPath, getNxWrapperContents());
1215
}
16+
17+
const bashScriptPath = 'nx';
18+
if (tree.exists(bashScriptPath) && tree.isFile(bashScriptPath)) {
19+
tree.write(bashScriptPath, getShellScriptContents(), {
20+
mode: FsConstants.S_IXUSR | FsConstants.S_IRUSR | FsConstants.S_IWUSR,
21+
});
22+
}
23+
24+
const batchScriptPath = 'nx.bat';
25+
if (tree.exists(batchScriptPath) && tree.isFile(batchScriptPath)) {
26+
tree.write(batchScriptPath, getBatchScriptContents());
27+
}
1328
}

0 commit comments

Comments
 (0)