{"record":{"id":"1403cc817019b691","repo":"docker/cli","slug":"invalid-stack-name-q","errorCode":null,"errorMessage":"invalid stack name: %q","messagePattern":"invalid stack name: %q","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/command/stack/common.go","lineNumber":20,"sourceCode":"\nimport (\n\t\"context\"\n\t\"fmt\"\n\t\"strings\"\n\t\"unicode\"\n\n\t\"github.com/docker/cli/cli/compose/convert\"\n\t\"github.com/docker/cli/opts\"\n\t\"github.com/moby/moby/client\"\n)\n\n// validateStackName checks if the provided string is a valid stack name (namespace).\n// It currently only does a rudimentary check if the string is empty, or consists\n// of only whitespace and quoting characters.\nfunc validateStackName(namespace string) error {\n\tv := strings.TrimFunc(namespace, quotesOrWhitespace)\n\tif v == \"\" {\n\t\treturn fmt.Errorf(\"invalid stack name: %q\", namespace)\n\t}\n\treturn nil\n}\n\nfunc validateStackNames(namespaces []string) error {\n\tfor _, ns := range namespaces {\n\t\tif err := validateStackName(ns); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc quotesOrWhitespace(r rune) bool {\n\treturn unicode.IsSpace(r) || r == '\"' || r == '\\''\n}\n\nfunc getStackFilter(namespace string) client.Filters {","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/docker/cli/blob/4f84911bfe8811e9b028e4b1fee8e7510be79387/cli/command/stack/common.go#L2-L38","documentation":"Raised by validateStackName when the provided stack name (namespace) is empty after trimming leading/trailing whitespace and quote characters. The stack name is used as a label namespace (com.docker.stack.namespace) to scope all stack resources, so an empty value would make resource lookup impossible.","triggerScenarios":"Invoking `docker stack <cmd> \"\"` or `docker stack <cmd> '   '` or `docker stack <cmd> '\"\"'` (a value that is empty after stripping spaces and quotes). validateStackName is called from ps.go, remove.go, deploy.go, and other stack subcommands before any API call.","commonSituations":"A shell variable that resolved to empty (e.g. $STACK_NAME unset) passed as the stack argument; quoting bugs where literal quotes become part of the argument; CI templates that interpolate an empty variable.","solutions":["Provide a non-empty stack name consisting of real characters, e.g. `docker stack deploy mystack -c compose.yml`.","Check the shell variable feeding the command: `${STACK_NAME:?STACK_NAME must be set}`.","Validate the name programmatically before calling the CLI (see validationCode).","Avoid wrapping the argument in extra quotes that survive into the arg."],"exampleFix":"// before\nSTACK_NAME=\"\"\ndocker stack deploy \"$STACK_NAME\" -c compose.yml\n// after\nSTACK_NAME=\"mystack\"\ndocker stack deploy \"$STACK_NAME\" -c compose.yml","handlingStrategy":"validation","validationCode":"// Validate a stack name before invoking the CLI\nimport (\n    \"strings\"\n    \"unicode\"\n)\n\nfunc isValidStackName(name string) bool {\n    trim := func(r rune) rune {\n        if unicode.IsSpace(r) || r == '\"' || r == '\\'' {\n            return -1\n        }\n        return r\n    }\n    return strings.Map(trim, name) != \"\"\n}","typeGuard":"type StackName string\n\nfunc (s StackName) Validate() error {\n    if strings.TrimSpace(strings.Trim(string(s), `\"'`)) == \"\" {\n        return errors.New(\"empty stack name\")\n    }\n    return nil\n}","tryCatchPattern":null,"preventionTips":["In shell, guard with `${STACK_NAME:?STACK_NAME must be set and non-empty}`.","Validate stack names at the config-loading boundary before passing to the CLI.","Add a unit test that empty/whitespace/quoted names are rejected."],"tags":["docker","stack","validation","namespace","argument"],"backgroundTag":null,"analyzedSha":"4f84911bfe8811e9b028e4b1fee8e7510be79387","analyzedAt":"2026-08-07T12:15:29.814Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}