microsoft/aspire · error · DistributedApplicationException

Unable to work out which binary the Rust app

Error message

Unable to work out which binary the Rust app '{resourceName}' produces: 'cargo metadata' reported {runnablePackages.Count} default workspace members with a binary target. Call WithCargoPackage("<name>") to select one. Available packages: {FormatPackageNames(metadata)}.

What it means

ResolvePackage throws this DistributedApplicationException when no WithCargoPackage was specified and the workspace's default members contain more than one package with a binary target, making the choice ambiguous (library-only members are filtered out first). Aspire refuses to guess which executable the resource should run, and the message includes the full package list plus the WithCargoPackage remedy. It fails before any build so the fix is cheap.

Solutions

  1. Call .WithCargoPackage("<package-name>") on the resource to pick the intended runnable package
  2. Use .WithCargoBinTarget("<name>") if the desired package was already resolved but has several binaries
  3. Restrict the workspace's default-members in the root Cargo.toml so only one runnable crate is a default member

Example fix

// before
builder.AddRustApp("app", "../rust-workspace"); // server + cli both have bins
// after
builder.AddRustApp("app", "../rust-workspace")
       .WithCargoPackage("server");
Defensive patterns

Strategy: validation

Validate before calling

var runnable = GetDefaultMembersWithBins(appDir);
if (runnable.Count > 1) Console.WriteLine($"Ambiguous workspace packages: {string.Join(", ", runnable)} — call WithCargoPackage.");

Type guard

bool HasSingleRunnableDefaultMember(CargoMetadata m) => m.Packages.Count(p => m.DefaultMemberIds.Contains(p.Id) && p.BinTargetNames.Count > 0) == 1;

Try / catch

try { builder.AddRustApp("app", dir); } catch (DistributedApplicationException ex) when (ex.Message.Contains("default workspace members")) { log.LogError(ex, "Ambiguous cargo workspace"); }

Prevention

When it happens

Trigger: Adding a RustAppResource on a workspace where 2+ default-member crates have bin targets (e.g. a workspace with both a server and a cli crate), without calling WithCargoPackage or WithCargoBinTarget. Count of 0 goes through a different path.

Common situations: Growing a single-crate directory into a multi-cargo workspace and then running the AppHost; vendoring extra runnable crates into the workspace; cloning a monorepo workspace where the resource previously auto-resolved to the only binary crate.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/00e1367f9102d337. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Rust/RustCargoTargetResolver.cs:186

                    $"The Rust app '{resourceName}' requested the cargo package '{requestedPackage}' with WithCargoPackage, but " +
                    $"'cargo metadata' reported no such package. Available packages: {FormatPackageNames(metadata)}.");
        }

        var defaultPackages = metadata.Packages.Where(p => metadata.DefaultMemberIds.Contains(p.Id)).ToList();

        if (defaultPackages is [var onlyMember])
        {
            return onlyMember;
        }

        // `cargo run` only needs one *runnable* member, so the common workspace shape of an app crate beside
        // library crates runs fine. Library-only members are dropped before the choice is called ambiguous.
        var runnablePackages = defaultPackages.Where(static p => p.BinTargetNames.Count > 0).ToList();

        return runnablePackages switch
        {
            [var single] => single,
            _ => throw new DistributedApplicationException(
                $"Unable to work out which binary the Rust app '{resourceName}' produces: 'cargo metadata' reported " +
                $"{runnablePackages.Count} default workspace members with a binary target. Call WithCargoPackage(\"<name>\") to select one. " +
                $"Available packages: {FormatPackageNames(metadata)}.")
        };
    }

    private static string FormatPackageNames(CargoMetadata metadata)
        => metadata.Packages is { Count: > 0 } packages
            ? string.Join(", ", packages.Select(static p => $"'{p.Name}'"))
            : "none";
}

View on GitHub (pinned to 25830f84bd)