Skip to content

Commit 3ca3bbc

Browse files
authored
fix(cargo-workspace): Update target dependencies in Cargo workspace packages (#1730)
* fix(cargo-workspace): Update target-scoped deps Update target-scoped dependencies in Cargo workspace packages * fix(cargo-workspace): Merge dep type updates
1 parent c588060 commit 3ca3bbc

5 files changed

Lines changed: 109 additions & 19 deletions

File tree

__snapshots__/cargo-workspace.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ Release notes for path: packages/rustA, releaseType: rust
2727
* pkgB bumped from 2.2.2 to 2.2.3
2828
</details>
2929
30+
<details><summary>pkgE: 3.3.4</summary>
31+
32+
### Dependencies
33+
34+
* The following workspace dependencies were updated
35+
* dependencies
36+
* pkgA bumped from 1.1.1 to 1.1.2
37+
</details>
38+
3039
---
3140
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
3241
`
@@ -83,6 +92,21 @@ Release notes for path: packages/rustD, releaseType: rust
8392
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
8493
`
8594

95+
exports['CargoWorkspace plugin run can skip merging rust packages 5'] = `
96+
:robot: I have created a release *beep* *boop*
97+
---
98+
99+
100+
### Dependencies
101+
102+
* The following workspace dependencies were updated
103+
* dependencies
104+
* pkgA bumped from 1.1.1 to 1.1.2
105+
106+
---
107+
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
108+
`
109+
86110
exports['CargoWorkspace plugin run combines rust packages 1'] = `
87111
:robot: I have created a release *beep* *boop*
88112
---
@@ -172,6 +196,15 @@ Release notes for path: packages/rustA, releaseType: rust
172196
Release notes for path: packages/rustD, releaseType: rust
173197
</details>
174198
199+
<details><summary>pkgE: 3.3.4</summary>
200+
201+
### Dependencies
202+
203+
* The following workspace dependencies were updated
204+
* dependencies
205+
* pkgA bumped from 1.1.1 to 1.1.2
206+
</details>
207+
175208
---
176209
This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
177210
`

src/plugins/cargo-workspace.ts

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
parseCargoManifest,
2626
CargoDependencies,
2727
CargoDependency,
28+
TargetDependencies,
2829
} from '../updaters/rust/common';
2930
import {VersionsMap, Version} from '../version';
3031
import {CargoToml} from '../updaters/rust/cargo-toml';
@@ -313,6 +314,22 @@ export class CargoWorkspace extends WorkspacePlugin<CrateInfo> {
313314
...(crateInfo.manifest['dev-dependencies'] ?? {}),
314315
...(crateInfo.manifest['build-dependencies'] ?? {}),
315316
});
317+
318+
const targets = crateInfo.manifest.target;
319+
if (targets) {
320+
for (const targetName in targets) {
321+
const target = targets[targetName];
322+
323+
allDeps.push(
324+
...Object.keys({
325+
...(target.dependencies ?? {}),
326+
...(target['dev-dependencies'] ?? {}),
327+
...(target['build-dependencies'] ?? {}),
328+
})
329+
);
330+
}
331+
}
332+
316333
const workspaceDeps = allDeps.filter(dep => workspaceCrateNames.has(dep));
317334
graph.set(crateInfo.name, {
318335
deps: workspaceDeps,
@@ -370,27 +387,52 @@ function getChangelogDepsNotes(
370387
return result;
371388
};
372389

373-
const updates: Map<DT, string[]> = new Map();
374-
for (const depType of depTypes) {
375-
const depUpdates = [];
376-
const pkgDepTypes = updatedManifest[depType];
377-
if (pkgDepTypes === undefined) {
378-
continue;
379-
}
380-
for (const [depName, currentDepVer] of Object.entries(
381-
getDepMap(pkgDepTypes)
382-
)) {
383-
const origDepVer = depVer(originalManifest[depType]?.[depName]);
384-
if (currentDepVer !== origDepVer) {
385-
depUpdates.push(
386-
`\n * ${depName} bumped from ${origDepVer} to ${currentDepVer}`
387-
);
390+
type DepUpdates = Map<DT, Set<string>>;
391+
392+
const populateUpdates = (
393+
originalScope: CargoManifest | TargetDependencies[string],
394+
updatedScope: CargoManifest | TargetDependencies[string],
395+
updates: DepUpdates
396+
) => {
397+
for (const depType of depTypes) {
398+
const depUpdates = [];
399+
const pkgDepTypes = updatedScope[depType];
400+
401+
if (pkgDepTypes === undefined) {
402+
continue;
403+
}
404+
for (const [depName, currentDepVer] of Object.entries(
405+
getDepMap(pkgDepTypes)
406+
)) {
407+
const origDepVer = depVer(originalScope[depType]?.[depName]);
408+
if (currentDepVer !== origDepVer) {
409+
depUpdates.push(
410+
`\n * ${depName} bumped from ${origDepVer} to ${currentDepVer}`
411+
);
412+
}
413+
}
414+
if (depUpdates.length > 0) {
415+
const updatesForType = updates.get(depType) || new Set();
416+
depUpdates.forEach(update => updatesForType.add(update));
417+
updates.set(depType, updatesForType);
388418
}
389419
}
390-
if (depUpdates.length > 0) {
391-
updates.set(depType, depUpdates);
420+
};
421+
422+
const updates: DepUpdates = new Map();
423+
424+
populateUpdates(originalManifest, updatedManifest, updates);
425+
426+
if (updatedManifest.target && originalManifest.target) {
427+
for (const targetName in updatedManifest.target) {
428+
populateUpdates(
429+
originalManifest.target[targetName],
430+
updatedManifest.target[targetName],
431+
updates
432+
);
392433
}
393434
}
435+
394436
for (const [dt, notes] of updates) {
395437
depUpdateNotes += `\n * ${dt}`;
396438
for (const note of notes) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["packages/rustA", "packages/rustB", "packages/rustC", "packages/rustD"]
2+
members = ["packages/rustA", "packages/rustB", "packages/rustC", "packages/rustD", "packages/rustE"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "pkgE"
3+
version = "3.3.3"
4+
5+
[dependencies]
6+
7+
[target.'cfg(foobar)'.dependencies]
8+
pkgA = { version = "1.1.1", path = "../pkgA" }

test/plugins/cargo-workspace.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ describe('CargoWorkspace plugin', () => {
216216
'packages/rustB/Cargo.toml',
217217
'packages/rustC/Cargo.toml',
218218
'packages/rustD/Cargo.toml',
219+
'packages/rustE/Cargo.toml',
219220
],
220221
flatten: false,
221222
targetBranch: 'main',
@@ -231,6 +232,7 @@ describe('CargoWorkspace plugin', () => {
231232
assertHasUpdate(updates, 'packages/rustB/Cargo.toml', RawContent);
232233
assertHasUpdate(updates, 'packages/rustC/Cargo.toml', RawContent);
233234
assertHasUpdate(updates, 'packages/rustD/Cargo.toml', RawContent);
235+
assertHasUpdate(updates, 'packages/rustE/Cargo.toml', RawContent);
234236
snapshot(dateSafe(rustCandidate!.pullRequest.body.toString()));
235237
});
236238
it('can skip merging rust packages', async () => {
@@ -265,6 +267,7 @@ describe('CargoWorkspace plugin', () => {
265267
'packages/rustB/Cargo.toml',
266268
'packages/rustC/Cargo.toml',
267269
'packages/rustD/Cargo.toml',
270+
'packages/rustE/Cargo.toml',
268271
],
269272
flatten: false,
270273
targetBranch: 'main',
@@ -285,7 +288,7 @@ describe('CargoWorkspace plugin', () => {
285288
}
286289
);
287290
const newCandidates = await plugin.run(candidates);
288-
expect(newCandidates).lengthOf(4);
291+
expect(newCandidates).lengthOf(5);
289292
for (const newCandidate of newCandidates) {
290293
safeSnapshot(newCandidate.pullRequest.body.toString());
291294
}
@@ -324,6 +327,7 @@ describe('CargoWorkspace plugin', () => {
324327
'packages/rustB/Cargo.toml',
325328
'packages/rustC/Cargo.toml',
326329
'packages/rustD/Cargo.toml',
330+
'packages/rustE/Cargo.toml',
327331
],
328332
flatten: false,
329333
targetBranch: 'main',
@@ -338,6 +342,7 @@ describe('CargoWorkspace plugin', () => {
338342
assertHasUpdate(updates, 'packages/rustA/Cargo.toml', RawContent);
339343
assertHasUpdate(updates, 'packages/rustB/Cargo.toml', RawContent);
340344
assertHasUpdate(updates, 'packages/rustC/Cargo.toml', RawContent);
345+
assertHasUpdate(updates, 'packages/rustE/Cargo.toml', RawContent);
341346
snapshot(dateSafe(rustCandidate!.pullRequest.body.toString()));
342347
});
343348
it('skips component if not touched', async () => {
@@ -362,6 +367,7 @@ describe('CargoWorkspace plugin', () => {
362367
'packages/rustB/Cargo.toml',
363368
'packages/rustC/Cargo.toml',
364369
'packages/rustD/Cargo.toml',
370+
'packages/rustE/Cargo.toml',
365371
],
366372
flatten: false,
367373
targetBranch: 'main',
@@ -375,6 +381,7 @@ describe('CargoWorkspace plugin', () => {
375381
const updates = rustCandidate!.pullRequest.updates;
376382
// pkgA is not touched and does not have a dependency on pkgB
377383
assertNoHasUpdate(updates, 'packages/rustA/Cargo.toml');
384+
assertNoHasUpdate(updates, 'packages/rustE/Cargo.toml');
378385
assertHasUpdate(updates, 'packages/rustB/Cargo.toml', RawContent);
379386
snapshot(dateSafe(rustCandidate!.pullRequest.body.toString()));
380387
});

0 commit comments

Comments
 (0)