Use Cargo with JFrog CLI
Configure Cargo for Artifactory and run jf cargo with optional build-info.
Run Cargo, the Rust package manager, with JFrog Artifactory integration for registry configuration, credential injection on selected subcommands, and optional build-info collection.
This topic covers the following tasks:
When to Use
Use jf cargo for Rust projects that use Cargo when Cargo.toml is in the project or workspace root. JFrog CLI wraps the local cargo binary, injects Artifactory credentials for build, install, and publish, and can record build-info.
- Run
jf setup cargoonce per environment to write thejfrogand optionaljfrog-localregistries into Cargo's user-level config. - Run
jf cargo buildorjf cargo installwith--build-nameand--build-numberto collect dependency build-info locally. - Run
jf cargo publishwith those flags to collect dependency and.crateartifact build-info locally, then publish the record withjf rt build-publish. - Run other native Cargo subcommands through
jf cargoas a passthrough with no build-info.
If you don't need build-info or JFrog-managed credentials, you can point Cargo at Artifactory yourself. For more information, see Cargo Repositories.
Note
Package Alias doesn't intercept
cargo. Supported tools aremvn,gradle,npm,yarn,pnpm,go,pip,pipenv,poetry,dotnet,nuget,docker,gem, andbundle. For more information, see Use JFrog CLI Package Alias.
Prerequisites
- Cargo on
PATH:jf cargodoesn't install Rust. Verify withcargo --version. Install from rustup if the binary is missing. - JFrog CLI installed and authenticated: Install or upgrade from the JFrog CLI quick start, then configure a server with
jf config addorjf c add. Use--server-idwhen you need a server other than the default. - Artifactory Cargo repositories: Create a remote Cargo repository for resolution as a crates.io proxy. Create a local Cargo repository if you publish crates. Artifactory doesn't provide virtual Cargo repositories. For more information, see Cargo Repositories.
- Write access to Cargo home for setup:
jf setup cargowrites$CARGO_HOME/config.tomland, when credentials exist,$CARGO_HOME/credentials.toml. The default$CARGO_HOMEis~/.cargo. JFROG_HOME(optional): Set this variable if you use a non-default directory for JFrog CLI configuration files.
Configure Cargo for Artifactory
jf setup cargo configures the local Cargo client to resolve crates through an Artifactory remote repository that replaces crates.io. You can also configure it to publish to an Artifactory local repository.
There's no jf cargo-config or jf cargo config command. Use jf setup cargo.
To configure Cargo for Artifactory:
- Configure a JFrog CLI server with
jf c addorjf config add. - Run
jf setup cargo --server-id=<server-id> --repo=<remote-repository-key>for resolution-only setup, or omit--repoto select a remote repository and optionally a local publish repository interactively. - Confirm
$CARGO_HOME/config.tomlcontains[registries.jfrog]with asparse+https://<host>/artifactory/api/cargo/<repo>/index/URL.
Synopsis
jf setup cargo [--server-id=<server-id>] [--repo=<repository-key>] [--project=<project-key>]Where:
<server-id>: JFrog CLI server ID fromjf config add.<repository-key>: Artifactory remote Cargo repository key used for resolution.<project-key>: JFrog project key used when listing repositories interactively.
Options
The following options apply to jf setup cargo.
| Flag | Default | Description |
|---|---|---|
--server-id | Default server | JFrog CLI server ID from jf config add |
--repo | Interactive selection | Artifactory remote Cargo repository key used for resolution. When this flag is set, setup doesn't prompt for a local publish repository. When omitted, the CLI prompts you to select a remote repository for resolving dependencies, then asks whether to configure a local repository for publishing crates. |
--project | None | JFrog project key used when listing repositories interactively |
Setup writes the following files.
$CARGO_HOME/config.toml:[registry] default = "jfrog",[registries.jfrog] index,[source.crates-io] replace-with = "jfrog", andglobal-credential-providers = ["cargo:token"]. If you selected a local repository interactively, the file also includes[registries.jfrog-local].$CARGO_HOME/credentials.toml:[registries.jfrog]token asBearer <access-token>orBasic <base64(user:password)>, matching the configured server. File mode is0600.
After setup, native cargo build resolves through Artifactory. Publish with cargo publish --registry jfrog-local or jf cargo publish --registry jfrog-local.
Warning
jf setup cargomerges JFrog registry keys into existing Cargo config files. Unrelated keys are preserved. Re-running setup overwrites thejfrogentries. If you pass--repo(resolution only), setup also removes any previousjfrog-localregistry and token so they don't keep pointing at an old local repository. If your access token expires, native Cargo andjf cargocan fail with401until you refresh the JFrog CLI server withjf c addorjf c editand re-runjf setup cargo.
Configure Cargo with Flags
To run non-interactive setup:
- Run:
jf setup cargo --server-id=my-server --repo=cargo-remoteExpected result: Successfully configured cargo to use JFrog repository 'cargo-remote'.
Run Cargo Commands
jf cargo runs the cargo binary with stdin, stdout, and stderr passed through. JFrog CLI removes its own options (--build-name, --build-number, --module, --project, --server-id) from the argument list wherever they appear, then passes the remaining tokens to cargo in order.
When --build-name and --build-number are both set, or when you supply JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER, and the Cargo subcommand is build or install, the CLI collects build-info after a successful run and prints cargo build info saved locally. Publish that record later with jf rt build-publish.
For build, install, and publish, the CLI injects CARGO_REGISTRIES_<NAME>_TOKEN and enables cargo:token for every registry in Cargo config whose index URL is on the same Artifactory host and path as the selected server. Other subcommands don't receive this injection. They use credentials already in Cargo, for example from jf setup cargo.
Synopsis
jf cargo [jfrog-options ...] <cargo-subcommand> [cargo-args ...]Where:
<cargo-subcommand>: First token passed tocargoafter JFrog-specific options are removed.[cargo-args ...]: Remaining arguments and flags forwarded tocargo.
JFrog options are --build-name, --build-number, --module, --project, and --server-id. They can appear before or after the Cargo subcommand.
Aliases: none.
Arguments
The following argument is required.
| Argument | Required | Description |
|---|---|---|
<cargo-subcommand> | Yes | First token passed to cargo after JFrog-specific options are removed. Examples include build, install, publish, test, and check. Remaining arguments and flags are forwarded to cargo unchanged. |
Build Options
The following flags apply to build-info and credential injection.
| Flag | Default | Description |
|---|---|---|
--build-name | JFROG_CLI_BUILD_NAME | Build name for local build-info. Must be provided together with --build-number. |
--build-number | JFROG_CLI_BUILD_NUMBER | Build number for local build-info. Must be provided together with --build-name. |
--module | None | Optional module name override in the build-info. Requires build name and number. |
--project | JFROG_CLI_BUILD_PROJECT | JFrog project key associated with the build-info. |
--server-id | Default server | Server ID from jf config add, used for credential injection. |
Note
Providing only
--build-nameor only--build-numberfails with:the build-name and build-number options cannot be provided separately.
Record Build-Info for a Cargo Build
To record build-info for a Cargo build:
- From the crate or workspace directory, run:
jf cargo build --build-name=<build-name> --build-number=<build-number>Where:
<build-name>: Logical build name. For example,docs-cargo.<build-number>: Build number or CI ID. For example,1.
Full example:
jf cargo build --build-name=docs-cargo --build-number=1Cargo prints its usual compile lines first. Those lines vary by crate, profile, and machine. After a successful run, JFrog CLI prints:
[Info] cargo build info saved locallyThe local record includes a module with "type": "cargo" and "id": "<crate-name>:<version>".
Collect Build-Info from Environment Variables
To collect build-info using environment variables:
- Set
JFROG_CLI_BUILD_NAMEandJFROG_CLI_BUILD_NUMBER, then runjf cargo build:
export JFROG_CLI_BUILD_NAME=ci-cargo-env
export JFROG_CLI_BUILD_NUMBER=99
jf cargo buildExpected output includes [Info] cargo build info saved locally. The saved record uses name ci-cargo-env and number 99.
Publish Crates
jf cargo publish runs native cargo publish with your Cargo arguments, for example --registry jfrog-local or -p <crate>. On success, if --build-name and --build-number are both set, the CLI collects dependency build-info and the published .crate artifact from target/package. If Cargo deleted the local file, the CLI collects the artifact from Artifactory with Artifactory Query Language (AQL). Then it saves the record locally.
To publish a crate and collect build-info:
- Run
jf setup cargoso thejfrog-localregistry exists, or pass--registryfor a registry already in.cargo/config.toml. - Run:
jf cargo publish --registry jfrog-local --build-name=<build-name> --build-number=<build-number>Where:
<build-name>: Logical build name. For example,docs-cargo.<build-number>: Build number or CI ID. For example,1.
- Publish the local build-info to Artifactory:
jf rt build-publish <build-name> <build-number>Full example:
jf cargo publish --registry jfrog-local --build-name=docs-cargo --build-number=1
jf rt build-publish docs-cargo 1Run Native Cargo Subcommands
Subcommands other than build, install, and publish are forwarded to cargo with no build-info collection and no per-run token injection. Examples include test, check, add, and metadata.
To run Cargo tests through JFrog CLI:
- From the crate or workspace directory, run:
jf cargo testThe command prints native cargo test output. JFrog CLI doesn't print cargo build info saved locally.
To run Cargo check through JFrog CLI:
- From the crate or workspace directory, run:
jf cargo checkThe command prints native cargo check output. JFrog CLI doesn't print cargo build info saved locally.
Important Notes
jf cargorequires at least one Cargo argument.jf cargowith no subcommand prints usage and exits1withWrong number of arguments (0).jf cargodoesn't parse or rewrite Cargo flags except the JFrog flags listed above. Native flags such as--release,--features,-p, and--manifest-pathgo tocargo. Feature and--manifest-pathflags thatcargo metadataunderstands are also used when collecting build-info.- Workspace members:
-por--packagelimits which members appear as build-info modules. jf cargodoesn't upload build-info. Usejf rt build-publish.- Token values aren't printed in normal logs. Debug logs redact
cargo logintokens and--tokenvalues.
Troubleshooting
The following table lists common errors and how to resolve them.
| Error | Cause | Fix |
|---|---|---|
exec: "cargo": executable file not found in $PATH | Cargo isn't installed or isn't on PATH | Install Rust from https://rustup.rs and ensure ~/.cargo/bin is on PATH |
Wrong number of arguments (0). | jf cargo with no subcommand | Pass a Cargo subcommand, for example jf cargo build |
the build-name and build-number options cannot be provided separately | Only one of the two flags or environment variables is set | Pass both --build-name and --build-number, or set both JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER |
Refresh access token failed or Invalid token, expired | JFrog CLI server access token expired | Re-authenticate with jf c add or jf c edit. Then re-run jf setup cargo if credentials.toml still has the old token |
failed while attempting to check if repository "<repo>" exists in Artifactory | Setup couldn't validate --repo because of an authentication problem or a missing repository | Confirm the remote Cargo repository key and a valid token |
| Authenticated registries require a credential-provider | Cargo has no cargo:token provider enabled | Run jf setup cargo, which writes global-credential-providers, or enable cargo:token in Cargo config |
Frequently Asked Questions
This section provides answers to frequently asked questions.
FAQs
Q: Does jf cargo Change My .cargo/config.toml?
jf cargo Change My .cargo/config.toml?A: jf cargo itself doesn't write Cargo config files. jf setup cargo does merge registry entries into $CARGO_HOME/config.toml and tokens into $CARGO_HOME/credentials.toml. For more information, see Configure Cargo for Artifactory.
Q: When Does jf cargo Collect Build-Info?
jf cargo Collect Build-Info?A: After a successful build, install, or publish, and only when both build name and build number are set. Other subcommands such as test and check don't collect build-info. For more information, see Run Cargo Commands.
Q: How Do I Publish Build-Info to Artifactory?
A: Run jf rt build-publish <build-name> <build-number> after jf cargo. For more information, see Build Tools.
Q: Can I Use Package Alias With cargo?
cargo?A: No. Package Alias doesn't create a cargo shim. Run jf cargo or native cargo after jf setup cargo.
Q: What Do I Need Before I Run jf cargo?
jf cargo?A: Install Cargo on PATH, configure a JFrog CLI server with jf c add, and create Artifactory Cargo repositories. Use a remote repository for resolution. Use a local repository if you publish. For more information, see Prerequisites.
Q: What Happens If Cargo Is Not on PATH?
PATH?A: jf cargo exits with exec: "cargo": executable file not found in $PATH. Install Rust from https://rustup.rs and add ~/.cargo/bin to PATH. For more information, see Troubleshooting.
Related Topics
Updated about 2 hours ago
