{"record":{"id":"9aedcfc0f65afc03","repo":"microsoft/aspire","slug":"the-rust-app-resourcename-requested-the-cargo-package","errorCode":null,"errorMessage":"The Rust app '{resourceName}' requested the cargo package '{requestedPackage}' with WithCargoPackage, but 'cargo metadata' reported no such package. Available packages: {FormatPackageNames(metadata)}.","messagePattern":"The Rust app '(.+?)' requested the cargo package '(.+?)' with WithCargoPackage, but 'cargo metadata' reported no such package\\. Available packages: (.+?)\\.","errorType":"exception","errorClass":"DistributedApplicationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Rust/RustCargoTargetResolver.cs","lineNumber":167,"sourceCode":"            // user at WithCargoBinTarget would send them looking for a target that does not exist.\n            [] => throw new DistributedApplicationException(\n                $\"Unable to work out which binary the Rust app '{resourceName}' produces: the package '{package.Name}' declares no \" +\n                $\"binary targets. Point the app directory at a package with a binary, or select one with WithCargoPackage(\\\"<name>\\\").\"),\n            var many => throw new DistributedApplicationException(\n                $\"Unable to work out which binary the Rust app '{resourceName}' produces: the package '{package.Name}' declares \" +\n                $\"{many.Count} binary targets. Call WithCargoBinTarget(\\\"<name>\\\") to select one.\")\n        };\n    }\n\n    private static CargoPackage ResolvePackage(CargoMetadata metadata, string? requestedPackage, string resourceName)\n    {\n        if (requestedPackage is not null)\n        {\n            // Reported here rather than left to cargo because this runs before any build: the debugger needs\n            // the executable path up front, so a typo would otherwise surface as an unexplained\n            // \"Sequence contains no matching element\" from LINQ.\n            return metadata.Packages.FirstOrDefault(p => p.Name == requestedPackage)\n                ?? throw new DistributedApplicationException(\n                    $\"The Rust app '{resourceName}' requested the cargo package '{requestedPackage}' with WithCargoPackage, but \" +\n                    $\"'cargo metadata' reported no such package. Available packages: {FormatPackageNames(metadata)}.\");\n        }\n\n        var defaultPackages = metadata.Packages.Where(p => metadata.DefaultMemberIds.Contains(p.Id)).ToList();\n\n        if (defaultPackages is [var onlyMember])\n        {\n            return onlyMember;\n        }\n\n        // `cargo run` only needs one *runnable* member, so the common workspace shape of an app crate beside\n        // library crates runs fine. Library-only members are dropped before the choice is called ambiguous.\n        var runnablePackages = defaultPackages.Where(static p => p.BinTargetNames.Count > 0).ToList();\n\n        return runnablePackages switch\n        {\n            [var single] => single,","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Rust/RustCargoTargetResolver.cs#L149-L185","documentation":"ResolvePackage throws this DistributedApplicationException when WithCargoPackage named a package that 'cargo metadata' does not report in the workspace. The check runs before any build because the debugger needs the executable path up front; otherwise the typo would surface later as an opaque LINQ 'Sequence contains no matching element' error. The message lists all available package names to make the typo obvious.","triggerScenarios":"Calling .WithCargoPackage(\"<name>\") with a name that does not exactly match any package Name reported by 'cargo metadata' for the app's workspace — typos, wrong casing, or referencing a package outside the workspace.","commonSituations":"Typo in the package name passed to WithCargoPackage; the package lives in a different workspace/repository than the app directory; renaming the crate in Cargo.toml without updating the resource configuration; case mismatch since the comparison is exact.","solutions":["Correct the name passed to WithCargoPackage to exactly match a package listed in the error message","Run 'cargo metadata --no-deps' in the app directory to see the actual package names","Ensure the resource's app directory points at the workspace that actually contains the requested package"],"exampleFix":"// before\nbuilder.AddRustApp(\"app\", \"../rust-workspace\")\n       .WithCargoPackage(\"my-server\"); // actual package is \"my_server\"\n// after\nbuilder.AddRustApp(\"app\", \"../rust-workspace\")\n       .WithCargoPackage(\"my_server\");","handlingStrategy":"validation","validationCode":"var names = GetCargoPackageNames(appDir);\nif (!names.Contains(requestedPackage)) throw new InvalidOperationException($\"Package '{requestedPackage}' not in workspace. Available: {string.Join(\", \", names)}\");","typeGuard":"bool PackageExists(CargoMetadata m, string name) => m.Packages.Any(p => p.Name == name);","tryCatchPattern":"try { builder.AddRustApp(\"app\", dir).WithCargoPackage(pkg); } catch (DistributedApplicationException ex) when (ex.Message.Contains(\"reported no such package\")) { log.LogError(ex, \"Unknown cargo package\"); }","preventionTips":["Copy package names exactly from 'cargo metadata --no-deps' output; watch for hyphens vs underscores","Update WithCargoPackage names whenever a crate is renamed in Cargo.toml","Confirm the app directory points at the workspace containing the package"],"tags":["rust","cargo","package-not-found","typo"],"backgroundTag":"entity-not-found","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}