Skip to content

Commit 28c8921

Browse files
rally25rsarcanis
authored andcommitted
fix(config): Don't pass .yarnrc generated flags to workspace child command (#5534)
* fix(config): Don't pass .yarnrc generated flags to workspace child command Previously, if flags were defined in .yarnrc then they would be insterted into the rawArgs array of commander.js. The yarn workspace command would then attempt to pass these flags to the child process that it spawns. They were being passed out-of-order and causing the child command to fail to properly parse it's command line args. This change prevents those flags from being passed at all. The yarn child process will read .yarnrc and inject the flags into it's own argument array anyway, so passing them was just duplicating the flags. #5496 * add test for 5534
1 parent f80cbb5 commit 28c8921

File tree

8 files changed

+49
-11
lines changed

8 files changed

+49
-11
lines changed

__tests__/commands/workspace.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ async function runWorkspace(
3939
}
4040

4141
// The unit tests don't use commander.js for argument parsing.
42-
// `rawArgs` is normally passed by commander.js so we just simulate it in the tests.
42+
// `originalArgs` is normally passed by index.js so we just simulate it in the tests.
4343

4444
test('workspace run command', (): Promise<void> => {
45-
const rawArgs = ['/path/to/node', '/path/to/yarn', 'workspace', 'workspace-1', 'run', 'script'];
46-
return runWorkspace({rawArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
45+
const originalArgs = ['workspace-1', 'run', 'script'];
46+
return runWorkspace({originalArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
4747
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'run', 'script'], {
4848
stdio: 'inherit',
4949
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
@@ -52,8 +52,8 @@ test('workspace run command', (): Promise<void> => {
5252
});
5353

5454
test('workspace run command forwards raw arguments', (): Promise<void> => {
55-
const rawArgs = ['/path/to/node', '/path/to/yarn', 'workspace', 'workspace-1', 'run', 'script', 'arg1', '--flag1'];
56-
return runWorkspace({rawArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
55+
const originalArgs = ['workspace-1', 'run', 'script', 'arg1', '--flag1'];
56+
return runWorkspace({originalArgs}, ['workspace-1', 'run', 'script'], 'run-basic', config => {
5757
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'run', 'script', 'arg1', '--flag1'], {
5858
stdio: 'inherit',
5959
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--*.emoji false
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "my-project",
3+
"private": true,
4+
"workspaces": [
5+
"packages/*"
6+
]
7+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "workspace-1",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"prescript": "echo workspace-1 prescript",
6+
"script": "echo workspace-1 script",
7+
"postscript": "echo workspace-1 postscript",
8+
"check": "echo $1"
9+
}
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "workspace-2",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"prescript": "echo workspace-2 prescript",
6+
"script": "echo workspace-2 script",
7+
"postscript": "echo workspace-2 postscript"
8+
},
9+
"dependencies": {
10+
"workspace-1": "1.0.0"
11+
}
12+
}

__tests__/index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,13 @@ test.concurrent('should run help for camelised command', async () => {
316316
expect(lastLines[2]).toMatch(/yarn generate-lock-entry --resolved local-file.tgz#hash/);
317317
expect(lastLines[3]).toMatch(/Visit https:\/\/yarnpkg.com\/en\/docs\/cli\/generate-lock-entry/);
318318
});
319+
320+
// regression test for #5496
321+
// this fixture has a .yarnrc in it that sets the `--emoji` flag.
322+
// we test to make sure that flag is not passed down to the workspace command,
323+
// but actual flags on the command line are passed.
324+
test.concurrent('should not pass yarnrc flags to workspace command', async () => {
325+
const stdout = await execCommand('workspace', ['workspace-1', 'run', 'check', '--x'], 'run-workspace', true);
326+
const params = stdout.find(x => x && x.indexOf('--x') >= 0);
327+
expect(params).not.toMatch(/emoji/);
328+
});

src/cli/commands/workspace.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,19 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
2121
throw new MessageError(reporter.lang('workspaceRootNotFound', config.cwd));
2222
}
2323

24-
if (args.length < 1) {
24+
if (flags.originalArgs < 1) {
2525
throw new MessageError(reporter.lang('workspaceMissingWorkspace'));
2626
}
2727

28-
if (args.length < 2) {
28+
if (flags.originalArgs < 2) {
2929
throw new MessageError(reporter.lang('workspaceMissingCommand'));
3030
}
3131

3232
const manifest = await config.findManifest(workspaceRootFolder, false);
3333
invariant(manifest && manifest.workspaces, 'We must find a manifest with a "workspaces" property');
3434

3535
const workspaces = await config.resolveWorkspaces(workspaceRootFolder, manifest);
36-
37-
const [workspaceName] = args;
38-
// rawArgs contains: [nodePath, yarnPath, 'workspace', workspaceName, ...]
39-
const [, , , , ...rest] = flags.rawArgs || [];
36+
const [workspaceName, ...rest] = flags.originalArgs || [];
4037

4138
if (!Object.prototype.hasOwnProperty.call(workspaces, workspaceName)) {
4239
throw new MessageError(reporter.lang('workspaceUnknownWorkspace', workspaceName));

src/cli/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ export function main({
180180
}
181181
}
182182

183+
commander.originalArgs = args;
183184
args = [...preCommandArgs, ...args];
184185

185186
command.setFlags(commander);

0 commit comments

Comments
 (0)