Cargo patch to redirect registry to local path without having to call cargo login

21 views Asked by At

My issue: Rust crate A (and several other crates) depends on crate B (both A and B are local crates). However, since we wanted to share crate A on a private registry (Artifactory), it was required to also publish crate B to a registry (since it is not allowed to depend on local/relative path dependencies on something you publish, which makes sense). So in the project manifest the dependency to crate B is set like this:

crate_b = { version = "0.1.0", registry = "artifactory"}

To accomplish this I have set up the config.toml like this:

[registry]
global-credential-providers = ["cargo:token"]

[net]
git-fetch-with-cli = true

[registries]
artifactory = { index = "sparse+https://my_artifactory_url" }

[package]
publish = "artifactory"

I was surprised to find that when doing cargo build I then needed to first have called cargo login to setup the needed credentials. But since there is a lot of places in our CI/CD setup where we call cargo build in different docker containers, I had hoped that this would not be needed until I decided to actually run cargo publish to push the crate to the registry (which is something we for now only need "on-demand" and not for every build).

I have tried the following: The source code for crate_b still resides in our project. So I though that I could use [patch] to tell cargo to fetch it from a local path instead of from the registry. I have set it like this in the cargo.toml workspace manifest:

[patch.artifactory]
crate_b = { path = "./src/crate_b"}

But it still tries to connect to the registry and requires me to do cargo login.

I have also tried all sorts of combinations of --locked, --frozen, --offline as options to cargo build, but that also did not help.

My question: Is there any way I can run a normal cargo build with a registry dependency (that has been overwritten by a manifest patch), without having to setup my cargo token/credentials?

Bonus question: I thought that "cargo login" simply crates the credentials.toml file for you. However, I tried to just copy in the resulting credentials.toml file to the .cargo folder, but that didn't seem to be enough. Does cargo login do more than just create the credentials.toml?

I hope that some of you awesome Rustacean are able to help me out :)

0

There are 0 answers