Skip to content

Commit 38d2007

Browse files
authored
Stops reporting identical objects as being different (#5934)
**What's the problem this PR addresses?** The constraints engine is making a `===` check, so using the `set` method with a JS object will never match and will report an error (example [here](https://github.com/babel/babel/actions/runs/6711592648/job/18239331717?pr=16069#step:4:8)). **How did you fix it?** We now compare stringified values to check sameness. I considered using something like `_.equals`, but using stringify only returns `true` when the key are in the same order, which is important for fields like `exports`. **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` --> - [x] 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. --> - [x] I will check that all automated PR checks pass before the PR gets reviewed.
1 parent 7bdfc60 commit 38d2007

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

.yarn/versions/68e485e1.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-constraints": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-dlx"
8+
- "@yarnpkg/plugin-essentials"
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/constraints.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,39 @@ describe(`Commands`, () => {
107107
await expect(run(`constraints`)).rejects.toThrow(/This should fail/);
108108
}));
109109

110+
it(`shouldn't report errors when comparing identical objects`, makeTemporaryEnv({
111+
foo: {
112+
ok: true,
113+
},
114+
}, async ({path, run, source}) => {
115+
await run(`install`);
116+
117+
await writeFile(ppath.join(path, `yarn.config.cjs`), `
118+
exports.constraints = ({Yarn}) => {
119+
Yarn.workspace().set('foo', {ok: true});
120+
};
121+
`);
122+
123+
await run(`constraints`);
124+
}));
125+
126+
it(`should report an error when comparing objects with different key ordering`, makeTemporaryEnv({
127+
foo: {
128+
b: true,
129+
a: true,
130+
},
131+
}, async ({path, run, source}) => {
132+
await run(`install`);
133+
134+
await writeFile(ppath.join(path, `yarn.config.cjs`), `
135+
exports.constraints = ({Yarn}) => {
136+
Yarn.workspace().set('foo', {a: true, b: true});
137+
};
138+
`);
139+
140+
await expect(run(`constraints`)).rejects.toThrow(`Invalid field foo; expected { a: true, b: true }, found { b: true, a: true }`);
141+
}));
142+
110143
for (const [environmentDescription, environment] of Object.entries(environments)) {
111144
for (const [scriptDescription, scripts] of Object.entries(constraints)) {
112145
for (const [scriptType, script] of Object.entries(scripts)) {

packages/plugin-constraints/sources/constraintUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ export function applyEngineReport(project: Project, {manifestUpdates, reportedEr
203203
const [[newValue]] = newValues;
204204

205205
const currentValue = get(manifest, fieldPath);
206-
if (currentValue === newValue)
206+
if (JSON.stringify(currentValue) === JSON.stringify(newValue))
207207
continue;
208208

209209
if (!fix) {

0 commit comments

Comments
 (0)