{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta.go#L755-L791","documentation":"A package-level sentinel error (errInvalidWorkspaceNameEnvVar) returned by WorkspaceOverridden() when the TF_WORKSPACE environment variable is set to a value that fails validWorkspaceName(). Terraform deliberately refuses to proceed because an invalid workspace name would corrupt state addressing. The %s expands to the literal env var name TF_WORKSPACE.","triggerScenarios":"TF_WORKSPACE is exported with a value containing illegal characters or matching a reserved name. validWorkspaceName() (delegating to arguments.ValidWorkspaceName) rejects names that are empty, contain path separators, or equal reserved tokens like 'default'.","commonSituations":"A CI script sets TF_WORKSPACE to a branch name containing '/' or uppercase chars; a user typos TF_WORKSPACE='prod-data/region'; copying a value with spaces or special chars from a secret manager.","solutions":["Unset or correct TF_WORKSPACE so it contains only valid workspace name characters.","Validate the env var before running Terraform: ensure it matches the allowed workspace name pattern.","Use `terraform workspace select <name>` instead of TF_WORKSPACE when the name is dynamic."],"exampleFix":"// before\nexport TF_WORKSPACE=\"feature/auth-v2\"\n// after\nexport TF_WORKSPACE=\"feature-auth-v2\"","handlingStrategy":"validation","validationCode":"// Validate TF_WORKSPACE before invoking terraform\nname := os.Getenv(\"TF_WORKSPACE\")\nif name != \"\" && !arguments.ValidWorkspaceName(name) {\n    log.Fatalf(\"TF_WORKSPACE=%q is not a valid workspace name\", name)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set TF_WORKSPACE to values with '/', spaces, or reserved names like 'default' mangling.","Sanitize dynamic values (branch names) before assigning to TF_WORKSPACE.","Prefer `terraform workspace select` for dynamic workspace switching."],"tags":["workspace","environment","tf-workspace","validation"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T20:17:04.800Z"}