{"record":{"id":"36ed84a8a52cb9de","repo":"hashicorp/terraform","slug":"invalid-workspace-name-set-using-s","errorCode":null,"errorMessage":"Invalid workspace name set using %s","messagePattern":"Invalid workspace name set using (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta.go","lineNumber":773,"sourceCode":"\t\tif interval, err := strconv.Atoi(val); err == nil && interval > DefaultStatePersistInterval {\n\t\t\t// The user-specified interval must be greater than the default minimum\n\t\t\treturn interval\n\t\t} else if err != nil {\n\t\t\tlog.Printf(\"[ERROR] Can't parse state persist interval %q of environment variable %q\", val, StatePersistIntervalEnvVar)\n\t\t}\n\t}\n\treturn DefaultStatePersistInterval\n}\n\n// WorkspaceNameEnvVar is the name of the environment variable that can be used\n// to set the name of the Terraform workspace, overriding the workspace chosen\n// by `terraform workspace select`.\n//\n// Note that this environment variable is ignored by `terraform workspace new`\n// and `terraform workspace delete`.\nconst WorkspaceNameEnvVar = \"TF_WORKSPACE\"\n\nvar errInvalidWorkspaceNameEnvVar = fmt.Errorf(\"Invalid workspace name set using %s\", WorkspaceNameEnvVar)\n\n// Workspace returns the name of the currently configured workspace, corresponding\n// to the desired named state.\n//\n// Workspace names are validated via use of the `WorkspaceOverridden` method.\nfunc (m *Meta) Workspace() (string, error) {\n\tcurrent, _, err := m.WorkspaceOverridden()\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\treturn current, nil\n}\n\n// WorkspaceOverridden returns the name of the currently configured workspace,\n// corresponding to the desired named state, as well as a bool saying whether\n// this was set via the TF_WORKSPACE environment variable.\n//\n// The method also validates the workspace name. If it's invalid, an error is","sourceCodeStart":755,"sourceCodeEnd":791,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta.go#L755-L791","documentation":"Defined as the package-level `errInvalidWorkspaceNameEnvVar` sentinel and returned from `WorkspaceOverridden` when the `TF_WORKSPACE` environment variable is set to a value that fails `validWorkspaceName`. The error message interpolates the env var name (`TF_WORKSPACE`) so the user knows exactly which variable to fix. Because it is a package var (not created inline), callers can compare against it by reference.","triggerScenarios":"`os.Getenv(\"TF_WORKSPACE\")` returns a non-empty string, and `arguments.ValidWorkspaceName(envVar)` returns false. `validWorkspaceName` rejects names that require path escaping or contain characters unsafe for filesystem paths / state identifiers (slashes, dots, etc., per the `arguments` package rules).","commonSituations":"Export `TF_WORKSPACE=feature/x` (contains a slash); `TF_WORKSPACE=my.ws` or other punctuation; a CI pipeline sets `TF_WORKSPACE` from a git branch name verbatim; copy-paste introduces a leading/trailing space or quote.","solutions":["Set `TF_WORKSPACE` to a value containing only letters, digits, dashes, and underscores (no slashes, dots, or spaces).","If you need a branch-derived name, sanitize it first: `export TF_WORKSPACE=$(echo \"$BRANCH\" | tr '/.' '__')`.","Unset the variable (`unset TF_WORKSPACE`) and select the workspace explicitly with `terraform workspace select`.","Check `.env` files or CI variable settings for stray characters."],"exampleFix":"# before\nexport TF_WORKSPACE=\"feature/login\"\n# after\nexport TF_WORKSPACE=\"feature_login\"","handlingStrategy":"validation","validationCode":"// Validate TF_WORKSPACE before any terraform command.\nif name := os.Getenv(\"TF_WORKSPACE\"); name != \"\" && !arguments.ValidWorkspaceName(name) {\n    return fmt.Errorf(\"TF_WORKSPACE=%q is invalid; use letters, digits, '-', '_' only\", name)\n}","typeGuard":"// arguments.ValidWorkspaceName is the canonical guard.\nfunc validWs(s string) bool { return arguments.ValidWorkspaceName(s) }","tryCatchPattern":"// Compare against the sentinel for targeted handling.\nif errors.Is(err, errInvalidWorkspaceNameEnvVar) {\n    // tell the user to fix/unset TF_WORKSPACE\n}","preventionTips":["Sanitize branch names before assigning to `TF_WORKSPACE`.","Document allowed workspace-name characters for your team.","Prefer `terraform workspace select` over the env var in scripts."],"tags":["terraform","workspace","env-var","validation","config"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}