microsoft/aspire · error · DistributedApplicationException
'cargo metadata' failed for the Rust app
Error message
'cargo metadata' failed for the Rust app '{resourceName}' with exit code {process.ExitCode}.{diagnosticSuffix} What it means
After 'cargo metadata' exits with a non-zero exit code, CargoMetadataReader.ReadAsync throws DistributedApplicationException including the exit code and a diagnostic suffix built from formatted stderr. The diagnostic redacts/compares environment variables where relevant so the failure reason from cargo is surfaced.
Solutions
- Read the diagnostic suffix (cargo's stderr) in the message for the underlying cargo error and fix that first.
- Run 'cargo metadata --format-version 1' manually in the resource's workingDirectory to reproduce and see the full error.
- Validate Cargo.toml and workspace member paths; run 'cargo check' to confirm the workspace builds.
- Ensure the full Rust toolchain (rustc, etc.) is installed via rustup, and fix any .cargo/config.toml registry settings.
Example fix
// reproduce the underlying cargo failure // before $ aspire run # 'cargo metadata' failed for the Rust app 'api' with exit code 101. error: failed to parse manifest ... // after $ cd ./api && cargo metadata --format-version 1 > /dev/null # fix reported Cargo.toml issue (e.g. typo in dependency version), then $ aspire run
Defensive patterns
Strategy: validation
Validate before calling
# Validate the workspace before running the apphost: cd <rust-resource-dir> && cargo metadata --format-version 1 > /dev/null && echo OK
Try / catch
try { /* start Rust resource */ }
catch (DistributedApplicationException ex) when (ex.Message.Contains("with exit code"))
{ /* act on the embedded cargo stderr diagnostic */ } Prevention
- Run 'cargo check' or 'cargo metadata' in CI before the apphost to catch broken Cargo.toml early.
- Keep the resource's workingDirectory pointing at the folder containing Cargo.toml.
- Install the complete toolchain via rustup (rustc included), and validate .cargo/config.toml registry settings.
When it happens
Trigger: Running the resource whose workingDirectory has a broken Cargo.toml/workspace, missing toolchain components, unresolvable dependencies, or any condition where cargo metadata itself fails and returns non-zero.
Common situations: Malformed or missing Cargo.toml in the configured directory; workspace member path errors; cargo toolchain components (rustc) not installed even though cargo exists; dependency resolution failures (bad registry, missing crate version); bad .cargo/config.toml.
Related errors
- Aspire.Hosting.Rust requires Cargo 1.71 or later because…
- 'cargo metadata' for the Rust app
- The Rust app ' ' requested the cargo package ' ' with…
- Unable to read the output of 'cargo metadata' for the Rust…
- Unable to start 'cargo' to inspect the Rust app
AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16).
Data as JSON: /api/errors/00d8e676e3d0c102.
Report an issue: GitHub.
Appendix: source
Thrown at src/Aspire.Hosting.Rust/CargoMetadataReader.cs:127
{
TryKillProcess(process);
throw new DistributedApplicationException(
$"'cargo metadata' for the Rust app '{resourceName}' did not complete within {s_timeout.TotalSeconds:0} seconds.");
}
catch (OperationCanceledException)
{
TryKillProcess(process);
throw;
}
var stdout = await stdoutTask.ConfigureAwait(false);
var stderr = await stderrTask.ConfigureAwait(false);
if (process.ExitCode != 0)
{
var diagnostic = FormatStandardError(stderr, environment, startInfo.Environment);
var diagnosticSuffix = diagnostic.Length > 0 ? $" {diagnostic}" : string.Empty;
throw new DistributedApplicationException(
$"'cargo metadata' failed for the Rust app '{resourceName}' with exit code {process.ExitCode}.{diagnosticSuffix}");
}
try
{
return CargoMetadata.Parse(stdout);
}
catch (Exception ex) when (ex is not DistributedApplicationException)
{
throw new DistributedApplicationException(
$"Unable to read the output of 'cargo metadata' for the Rust app '{resourceName}'. Cargo returned invalid {ex.GetType().Name} output.");
}
}
internal static string FormatStandardError(
string standardError,
IReadOnlyDictionary<string, string> environment,
IEnumerable<KeyValuePair<string, string?>>? inheritedEnvironment = null)View on GitHub (pinned to 25830f84bd)