{"record":{"id":"61e3c194fbfdf110","repo":"hashicorp/terraform","slug":"failed-to-set-new-workspace-s","errorCode":null,"errorMessage":"Failed to set new workspace: %s","messagePattern":"Failed to set new workspace: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend_migrate.go","lineNumber":324,"sourceCode":"\t\t\t\t\treturn nil, err\n\t\t\t\t}\n\n\t\t\t\t// Update the name of the destination state.\n\t\t\t\topts.destinationWorkspace = name\n\n\t\t\t\tdestinationState, sDiags := opts.Destination.StateMgr(opts.destinationWorkspace)\n\t\t\t\tif sDiags.HasErrors() {\n\t\t\t\t\treturn nil, sDiags.Err()\n\t\t\t\t}\n\n\t\t\t\t// Ignore invalid workspace name as it is irrelevant in this context.\n\t\t\t\tworkspace, _ := m.Workspace()\n\n\t\t\t\t// If the currently selected workspace is the default workspace, then set\n\t\t\t\t// the named workspace as the new selected workspace.\n\t\t\t\tif workspace == backend.DefaultStateName {\n\t\t\t\t\tif err := m.SetWorkspace(opts.destinationWorkspace); err != nil {\n\t\t\t\t\t\treturn nil, fmt.Errorf(\"Failed to set new workspace: %s\", err)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn destinationState, nil\n\t\t\t}()\n\t\t} else {\n\t\t\t// For any other error, return it immediately to avoid nil pointer dereference\n\t\t\treturn fmt.Errorf(strings.TrimSpace(\n\t\t\t\terrMigrateSingleLoadDefault), opts.DestinationType, dstWord, sDiags.Err())\n\t\t}\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(strings.TrimSpace(\n\t\t\terrMigrateSingleLoadDefault), opts.DestinationType, err)\n\t}\n\tif err := destinationState.RefreshState(); err != nil {\n\t\treturn fmt.Errorf(strings.TrimSpace(\n\t\t\terrMigrateSingleLoadDefault), opts.DestinationType, err)","sourceCodeStart":306,"sourceCodeEnd":342,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend_migrate.go#L306-L342","documentation":"Returned during single-to-single migration when the destination workspace cannot be made the active workspace after the destination state manager is constructed. The error is wrapped from `m.SetWorkspace(opts.destinationWorkspace)` and surfaced as a tuple `(nil, error)` from an immediately-invoked closure.","triggerScenarios":"Fires inside the `func() (statemgr.Full, error) {...}()` closure in `backendMigrateState_s_s` when the current workspace equals `backend.DefaultStateName` and `m.SetWorkspace(opts.destinationWorkspace)` returns an error — typically because the local workspace metadata store cannot persist the selection.","commonSituations":"A read-only working directory, filesystem permissions on the local `.terraform/` workspace marker, or an invalid destination workspace name rejected by the local workspace backend.","solutions":["Ensure the working directory is writable by the user running `terraform init`.","Validate `opts.destinationWorkspace` is a legal workspace name for the destination backend.","Retry after fixing filesystem permissions on `.terraform/terraform.tfstate` and the workspace marker."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"# Ensure the working directory and workspace marker are writable.\ntouch .terraform/.workspace-write-test && rm .terraform/.workspace-write-test \\\n  || echo \"working dir not writable by $(whoami)\"\nterraform init","typeGuard":null,"tryCatchPattern":"# Fail with an actionable message when workspace selection breaks:\nif ! terraform init >/tmp/init.log 2>&1; then\n  grep -q 'Failed to set new workspace' /tmp/init.log \\\n    && { echo 'Check .terraform/ write perms and workspace name validity' >&2; exit 2; }\n  cat /tmp/init.log; exit 1\nfi","preventionTips":["Run terraform as a user with write access to the working directory.","Avoid read-only bind-mounts of the working directory in containers.","Validate workspace names against the destination backend's naming rules."],"tags":["backend","migration","workspace","filesystem","permissions","init"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}