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

  1. Read the diagnostic suffix (cargo's stderr) in the message for the underlying cargo error and fix that first.
  2. Run 'cargo metadata --format-version 1' manually in the resource's workingDirectory to reproduce and see the full error.
  3. Validate Cargo.toml and workspace member paths; run 'cargo check' to confirm the workspace builds.
  4. 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

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


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)