Skip to content

Commit e97b8c1

Browse files
authored
fix(constraints): make constraints --fix persist changes to manifest (yarnpkg#2245)
* fix(constraints): make constraints --fix persist changes to manifest The change introduced in yarnpkg#1775 to fix fixing the field constraints broke fixing dependency constraints, by overriding the changes made to the manifest before persisting. Fixes yarnpkg#2242 * fix(constraints): add tests to constraints --fix
1 parent 50d4177 commit e97b8c1

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

.yarn/versions/273ab9b6.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
releases:
2+
"@yarnpkg/plugin-constraints": patch
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import {xfs} from '@yarnpkg/fslib';
2+
3+
describe(`Commands`, () => {
4+
const config = {
5+
plugins: [
6+
require.resolve(`@yarnpkg/monorepo/scripts/plugin-constraints.js`),
7+
],
8+
};
9+
10+
const manifest = {
11+
dependencies: {
12+
'is-number': `1.0.0`,
13+
},
14+
license: `MIT`,
15+
};
16+
17+
describe(`constraints --fix`, () => {
18+
test(`test apply fix to dependencies`, makeTemporaryEnv(manifest, config, async ({path, run, source}) => {
19+
await xfs.writeFilePromise(`${path}/constraints.pro`, `
20+
gen_enforced_dependency('.', 'is-number', '2.0.0', dependencies).
21+
`);
22+
23+
await run(`constraints`, `--fix`);
24+
25+
const fixedManifest = await xfs.readJsonPromise(`${path}/package.json`);
26+
27+
expect(fixedManifest.dependencies[`is-number`]).toBe(`2.0.0`);
28+
expect(fixedManifest.license).toBe(`MIT`);
29+
}));
30+
31+
test(`test apply fix to fields`, makeTemporaryEnv(manifest, config, async ({path, run, source}) => {
32+
await xfs.writeFilePromise(`${path}/constraints.pro`, `
33+
gen_enforced_field('.', 'license', 'BSD-2-Clause').
34+
`);
35+
36+
await run(`constraints`, `--fix`);
37+
38+
const fixedManifest = await xfs.readJsonPromise(`${path}/package.json`);
39+
40+
expect(fixedManifest.dependencies[`is-number`]).toBe(`1.0.0`);
41+
expect(fixedManifest.license).toBe(`BSD-2-Clause`);
42+
}));
43+
44+
test(`test apply fix to fields and manifests`, makeTemporaryEnv(manifest, config, async ({path, run, source}) => {
45+
await xfs.writeFilePromise(`${path}/constraints.pro`, `
46+
gen_enforced_dependency('.', 'is-number', '2.0.0', dependencies).
47+
gen_enforced_field('.', 'license', 'BSD-2-Clause').
48+
`);
49+
50+
await run(`constraints`, `--fix`);
51+
52+
const fixedManifest = await xfs.readJsonPromise(`${path}/package.json`);
53+
54+
expect(fixedManifest.dependencies[`is-number`]).toBe(`2.0.0`);
55+
expect(fixedManifest.license).toBe(`BSD-2-Clause`);
56+
}));
57+
});
58+
});

packages/plugin-constraints/sources/commands/constraints.ts

+15-9
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,45 @@ export default class ConstraintsCheckCommand extends BaseCommand {
4949
for (let t = 0, T = this.fix ? 10 : 1; t < T; ++t) {
5050
errors = [];
5151

52-
const toSave = new Set<Workspace>();
5352
const result = await constraints.process();
5453

55-
await processDependencyConstraints(toSave, errors, result.enforcedDependencies, {
54+
const modifiedDependencies = new Set<Workspace>();
55+
await processDependencyConstraints(modifiedDependencies, errors, result.enforcedDependencies, {
5656
fix: this.fix,
5757
configuration,
5858
});
5959

60-
await processFieldConstraints(toSave, errors, result.enforcedFields, {
60+
// Dependency constraints work on the manifess, field constraints work on the raw JSON objects
61+
for (const {manifest} of modifiedDependencies)
62+
manifest.exportTo(manifest.raw = {});
63+
64+
const modifiedFields = new Set<Workspace>();
65+
await processFieldConstraints(modifiedFields, errors, result.enforcedFields, {
6166
fix: this.fix,
6267
configuration,
6368
});
6469

70+
// Persist changes made by the constraints back to the Manifest instance
71+
for (const {manifest} of modifiedFields)
72+
manifest.load(manifest.raw);
73+
6574
allSaves = new Set([
6675
...allSaves,
67-
...toSave,
76+
...modifiedDependencies,
77+
...modifiedFields,
6878
]);
6979

7080
// If we didn't apply any change then we can exit the loop
71-
if (toSave.size === 0) {
81+
if (modifiedDependencies.size === 0 && modifiedFields.size === 0) {
7282
break;
7383
}
7484
}
7585

7686
// save all modified manifests
7787
await Promise.all([...allSaves].map(async workspace => {
78-
// Constraints modify the raw manifest so we need to reload it here
79-
// otherwise changes are not persisted
80-
workspace.manifest.load(workspace.manifest.raw);
8188
await workspace.persistManifest();
8289
}));
8390

84-
8591
// report all outstanding errors
8692
for (const [messageName, message] of errors) {
8793
report.reportError(messageName, message);

0 commit comments

Comments
 (0)