Skip to content

Commit

Permalink
Stops reporting identical objects as being different (#5934)
Browse files Browse the repository at this point in the history
**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.
  • Loading branch information
arcanis authored Nov 3, 2023
1 parent 7bdfc60 commit 38d2007
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
23 changes: 23 additions & 0 deletions .yarn/versions/68e485e1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
releases:
"@yarnpkg/cli": patch
"@yarnpkg/plugin-constraints": patch

declined:
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnp"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,39 @@ describe(`Commands`, () => {
await expect(run(`constraints`)).rejects.toThrow(/This should fail/);
}));

it(`shouldn't report errors when comparing identical objects`, makeTemporaryEnv({
foo: {
ok: true,
},
}, async ({path, run, source}) => {
await run(`install`);

await writeFile(ppath.join(path, `yarn.config.cjs`), `
exports.constraints = ({Yarn}) => {
Yarn.workspace().set('foo', {ok: true});
};
`);

await run(`constraints`);
}));

it(`should report an error when comparing objects with different key ordering`, makeTemporaryEnv({
foo: {
b: true,
a: true,
},
}, async ({path, run, source}) => {
await run(`install`);

await writeFile(ppath.join(path, `yarn.config.cjs`), `
exports.constraints = ({Yarn}) => {
Yarn.workspace().set('foo', {a: true, b: true});
};
`);

await expect(run(`constraints`)).rejects.toThrow(`Invalid field foo; expected { a: true, b: true }, found { b: true, a: true }`);
}));

for (const [environmentDescription, environment] of Object.entries(environments)) {
for (const [scriptDescription, scripts] of Object.entries(constraints)) {
for (const [scriptType, script] of Object.entries(scripts)) {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-constraints/sources/constraintUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function applyEngineReport(project: Project, {manifestUpdates, reportedEr
const [[newValue]] = newValues;

const currentValue = get(manifest, fieldPath);
if (currentValue === newValue)
if (JSON.stringify(currentValue) === JSON.stringify(newValue))
continue;

if (!fix) {
Expand Down

0 comments on commit 38d2007

Please sign in to comment.