rust-lang/rust · error · Error
Cargo invocation has failed: ${err}
Error message
Cargo invocation has failed: ${err} What it means
Thrown by Cargo.getArtifacts() in toolchain.ts when runCargo() rejects. runCargo() spawns the cargo executable with the provided args and rejects on three conditions: (a) the spawn itself emits an 'error' event (binary not found / not executable) at line 139, (b) cargo exits with a non-zero code at line 151, or (c) a line of stdout fails JSON.parse at line 145. The error wraps the original cause via {cause: err} so the underlying reason is preserved.
Source
Thrown at src/tools/rust-analyzer/editors/code/src/toolchain.ts:103
const isBuildScript = message.target.kind.includes("custom-build");
if ((isBinary && !isBuildScript) || message.profile.test) {
artifacts.push({
fileName: message.executable,
name: message.target.name,
kind: message.target.kind[0],
isTest: message.profile.test,
});
}
} else if (message.reason === "compiler-message") {
log.info(message.message.rendered);
}
},
(stderr) => log.error(stderr),
env,
);
} catch (err) {
log.error(`Cargo invocation has failed: ${err}`);
throw new Error(`Cargo invocation has failed: ${err}`, { cause: err });
}
return spec.filter?.(artifacts) ?? artifacts;
}
async executableFromArgs(runnableArgs: CargoRunnableArgs): Promise<string> {
const artifacts = await this.getArtifacts(
Cargo.artifactSpec(runnableArgs.cargoArgs, runnableArgs.executableArgs),
runnableArgs.environment,
);
if (artifacts.length === 0) {
throw new Error("No compilation artifacts");
} else if (artifacts.length > 1) {
throw new Error("Multiple compilation artifacts are not supported.");
}
const artifact = unwrapUndefinable(artifacts[0]);View on GitHub (pinned to 7088e4b63a)
Solutions
- Run the exact cargo command shown in the error from a terminal in the workspace root to see the full compiler/spawn error.
- Verify cargo is installed and on PATH: 'cargo --version' and 'rustup which cargo'.
- Fix any compilation errors reported by cargo in the terminal output.
- Check that CARGO_HOME and PATH in the VS Code integrated terminal match the resolved environment (see cargoPath resolution in toolchain.ts).
- Ensure the workspace's Cargo.toml is valid and all dependencies resolve.
Defensive patterns
Strategy: try-catch
Validate before calling
import { cargoPath } from './toolchain';
// Pre-check: verify cargo exists and runs before launching a build
const cp = await cargoPath(env);
import * as vscode from 'vscode';
const isFile = await vscode.workspace.fs.stat(vscode.Uri.file(cp)).then(
() => true, () => false
);
if (!isFile) {
throw new Error(`cargo not found at resolved path: ${cp}`);
} Try / catch
try {
const exe = await cargo.executableFromArgs(runnableArgs);
} catch (e) {
if (e.message.startsWith('Cargo invocation has failed')) {
// The cause is in e.cause — could be spawn error, non-zero exit, or JSON parse
const cause = (e as Error & { cause?: Error }).cause;
log.error('Cargo failed:', cause?.message ?? e.message);
// Optionally retry once or show terminal output to user
}
throw e;
} Prevention
- Ensure cargo is installed and on PATH before using Run/Debug features.
- Keep the project compiling cleanly — fix all cargo errors before using code lenses.
- Verify CARGO_HOME and PATH are set correctly in the VS Code terminal environment.
- If using a custom toolchain via rust-toolchain.toml, run 'rustup show' to confirm it activates.
When it happens
Trigger: getArtifacts() calls runCargo(spec.cargoArgs, ...) at toolchain.ts:80. runCargo rejects when: cp.spawn(cargoPath, args) emits an error event (line 139 — cargo binary missing or cannot launch), cargo exits non-zero (line 149-151 — compilation error, missing dependencies, bad Cargo.toml), or JSON.parse throws on a non-JSON stdout line (line 145 — cargo printed a non-JSON diagnostic before the machine-readable output). The catch at line 101-103 wraps it.
Common situations: Running or debugging a Rust binary/test through rust-analyzer's code lens or Run button when the project doesn't compile; cargo is not installed or not on PATH; a rust-toolchain.toml override points to a broken toolchain; CARGO_HOME or PATH env is misconfigured; a dependency fails to build; or cargo emits a warning/error line to stdout that isn't valid JSON.
Related errors
- No compilation artifacts
- Multiple compilation artifacts are not supported.
- proc-macro-srv-cli needs to be compiled with the `in-rust-tr
- {e:?}: {error}
- rust-analyzer Language Server is not available. Please, ensu
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/c0837aef2b9e4099.
Report an issue: GitHub.