{"record":{"id":"4dab76f87adddee3","repo":"hashicorp/terraform","slug":"error-selecting-workspace-s","errorCode":null,"errorMessage":"Error selecting workspace: %s","messagePattern":"Error selecting workspace: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/init_run.go","lineNumber":316,"sourceCode":"\t\t\treturn 1\n\t\t}\n\t}\n\tproviderHook := &providerPolicyHook{\n\t\tclient:     policyClient,\n\t\tview:       view,\n\t\trootModule: rootModEarly,\n\t}\n\n\tvar state *states.State\n\n\t// If we have a functional backend (either just initialized or initialized\n\t// on a previous run) we'll use the current state as a potential source\n\t// of provider dependencies.\n\tif back != nil {\n\t\tc.ignoreRemoteVersionConflict(back)\n\t\tworkspace, err := c.Workspace()\n\t\tif err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error selecting workspace: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\t\tsMgr, sDiags := back.StateMgr(workspace)\n\t\tif sDiags.HasErrors() {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error loading state: %s\", sDiags.Err()))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tif err := sMgr.RefreshState(); err != nil {\n\t\t\tdiags = diags.Append(fmt.Errorf(\"Error refreshing state: %s\", err))\n\t\t\tview.Diagnostics(diags)\n\t\t\treturn 1\n\t\t}\n\n\t\tstate = sMgr.State()\n\t}","sourceCodeStart":298,"sourceCodeEnd":334,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/init_run.go#L298-L334","documentation":"Thrown when c.Workspace() returns an error during init after a backend is initialized/available. Workspace() resolves the active workspace (from TF_WORKSPACE env, .terraform/environment, or CLI selection) and can fail if the metadata file is unreadable or the selected workspace is invalid.","triggerScenarios":"Backend is configured but the workspace cannot be resolved: corrupted .terraform/environment file, invalid workspace name, or workspace selection logic fails.","commonSituations":".terraform/environment contains an invalid/removed workspace name; stale state from switching branches; permission error reading the environment marker file.","solutions":["Inspect .terraform/environment: it should contain a valid workspace name (or be absent).","Run `terraform workspace select default` (or a valid workspace) to reset the active workspace.","Remove a corrupted .terraform/environment file and re-run init."],"exampleFix":"$ terraform workspace select default && terraform init","handlingStrategy":"validation","validationCode":"// Validate the active workspace marker before init.\nfunc validWorkspace() error {\n    b, err := os.ReadFile(\".terraform/environment\")\n    if err != nil { return nil } // absent is fine\n    name := strings.TrimSpace(string(b))\n    if name == \"\" { return errors.New(\"empty workspace marker\") }\n    return nil\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Run `terraform workspace select <name>` before init to ensure a valid workspace.","Delete corrupted .terraform/environment files.","Avoid switching branches that leave stale workspace markers."],"tags":["terraform-init","workspace","backend","state"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}