{"record":{"id":"c0837aef2b9e4099","repo":"rust-lang/rust","slug":"cargo-invocation-has-failed-err","errorCode":null,"errorMessage":"Cargo invocation has failed: ${err}","messagePattern":"Cargo invocation has failed: (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/tools/rust-analyzer/editors/code/src/toolchain.ts","lineNumber":103,"sourceCode":"                        const isBuildScript = message.target.kind.includes(\"custom-build\");\n                        if ((isBinary && !isBuildScript) || message.profile.test) {\n                            artifacts.push({\n                                fileName: message.executable,\n                                name: message.target.name,\n                                kind: message.target.kind[0],\n                                isTest: message.profile.test,\n                            });\n                        }\n                    } else if (message.reason === \"compiler-message\") {\n                        log.info(message.message.rendered);\n                    }\n                },\n                (stderr) => log.error(stderr),\n                env,\n            );\n        } catch (err) {\n            log.error(`Cargo invocation has failed: ${err}`);\n            throw new Error(`Cargo invocation has failed: ${err}`, { cause: err });\n        }\n\n        return spec.filter?.(artifacts) ?? artifacts;\n    }\n\n    async executableFromArgs(runnableArgs: CargoRunnableArgs): Promise<string> {\n        const artifacts = await this.getArtifacts(\n            Cargo.artifactSpec(runnableArgs.cargoArgs, runnableArgs.executableArgs),\n            runnableArgs.environment,\n        );\n\n        if (artifacts.length === 0) {\n            throw new Error(\"No compilation artifacts\");\n        } else if (artifacts.length > 1) {\n            throw new Error(\"Multiple compilation artifacts are not supported.\");\n        }\n\n        const artifact = unwrapUndefinable(artifacts[0]);","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/src/tools/rust-analyzer/editors/code/src/toolchain.ts#L85-L121","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"import { cargoPath } from './toolchain';\n\n// Pre-check: verify cargo exists and runs before launching a build\nconst cp = await cargoPath(env);\nimport * as vscode from 'vscode';\nconst isFile = await vscode.workspace.fs.stat(vscode.Uri.file(cp)).then(\n  () => true, () => false\n);\nif (!isFile) {\n  throw new Error(`cargo not found at resolved path: ${cp}`);\n}","typeGuard":null,"tryCatchPattern":"try {\n  const exe = await cargo.executableFromArgs(runnableArgs);\n} catch (e) {\n  if (e.message.startsWith('Cargo invocation has failed')) {\n    // The cause is in e.cause — could be spawn error, non-zero exit, or JSON parse\n    const cause = (e as Error & { cause?: Error }).cause;\n    log.error('Cargo failed:', cause?.message ?? e.message);\n    // Optionally retry once or show terminal output to user\n  }\n  throw e;\n}","preventionTips":["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."],"tags":["rust-analyzer","cargo","build","child-process","typescript"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}