Skip to content

Commit f1a8a4d

Browse files
sparrowtarcanis
andauthored
Yarn config respect --no-defaults with --json (fixes #6341) (#6635)
## What's the problem this PR addresses? As documented here https://yarnpkg.com/cli/config you would expect `--no-defaults` to omit defaults even when `--json` is also specified. This PR resolves #6341 ## How did you fix it? By respecting `noDefaults` in the `if (this.json) {` branch as well as the `else` ## Checklist <!--- Don't worry if you miss something, chores are automatically tested. --> <!--- This checklist exists to help you remember doing the chores when you submit a PR. --> <!--- Put an `x` in all the boxes that apply. --> - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). <!-- See https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released for more details. --> <!-- Check with `yarn version check` and fix with `yarn version check -i` --> - [ ] I have set the packages that need to be released for my changes to be effective. <!-- The "Testing chores" workflow validates that your PR follows our guidelines. --> <!-- If it doesn't pass, click on it to see details as to what your PR might be missing. --> - [ ] I will check that all automated PR checks pass before the PR gets reviewed. --------- Co-authored-by: Maël Nison <[email protected]>
1 parent 1ef45ae commit f1a8a4d

File tree

4 files changed

+173
-43
lines changed

4 files changed

+173
-43
lines changed

.yarn/versions/c7de517e.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-essentials": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-init"
10+
- "@yarnpkg/plugin-interactive-tools"
11+
- "@yarnpkg/plugin-nm"
12+
- "@yarnpkg/plugin-npm-cli"
13+
- "@yarnpkg/plugin-pack"
14+
- "@yarnpkg/plugin-patch"
15+
- "@yarnpkg/plugin-pnp"
16+
- "@yarnpkg/plugin-pnpm"
17+
- "@yarnpkg/plugin-stage"
18+
- "@yarnpkg/plugin-typescript"
19+
- "@yarnpkg/plugin-version"
20+
- "@yarnpkg/plugin-workspace-tools"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"

packages/acceptance-tests/pkg-tests-specs/sources/commands/__snapshots__/config.test.ts.snap

+139
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Commands config test (folder with rcfile / as json no defaults) 1`] = `
4+
{
5+
"code": 0,
6+
"stderr": "",
7+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
8+
",
9+
}
10+
`;
11+
312
exports[`Commands config test (folder with rcfile / as json) 1`] = `
413
{
514
"code": 0,
@@ -11,6 +20,18 @@ exports[`Commands config test (folder with rcfile / as json) 1`] = `
1120
}
1221
`;
1322
23+
exports[`Commands config test (folder with rcfile / no defaults) 1`] = `
24+
{
25+
"code": 0,
26+
"stderr": "",
27+
"stdout": "└─ initScope
28+
- ├─ Description: Scope used when creating packages via the init command
29+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
30+
- └─ Value: 'my-test'
31+
",
32+
}
33+
`;
34+
1435
exports[`Commands config test (folder with rcfile / without flags) 1`] = `
1536
{
1637
"code": 0,
@@ -33,6 +54,16 @@ exports[`Commands config test (folder with rcfile / without flags) 1`] = `
3354
}
3455
`;
3556
57+
exports[`Commands config test (folder with rcfile and rc in ancestor parent / as json no defaults) 1`] = `
58+
{
59+
"code": 0,
60+
"stderr": "",
61+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
62+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
63+
",
64+
}
65+
`;
66+
3667
exports[`Commands config test (folder with rcfile and rc in ancestor parent / as json) 1`] = `
3768
{
3869
"code": 0,
@@ -44,6 +75,23 @@ exports[`Commands config test (folder with rcfile and rc in ancestor parent / as
4475
}
4576
`;
4677
78+
exports[`Commands config test (folder with rcfile and rc in ancestor parent / no defaults) 1`] = `
79+
{
80+
"code": 0,
81+
"stderr": "",
82+
"stdout": "├─ initScope
83+
- ├─ Description: Scope used when creating packages via the init command
84+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
85+
- └─ Value: 'my-test'
86+
87+
└─ lastUpdateCheck
88+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
89+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
90+
- └─ Value: '1555784893958'
91+
",
92+
}
93+
`;
94+
4795
exports[`Commands config test (folder with rcfile and rc in ancestor parent / without flags) 1`] = `
4896
{
4997
"code": 0,
@@ -66,6 +114,16 @@ exports[`Commands config test (folder with rcfile and rc in ancestor parent / wi
66114
}
67115
`;
68116
117+
exports[`Commands config test (folder with rcfile and rc in home folder / as json no defaults) 1`] = `
118+
{
119+
"code": 0,
120+
"stderr": "",
121+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
122+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
123+
",
124+
}
125+
`;
126+
69127
exports[`Commands config test (folder with rcfile and rc in home folder / as json) 1`] = `
70128
{
71129
"code": 0,
@@ -77,6 +135,23 @@ exports[`Commands config test (folder with rcfile and rc in home folder / as jso
77135
}
78136
`;
79137
138+
exports[`Commands config test (folder with rcfile and rc in home folder / no defaults) 1`] = `
139+
{
140+
"code": 0,
141+
"stderr": "",
142+
"stdout": "├─ initScope
143+
- ├─ Description: Scope used when creating packages via the init command
144+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
145+
- └─ Value: 'my-test'
146+
147+
└─ lastUpdateCheck
148+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
149+
- ├─ Source: WORKSPACE_ROOT/.yarnrc.yml
150+
- └─ Value: '1555784893958'
151+
",
152+
}
153+
`;
154+
80155
exports[`Commands config test (folder with rcfile and rc in home folder / without flags) 1`] = `
81156
{
82157
"code": 0,
@@ -99,6 +174,16 @@ exports[`Commands config test (folder with rcfile and rc in home folder / withou
99174
}
100175
`;
101176
177+
exports[`Commands config test (folder with rcfile and rc in parent / as json no defaults) 1`] = `
178+
{
179+
"code": 0,
180+
"stderr": "",
181+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
182+
{"key":"lastUpdateCheck","effective":"1555784893958","source":"WORKSPACE_ROOT/subfolder/.yarnrc.yml","description":"Last timestamp we checked whether new Yarn versions were available","type":"STRING","default":null}
183+
",
184+
}
185+
`;
186+
102187
exports[`Commands config test (folder with rcfile and rc in parent / as json) 1`] = `
103188
{
104189
"code": 0,
@@ -110,6 +195,23 @@ exports[`Commands config test (folder with rcfile and rc in parent / as json) 1`
110195
}
111196
`;
112197
198+
exports[`Commands config test (folder with rcfile and rc in parent / no defaults) 1`] = `
199+
{
200+
"code": 0,
201+
"stderr": "",
202+
"stdout": "├─ initScope
203+
- ├─ Description: Scope used when creating packages via the init command
204+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
205+
- └─ Value: 'my-test'
206+
207+
└─ lastUpdateCheck
208+
- ├─ Description: Last timestamp we checked whether new Yarn versions were available
209+
- ├─ Source: WORKSPACE_ROOT/subfolder/.yarnrc.yml
210+
- └─ Value: '1555784893958'
211+
",
212+
}
213+
`;
214+
113215
exports[`Commands config test (folder with rcfile and rc in parent / without flags) 1`] = `
114216
{
115217
"code": 0,
@@ -132,6 +234,15 @@ exports[`Commands config test (folder with rcfile and rc in parent / without fla
132234
}
133235
`;
134236
237+
exports[`Commands config test (folder with rcfile without trailing newline / as json no defaults) 1`] = `
238+
{
239+
"code": 0,
240+
"stderr": "",
241+
"stdout": "{"key":"initScope","effective":"my-test","source":"WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml","description":"Scope used when creating packages via the init command","type":"STRING","default":null}
242+
",
243+
}
244+
`;
245+
135246
exports[`Commands config test (folder with rcfile without trailing newline / as json) 1`] = `
136247
{
137248
"code": 0,
@@ -143,6 +254,18 @@ exports[`Commands config test (folder with rcfile without trailing newline / as
143254
}
144255
`;
145256
257+
exports[`Commands config test (folder with rcfile without trailing newline / no defaults) 1`] = `
258+
{
259+
"code": 0,
260+
"stderr": "",
261+
"stdout": "└─ initScope
262+
- ├─ Description: Scope used when creating packages via the init command
263+
- ├─ Source: WORKSPACE_ROOT/subfolder/subfolder/.yarnrc.yml
264+
- └─ Value: 'my-test'
265+
",
266+
}
267+
`;
268+
146269
exports[`Commands config test (folder with rcfile without trailing newline / without flags) 1`] = `
147270
{
148271
"code": 0,
@@ -165,6 +288,14 @@ exports[`Commands config test (folder with rcfile without trailing newline / wit
165288
}
166289
`;
167290
291+
exports[`Commands config test (folder without rcfile in ancestry / as json no defaults) 1`] = `
292+
{
293+
"code": 0,
294+
"stderr": "",
295+
"stdout": "",
296+
}
297+
`;
298+
168299
exports[`Commands config test (folder without rcfile in ancestry / as json) 1`] = `
169300
{
170301
"code": 0,
@@ -176,6 +307,14 @@ exports[`Commands config test (folder without rcfile in ancestry / as json) 1`]
176307
}
177308
`;
178309
310+
exports[`Commands config test (folder without rcfile in ancestry / no defaults) 1`] = `
311+
{
312+
"code": 0,
313+
"stderr": "",
314+
"stdout": "",
315+
}
316+
`;
317+
179318
exports[`Commands config test (folder without rcfile in ancestry / without flags) 1`] = `
180319
{
181320
"code": 0,

packages/acceptance-tests/pkg-tests-specs/sources/commands/config.test.ts

+8-43
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {PortablePath, npath, ppath, xfs} from '@yarnpkg/fslib';
22

33
const RC_FILENAME = `.yarnrc.yml`;
44
const SUBFOLDER = `subfolder`;
5-
const FAKE_REGISTRY_URL = `http://yarn.test.registry`;
65
const FAKE_LOCAL_APP_DATA = `LOCAL_APP_DATA`;
76
const FAKE_WORKSPACE_ROOT = `WORKSPACE_ROOT`;
87
const FAKE_HOME = `HOME`;
@@ -40,7 +39,7 @@ const environments: Record<string, (opts: {
4039
},
4140
};
4241

43-
function cleanupPlainOutput(output: string, path: PortablePath, homePath: PortablePath) {
42+
function cleanupOutput(output: string, path: PortablePath, homePath: PortablePath) {
4443
// Replace multiple consecutive spaces with one space.
4544
// The output of the config command is aligned according to the longest value, which probably
4645
// contains `path`. In other words, the formatting depends on the length of `path`.
@@ -65,51 +64,17 @@ function cleanupPlainOutput(output: string, path: PortablePath, homePath: Portab
6564
return output;
6665
}
6766

68-
function cleanupJsonOutput(output: string, path: PortablePath, homePath: PortablePath) {
69-
let outputObject;
70-
try {
71-
outputObject = JSON.parse(output);
72-
} catch {
73-
return cleanupPlainOutput(output, path, homePath);
74-
}
75-
76-
// the default globalFolder contains the user's home folder, override that value
77-
outputObject.globalFolder.default = `DEFAULT_GLOBAL_FOLDER`;
78-
79-
// replace the generated registry server URL with a constant
80-
outputObject.npmRegistryServer.effective = FAKE_REGISTRY_URL;
81-
82-
const pathN = npath.fromPortablePath(path);
83-
const homePathN = npath.fromPortablePath(homePath);
84-
85-
const cleanPath = (input: string) => input
86-
.replaceAll(pathN, FAKE_WORKSPACE_ROOT)
87-
.replaceAll(homePathN, FAKE_HOME);
88-
89-
for (const setting of Object.values<any>(outputObject)) {
90-
if (typeof setting.source === `string`)
91-
setting.source = cleanPath(setting.source);
92-
93-
if (typeof setting.default === `string`)
94-
setting.default = cleanPath(setting.default);
95-
96-
if (typeof setting.effective === `string`) {
97-
setting.effective = cleanPath(setting.effective);
98-
}
99-
}
100-
101-
return JSON.stringify(outputObject);
102-
}
103-
10467
const options = {
105-
[`without flags`]: {cleanupStdout: cleanupPlainOutput, flags: []},
106-
[`as json`]: {cleanupStdout: cleanupJsonOutput, flags: [`--json`]},
68+
[`without flags`]: {flags: []},
69+
[`as json`]: {flags: [`--json`]},
70+
[`no defaults`]: {flags: [`--no-defaults`]},
71+
[`as json no defaults`]: {flags: [`--json`, `--no-defaults`]},
10772
};
10873

10974
describe(`Commands`, () => {
11075
describe(`config`, () => {
11176
for (const [environmentDescription, environment] of Object.entries(environments)) {
112-
for (const [optionDescription, {flags, cleanupStdout}] of Object.entries(options)) {
77+
for (const [optionDescription, {flags}] of Object.entries(options)) {
11378
test(`test (${environmentDescription} / ${optionDescription})`, makeTemporaryEnv({}, async ({path, run, source}) => {
11479
const cwd = ppath.join(path, `${SUBFOLDER}/${SUBFOLDER}`);
11580
const homePath = await xfs.mktempPromise();
@@ -127,8 +92,8 @@ describe(`Commands`, () => {
12792
({code, stdout, stderr} = error);
12893
}
12994

130-
stdout = cleanupStdout(stdout, path, homePath);
131-
stderr = cleanupPlainOutput(stderr, path, homePath);
95+
stdout = cleanupOutput(stdout, path, homePath);
96+
stderr = cleanupOutput(stderr, path, homePath);
13297

13398
expect({code, stdout, stderr}).toMatchSnapshot();
13499
}));

packages/plugin-essentials/sources/commands/config.ts

+3
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ export default class ConfigCommand extends BaseCommand {
7676

7777
if (this.json) {
7878
for (const name of names) {
79+
if (this.noDefaults && !configuration.sources.has(name))
80+
continue;
81+
7982
const data = configuration.settings.get(name);
8083
if (typeof data === `undefined`)
8184
report.reportError(MessageName.INVALID_CONFIGURATION_KEY, `No configuration key named "${name}"`);

0 commit comments

Comments
 (0)