Skip to content

Commit

Permalink
feat(manager/cargo): support private registry index env var (#20379)
Browse files Browse the repository at this point in the history
Co-authored-by: Rhys Arkins <[email protected]>
Co-authored-by: HonkingGoose <[email protected]>
Co-authored-by: Sebastian Poxhofer <[email protected]>
  • Loading branch information
4 people authored Apr 18, 2023
1 parent 7d6438b commit 2a72f85
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 2 deletions.
12 changes: 11 additions & 1 deletion docs/usage/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ Renovate supports upgrading dependencies in `Cargo.toml` files and their accompa

1. Renovate searches in each repository for any `Cargo.toml` files
1. Renovate extracts existing dependencies from `[dependencies]`, `[dev-dependencies]`, `[build-dependencies]` and `[workspace.dependencies]`
1. Renovate tries to find and parse a `.cargo/config.toml` file to discover index URLs for private registries
1. Renovate looks up Cargo configuration to find index URLs for private registries
1. Renovate resolves the dependency's version using the crates.io API or by cloning the index URL
1. If Renovate finds an update, Renovate will use `cargo update` to update both `Cargo.toml` and `Cargo.lock`

## Enabling Rust Modules Updating

Renovate updates Rust crates by default.

## Cargo configuration and private registry discovery

Renovate can find private registry URLs in these Cargo configuration files:

- `.cargo/config.toml`
- `.cargo/config` (legacy)

Renovate can also find private registry URLs via a `CARGO_REGISTRIES_<name>_INDEX` environment variable.
Read the [Rust environment variables docs](https://doc.rust-lang.org/cargo/reference/environment-variables.html#configuration-environment-variables) to learn more.

## Private crate registries and private Git dependencies

If any dependencies are hosted in private Git repositories, [Git Authentication for cargo](https://doc.rust-lang.org/cargo/appendix/git-authentication.html) must be set up.
Expand Down
46 changes: 46 additions & 0 deletions lib/modules/manager/cargo/extract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ describe('modules/manager/cargo/extract', () => {
};

GlobalConfig.set(adminConfig);
delete process.env.CARGO_REGISTRIES_PRIVATE_CRATES_INDEX;
delete process.env.CARGO_REGISTRIES_MCORBIN_INDEX;
});

afterEach(async () => {
Expand Down Expand Up @@ -113,6 +115,50 @@ describe('modules/manager/cargo/extract', () => {
expect(res?.deps).toHaveLength(3);
});

it('extracts registry urls from environment', async () => {
process.env.CARGO_REGISTRIES_PRIVATE_CRATES_INDEX =
'https://dl.cloudsmith.io/basic/my-org/my-repo/cargo/index.git';
process.env.CARGO_REGISTRIES_MCORBIN_INDEX =
'https://github.com/mcorbin/testregistry';
const res = await extractPackageFile(cargo6toml, 'Cargo.toml', {
...config,
});

expect(res?.deps).toEqual([
{
currentValue: '0.1.0',
datasource: 'crate',
depName: 'proprietary-crate',
depType: 'dependencies',
managerData: {
nestedVersion: true,
},
registryUrls: [
'https://dl.cloudsmith.io/basic/my-org/my-repo/cargo/index.git',
],
},
{
currentValue: '3.0.0',
datasource: 'crate',
depName: 'mcorbin-test',
depType: 'dependencies',
managerData: {
nestedVersion: true,
},
registryUrls: ['https://github.com/mcorbin/testregistry'],
},
{
currentValue: '0.2',
datasource: 'crate',
depName: 'tokio',
depType: 'dependencies',
managerData: {
nestedVersion: false,
},
},
]);
});

it('extracts workspace dependencies', async () => {
const cargoToml = codeBlock`
[package]
Expand Down
9 changes: 8 additions & 1 deletion lib/modules/manager/cargo/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import type {
CargoSection,
} from './types';

function getCargoIndexEnv(registryName: string): string | null {
const registry = registryName.toUpperCase().replaceAll('-', '_');
return process.env[`CARGO_REGISTRIES_${registry}_INDEX`] ?? null;
}

function extractFromSection(
parsedContent: CargoSection,
section: keyof CargoSection,
Expand Down Expand Up @@ -47,7 +52,9 @@ function extractFromSection(
currentValue = version;
nestedVersion = true;
if (registryName) {
const registryUrl = cargoRegistries[registryName];
const registryUrl =
cargoRegistries[registryName] ?? getCargoIndexEnv(registryName);

if (registryUrl) {
registryUrls = [registryUrl];
} else {
Expand Down

0 comments on commit 2a72f85

Please sign in to comment.