{"record":{"id":"cad80f457093a47d","repo":"hashicorp/terraform","slug":"invalid-workspace-name-the-selected-workspace-des","errorCode":null,"errorMessage":"Invalid workspace name: The selected workspace described in %q has an invalid name. This suggests that the file contents were edited by something other than Terraform. To select a different, valid workspace name use commands `terraform workspace select` or `terraform workspace select -or-create`.","messagePattern":"Invalid workspace name: The selected workspace described in %q has an invalid name\\. This suggests that the file contents were edited by something other than Terraform\\. To select a different, valid workspace name use commands `terraform workspace select` or `terraform workspace select -or-create`\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta.go","lineNumber":811,"sourceCode":"// whether the workspace is set by ENV or not.\nfunc (m *Meta) WorkspaceOverridden() (string, bool, error) {\n\tif envVar := os.Getenv(WorkspaceNameEnvVar); envVar != \"\" {\n\t\tif !validWorkspaceName(envVar) {\n\t\t\t// Protect against invalid workspace names set via ENV.\n\t\t\treturn \"\", true, errInvalidWorkspaceNameEnvVar\n\t\t}\n\t\treturn envVar, true, nil\n\t}\n\n\tenvData, err := os.ReadFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile))\n\tcurrent := string(bytes.TrimSpace(envData))\n\tif current == \"\" {\n\t\tcurrent = backend.DefaultStateName\n\t}\n\tif !m.bypassWorkspaceNameValidityCheck && !validWorkspaceName(current) {\n\t\t// This check is active in every command that uses a backend.\n\t\t// It is selectively disabled in commands that are recommended for recovering from an invalid workspace.\n\t\terr := fmt.Errorf(\"Invalid workspace name: The selected workspace described in %q has an invalid name. This suggests that the file contents were edited by something other than Terraform. To select a different, valid workspace name use commands `terraform workspace select` or `terraform workspace select -or-create`.\", filepath.Join(m.DataDir(), local.DefaultWorkspaceFile))\n\t\treturn \"\", false, err\n\t}\n\n\tif err != nil && !os.IsNotExist(err) {\n\t\t// always return the default if we can't get a workspace name\n\t\tlog.Printf(\"[ERROR] failed to read current workspace: %s\", err)\n\t}\n\n\treturn current, false, nil\n}\n\n// SetWorkspace saves the given name as the current workspace in the local\n// filesystem.\nfunc (m *Meta) SetWorkspace(name string) error {\n\terr := os.MkdirAll(m.DataDir(), 0755)\n\tif err != nil {\n\t\treturn err\n\t}","sourceCodeStart":793,"sourceCodeEnd":829,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta.go#L793-L829","documentation":"Returned from `WorkspaceOverridden` when the on-disk workspace file (`.terraform/terraform.tfstate.d/default` workspace marker, i.e. `local.DefaultWorkspaceFile` under `m.DataDir()`) contains a name that fails `validWorkspaceName`. The message explicitly warns that the file was likely hand-edited, because Terraform itself only ever writes valid names there.","triggerScenarios":"`os.ReadFile(filepath.Join(m.DataDir(), local.DefaultWorkspaceFile))` succeeds, the trimmed content is non-empty (so not default), but `validWorkspaceName(current)` is false. The `bypassWorkspaceNameValidityCheck` flag is false (it is only enabled for recovery commands like `workspace select`).","commonSituations":"A user or script manually edited `.terraform/tfstack/default` / the workspace marker file; a sync tool corrupted the file; the file was checked into version control with an invalid value; partial filesystem corruption after a crash.","solutions":["Run `terraform workspace select <valid-name>` (or `-or-create`) — recovery commands set `bypassWorkspaceNameValidityCheck` and overwrite the file.","If no valid workspace is intended, delete the marker file under `.terraform/` so Terraform falls back to `default`.","Inspect the file content: `cat .terraform/<workspace-marker>` to see the invalid value.","Remove the file from version control if it was accidentally committed."],"exampleFix":"# before: file contains 'feat/x' (invalid)\n# recover\nterraform workspace select -or-create=true prod\n# or remove the marker\nrm -f .terraform/tfstack/default\n# next terraform command will use the default workspace","handlingStrategy":"validation","validationCode":"// Validate the workspace marker file contents before running.\ndata, _ := os.ReadFile(filepath.Join(dataDir, local.DefaultWorkspaceFile))\nname := strings.TrimSpace(string(data))\nif name != \"\" && !arguments.ValidWorkspaceName(name) {\n    // delete it or run `terraform workspace select` to recover\n}","typeGuard":"func validWs(s string) bool { return arguments.ValidWorkspaceName(s) }","tryCatchPattern":"// Use recovery commands (workspace select) which set bypassWorkspaceNameValidityCheck.\n// They overwrite the marker with a valid name.","preventionTips":["Never hand-edit `.terraform/` workspace marker files.","Do not commit `.terraform/` to version control.","If the marker is suspect, delete it and let Terraform reset to default."],"tags":["terraform","workspace","filesystem","validation","recovery"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}