{"record":{"id":"63dc0f73c55eabb6","repo":"microsoft/aspire","slug":"command-array-cannot-be-empty","errorCode":null,"errorMessage":"Command array cannot be empty.","messagePattern":"Command array cannot be empty\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting/ApplicationModel/Docker/DockerfileStage.cs","lineNumber":201,"sourceCode":"    public DockerfileStage Expose(int port)\n    {\n        ArgumentOutOfRangeException.ThrowIfNegativeOrZero(port);\n        _statements.Add(new DockerfileExposeStatement(port));\n        return this;\n    }\n\n    /// <summary>\n    /// Adds a CMD statement to set the default command.\n    /// </summary>\n    /// <param name=\"command\">The command to execute.</param>\n    /// <returns>The current stage.</returns>\n    [Experimental(\"ASPIREDOCKERFILEBUILDER001\", UrlFormat = \"https://aka.ms/aspire/diagnostics/{0}\")]\n    public DockerfileStage Cmd(string[] command)\n    {\n        ArgumentNullException.ThrowIfNull(command);\n        if (command.Length == 0)\n        {\n            throw new ArgumentException(\"Command array cannot be empty.\", nameof(command));\n        }\n        _statements.Add(new DockerfileCmdStatement(command));\n        return this;\n    }\n\n    /// <summary>\n    /// Adds an ENTRYPOINT statement to set the container entrypoint.\n    /// </summary>\n    /// <param name=\"command\">The entrypoint command to execute.</param>\n    /// <returns>The current stage.</returns>\n    [Experimental(\"ASPIREDOCKERFILEBUILDER001\", UrlFormat = \"https://aka.ms/aspire/diagnostics/{0}\")]\n    public DockerfileStage Entrypoint(string[] command)\n    {\n        ArgumentNullException.ThrowIfNull(command);\n        if (command.Length == 0)\n        {\n            throw new ArgumentException(\"Command array cannot be empty.\", nameof(command));\n        }","sourceCodeStart":183,"sourceCodeEnd":219,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting/ApplicationModel/Docker/DockerfileStage.cs#L183-L219","documentation":"DockerfileStage.Cmd emits a CMD instruction from a command array. Docker's CMD requires at least one token, so an empty array would produce an invalid Dockerfile line. The method throws ArgumentNullException for null and ArgumentException (\"Command array cannot be empty.\") for a zero-length array on the command parameter.","triggerScenarios":"stage.Cmd(Array.Empty<string>()), stage.Cmd(args.Where(...).ToArray()) where the filter matched nothing, or stage.Cmd(config[\"cmd\"].Split(' ')) resolving to an empty array.","commonSituations":"Building Dockerfiles programmatically where the command comes from options/config that was empty; filtering arguments with a predicate that removed all entries; splitting an empty string, which yields an array with one empty element or an empty array depending on options.","solutions":["Pass a non-empty command array, e.g. new[] { \"dotnet\", \"MyApp.dll\" }.","Validate the command source before calling; throw or substitute a default when empty.","If the CMD should be omitted, skip the call instead of passing an empty array."],"exampleFix":"// before\nvar args = extraArgs.Where(a => a.IsEnabled).ToArray();\nstage.Cmd(args); // may be empty\n// after\nvar args = extraArgs.Where(a => a.IsEnabled).ToArray();\nif (args.Length > 0) stage.Cmd(args);","handlingStrategy":"validation","validationCode":"if (command is null || command.Length == 0)\n    throw new ArgumentException(\"CMD requires at least one token.\", nameof(command));","typeGuard":null,"tryCatchPattern":"try\n{\n    stage.Cmd(command);\n}\ncatch (ArgumentException ex) when (ex.ParamName == nameof(command))\n{\n    logger.LogError(ex, \"Refusing to emit empty CMD in Dockerfile stage {Stage}\", stageName);\n    throw;\n}","preventionTips":["Never build the command array with filters that can legitimately return empty — check Length first.","Split strings with StringSplitOptions.RemoveEmptyEntries and validate the result.","Skip the Cmd() call when no command is intended rather than passing [].","Snapshot-test generated Dockerfiles so empty instructions fail review."],"tags":["dotnet","aspire","dockerfile","dockerfilebuilder"],"backgroundTag":"empty-required-field","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"}