{"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/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta.go#L793-L829","documentation":"Returned by WorkspaceOverridden() when the on-disk workspace marker file (.terraform/environment by default) contains a workspace name that fails validWorkspaceName(). The message explicitly suspects the file was hand-edited, because Terraform only ever writes valid names to it. This guard is active in every backend-using command and can be selectively bypassed via bypassWorkspaceNameValidityCheck for recovery commands.","triggerScenarios":"The file at DataDir()/DefaultWorkspaceFile ('.terraform/environment') exists and its trimmed contents fail the workspace name validator. This occurs when an external tool, editor, or script corrupts the file contents.","commonSituations":"A sed/script that rewrote .terraform/environment with a slash-delimited name; a symlink or mount issue causing garbage contents; a merge conflict resolution that left conflict markers in the file; restoring a partial backup of .terraform/.","solutions":["Run `terraform workspace select <valid-name>` or `terraform workspace select <name> -or-create` to overwrite the corrupted file with a valid value.","Inspect .terraform/environment and manually restore a known-good workspace name if select commands also fail.","Delete the .terraform/environment file so Terraform falls back to the default workspace."],"exampleFix":"# before: .terraform/environment contains \"prod/us-east\"\nterraform plan\n# after\nterraform workspace select prod-us-east\nterraform plan","handlingStrategy":"validation","validationCode":"// Check the environment marker file before running terraform\nenvFile := filepath.Join(workdir, \".terraform\", \"environment\")\nif data, err := os.ReadFile(envFile); err == nil {\n    name := strings.TrimSpace(string(data))\n    if name != \"\" && !arguments.ValidWorkspaceName(name) {\n        log.Fatalf(\"%s contains invalid workspace name %q\", envFile, name)\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never hand-edit .terraform/environment; use terraform workspace commands.","Add .terraform/ to .gitignore so it isn't shared/corrupted across machines.","If recovering, delete the file rather than editing it."],"tags":["workspace","filesystem","corruption","environment-file"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}