Skip to content

Commit 5382dc8

Browse files
rix0rrrgithub-actions
and
github-actions
authored
fix(monorepo): gather-versions leaves repo in a state with diffs (#836)
* fix(monorepo): gather-versions leaves repo in a state with diffs Releasing currently doesn't work since the monorepo release workflow changes `package.json` with exact references to monorepo packages like this: ```js { "monorepoDep": "^0.0.0" } // --> bump { "monorepoDep": "2.100.0" } // --> unbump { "monorepoDep": "0.0.0" } ``` Which counts as a git diff and the `release` step fails. * chore: self mutation Signed-off-by: github-actions <[email protected]> --------- Signed-off-by: github-actions <[email protected]> Co-authored-by: github-actions <[email protected]>
1 parent 85d5cfb commit 5382dc8

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/yarn/gather-versions.exec.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,14 @@ export function main(argv: string[], packageDirectory: string) {
1010
console.error('Positionals:');
1111
console.error(' PKG\tPackage name.');
1212
console.error(' TYPE\tmajor | minor | exact | minimal');
13+
console.error('');
14+
console.error('If $RESET_VERSIONS is "true", ignore the version specifier and just reset all packages to ^0.0.0');
1315
process.exitCode = 1;
1416
return;
1517
}
1618

19+
const isReset = process.env.RESET_VERSIONS === 'true';
20+
1721
const deps = Object.fromEntries(argv.map(x => x.split('=', 2) as [string, string]));
1822

1923
// The PJ file we are updating
@@ -39,7 +43,8 @@ export function main(argv: string[], packageDirectory: string) {
3943
];
4044

4145
for (const [depSection, prefix] of dependencyClasses) {
42-
const updatedRange = prefix + depVersion;
46+
const bumpForward = !isReset;
47+
const updatedRange = bumpForward ? prefix + depVersion : '^0.0.0';
4348

4449
if (manifest[depSection]?.[dep]) {
4550
manifest[depSection][dep] = updatedRange;

src/yarn/typescript-workspace-release.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,17 @@ export class WorkspaceRelease extends Component {
102102
}
103103

104104

105-
// After we have unbumped package versions back to 0.0.0,
106-
// we can run the gather-versions task again which will now replace the to-be-release versions with 0.0.0
107-
this.obtainUnbumpTask().spawn(gatherVersions);
105+
// After we have unbumped package versions back to ^0.0.0,
106+
// we can run the gather-versions task again which will now replace the to-be-release versions with ^0.0.0
107+
//
108+
// We need to set an environment variable to get `gatherVersions` to behave differently in the unbump
109+
// invocation (in the bump invocation, it may change the package.json dependency range from `^0.0.0` to `1.2.3`;
110+
// using a different range symbol), but when unbumping it must always reset back to the `^0.0.0` version.
111+
this.obtainUnbumpTask().spawn(gatherVersions, {
112+
env: {
113+
RESET_VERSIONS: 'true',
114+
},
115+
});
108116
}
109117

110118
/**

test/__snapshots__/cdklabs-monorepo.test.ts.snap

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/gather-versions.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,36 @@ test('gather-versions updates all package versions respecting existing ranges',
5757
});
5858
});
5959

60+
test('if RESET_VERSIONS is true, gather-versions ignores command line and reverts ^0.0.0', async () => {
61+
await withTempDir(async (dir) => {
62+
await writeJsonFiles(dir, {
63+
'node_modules/depA/package.json': {
64+
name: 'depA',
65+
version: '0.0.0',
66+
},
67+
'package.json': {
68+
name: 'root',
69+
dependencies: {
70+
depA: '1.2.3',
71+
},
72+
},
73+
});
74+
75+
// WHEN
76+
process.env.RESET_VERSIONS = 'true';
77+
main(['depA=exact'], dir);
78+
delete process.env.RESET_VERSIONS;
79+
80+
// THEN
81+
expect(JSON.parse(await fs.readFile(path.join(dir, 'package.json'), 'utf-8'))).toEqual({
82+
name: 'root',
83+
dependencies: {
84+
depA: '^0.0.0',
85+
},
86+
});
87+
});
88+
});
89+
6090
const NO_DEVDEPS: Partial<TypeScriptWorkspaceOptions> = {
6191
// We're actually installing these, so cut down on deps
6292
jest: false,

0 commit comments

Comments
 (0)