Skip to content

Commit

Permalink
feat(manager/cargo): support reading package version from workspace (…
Browse files Browse the repository at this point in the history
…local only) (#32533)
  • Loading branch information
viceice authored Nov 14, 2024
1 parent 7e6d11a commit 6de1322
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
16 changes: 16 additions & 0 deletions lib/modules/manager/cargo/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,5 +635,21 @@ replace-with = "mcorbin"
const res = await extractPackageFile(cargotoml, 'Cargo.toml', config);
expect(res?.packageFileVersion).toBe('0.1.0');
});

it('should extract project version from workspace', async () => {
const cargotoml = codeBlock`
[package]
name = "test"
version.workspace = true
edition = "2021"
[workspace.package]
version = "0.1.0"
[dependencies]
syn = "2.0"
`;

const res = await extractPackageFile(cargotoml, 'Cargo.toml', config);
expect(res?.packageFileVersion).toBe('0.1.0');
});
});
});
11 changes: 10 additions & 1 deletion lib/modules/manager/cargo/extract.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { coerceArray } from '../../../util/array';
import { findLocalSiblingOrParent, readLocalFile } from '../../../util/fs';
Expand Down Expand Up @@ -248,7 +249,15 @@ export async function extractPackageFile(
const packageSection = cargoManifest.package;
let version: string | undefined = undefined;
if (packageSection) {
version = packageSection.version;
if (is.string(packageSection.version)) {
version = packageSection.version;
} else if (
is.object(packageSection.version) &&
cargoManifest.workspace?.package?.version
) {
// TODO: Support reading from parent workspace manifest?
version = cargoManifest.workspace.package.version;
}
}

const lockFileName = await findLocalSiblingOrParent(
Expand Down
13 changes: 12 additions & 1 deletion lib/modules/manager/cargo/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,24 @@ const CargoSection = z.object({

const CargoWorkspace = z.object({
dependencies: withDepType(CargoDeps, 'workspace.dependencies').optional(),
package: z
.object({
version: z.string().optional(),
})
.optional(),
});

const CargoTarget = z.record(z.string(), CargoSection);

export const CargoManifestSchema = Toml.pipe(
CargoSection.extend({
package: z.object({ version: z.string().optional() }).optional(),
package: z
.object({
version: z
.union([z.string(), z.object({ workspace: z.literal(true) })])
.optional(),
})
.optional(),
workspace: CargoWorkspace.optional(),
target: CargoTarget.optional(),
}),
Expand Down

0 comments on commit 6de1322

Please sign in to comment.