{"record":{"id":"3b2ad9df07e8921d","repo":"microsoft/aspire","slug":"foundry-cli-command-formatcommand-arguments-failed-with-exit","errorCode":null,"errorMessage":"Foundry CLI command '{FormatCommand(arguments)}' failed with exit code {result.ExitCode}: {result.Error}{result.Output}","messagePattern":"Foundry CLI command '(.+?)' failed with exit code (.+?): (.+?)(.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/FoundryLocalService.cs","lineNumber":364,"sourceCode":"        }\n    }\n\n    private static async Task<string> RunFoundryCommandAsync(\n        string[] arguments,\n        Action<string>? onOutput,\n        CancellationToken cancellationToken,\n        bool stopReadingAfterProcessExit = false,\n        Func<string, bool>? outputCompletionPredicate = null)\n    {\n        var result = await RunFoundryCommandCoreAsync(\n            arguments,\n            onOutput,\n            cancellationToken,\n            stopReadingAfterProcessExit,\n            outputCompletionPredicate).ConfigureAwait(false);\n        if (result.ExitCode != 0)\n        {\n            throw new InvalidOperationException($\"Foundry CLI command '{FormatCommand(arguments)}' failed with exit code {result.ExitCode}: {result.Error}{result.Output}\");\n        }\n\n        return result.Output;\n    }\n\n    private static async Task<FoundryCommandResult> RunFoundryCommandCoreAsync(\n        string[] arguments,\n        Action<string>? onOutput,\n        CancellationToken cancellationToken,\n        bool stopReadingAfterProcessExit = false,\n        Func<string, bool>? outputCompletionPredicate = null)\n    {\n        using var process = new Process\n        {\n            StartInfo = CreateFoundryStartInfo(arguments)\n        };\n\n        return await RunProcessAsync(","sourceCodeStart":346,"sourceCodeEnd":382,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/FoundryLocalService.cs#L346-L382","documentation":"Aspire's FoundryLocalService shells out to the `foundry` CLI (via RunFoundryCommandAsync) for operations like loading models, checking model status, and starting/stopping the local service. When the CLI process exits with a non-zero exit code, the library wraps the command line, exit code, and captured stderr/stdout in this InvalidOperationException. It means the Foundry CLI itself rejected or failed the requested operation, not that Aspire failed internally.","triggerScenarios":"Any call path that runs a foundry CLI command returns a non-zero exit code: LoadModelAsync (foundry model load), IsModelLoadedAsync (foundry model info), GetModelIdAsync, StartAsync/StartCliServerAsync help/version probes, and StopAsync. The exception message embeds FormatCommand(arguments), the exit code, and the CLI's Error+Output text.","commonSituations":"The Foundry Local CLI is not installed or an older version lacks the invoked subcommand; the requested model name/alias does not exist locally or in the catalog; the Foundry service/daemon is not running or not accepting requests; the model download failed (disk space, network); or the user is not logged in / lacks entitlement for the model.","solutions":["Read the CLI's Error text embedded in the exception message and run the same `foundry ...` command manually in a terminal to see the full failure.","Verify the Foundry Local CLI is installed and current: `foundry --version`; install/update from https://learn.microsoft.com/azure/ai-foundry/foundry-local/","Confirm the model name/alias exists: `foundry model list` and `foundry model search <name>`; fix the model name passed to AddFoundryLocalModel.","Ensure the Foundry local service is running and healthy (`foundry service status`, start it if needed) before the app host loads models.","Check disk space and network access if the failure happens during model download, and re-run the load."],"exampleFix":"// before: model alias typo surfaces as CLI exit-code failure\nvar foundry = builder.AddFoundryLocal(\"foundry\")\n    .AddModel(\"phi-4-mini-q4-GS\", \"my-model\");\n\n// after: use a valid alias from `foundry model list`\nvar foundry = builder.AddFoundryLocal(\"foundry\")\n    .AddModel(\"phi-4-mini\", \"my-model\");","handlingStrategy":"try-catch","validationCode":"// Before starting the AppHost, verify the CLI and model are usable:\n//   foundry --version && foundry model list | grep <modelName>\nvar psi = new ProcessStartInfo(\"foundry\", $\"model list\") { RedirectStandardOutput = true, UseShellExecute = false };\nusing var p = Process.Start(psi)!;\nvar list = await p.StandardOutput.ReadToEndAsync();\nbool modelAvailable = list.Contains(\"phi-4-mini\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await modelResource.WaitForModelReadyAsync(...);\n}\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Foundry CLI command\"))\n{\n    logger.LogError(ex, \"Foundry CLI failed: {Message}\", ex.Message);\n    // surface remediation steps (install CLI, fix model name, start service)\n}","preventionTips":["Install and keep the Foundry Local CLI up to date before running the AppHost.","Validate model names against `foundry model list` / `foundry model search` instead of hardcoding guesses.","Run the exact CLI command manually first when a new model or CLI version is involved.","Ensure the Foundry service is running before app-host startup in scripts and CI."],"tags":["foundry","cli","process-exit-code","model-loading"],"backgroundTag":"command-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"}