{"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":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend_migrate.go","lineNumber":311,"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, 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":293,"sourceCodeEnd":329,"githubUrl":"https://github.com/hashicorp/terraform/blob/c9def3e214014c1188faabfc4a5bde5095139765/internal/command/meta_backend_migrate.go#L293-L329","documentation":"Returned inside the closure in backendMigrateState_s_s when the destination backend doesn't support a default workspace (ErrDefaultWorkspaceNotSupported), the user is prompted for a new name, and then m.SetWorkspace(opts.destinationWorkspace) fails at line 310. The migration was about to use a named workspace but the local workspace selector could not be switched.","triggerScenarios":"Destination backend (e.g., cloud/remote) requires named workspaces, the prompt succeeded yielding a name, but SetWorkspace rejected that name — invalid characters, name already in an inconsistent state, or the local workspace metadata file (.terraform/terraform.tfstate) is unwritable. Wrapped at line 311.","commonSituations":"User types a workspace name with invalid characters at the prompt; the .terraform directory is read-only or deleted between prompt and SetWorkspace; disk full; running in a container with a read-only mounted workspace directory.","solutions":["Inspect the inner %s for SetWorkspace's specific reason (invalid name, write error).","Ensure the .terraform/ directory is writable by the current user.","If the name was invalid, re-run init and provide a valid workspace name (alphanumeric, hyphens).","Free disk space if /tmp or the working directory is full.","Pre-create the named workspace in the destination and select it with 'terraform workspace select' before migrating."],"exampleFix":"// before: prompt returns 'my workspace' (space invalid)\n//   Failed to set new workspace: workspace name contains invalid characters\n// after: use a valid name\n//   (re-run init) -> enter 'my-workspace'","handlingStrategy":"validation","validationCode":"# Ensure .terraform is writable and pre-supply a valid named workspace.\nmkdir -p .terraform && touch .terraform/.writetest && rm .terraform/.writetest \\\n  || { echo '.terraform not writable'; exit 1; }\n# For cloud destinations, set an explicit workspace name to skip the prompt:\n# cloud { workspaces { name = \"prod\" } }","typeGuard":null,"tryCatchPattern":"# Detect the SetWorkspace failure and guide the operator.\nterraform init 2>/tmp/init.err || rc=$?\nif grep -q 'Failed to set new workspace' /tmp/init.err; then\n  echo 'SetWorkspace failed — provide a valid workspace name or fix .terraform perms' >&2\nfi\nexit ${rc:-0}","preventionTips":["Always configure an explicit workspace.name in cloud/remote blocks to avoid the prompt.","Ensure the working directory and .terraform/ are writable.","Validate workspace names: lowercase, alphanumerics, hyphens only.","Pre-create destination workspaces and 'terraform workspace select' before migrating."],"tags":["backend","state-migration","workspace-naming","set-workspace","named-workspace","terraform-init"],"analyzedSha":"c9def3e214014c1188faabfc4a5bde5095139765","analyzedAt":"2026-08-07T15:39:49.278Z","schemaVersion":2},"datasetVersion":"2026-08-07T21:17:07.882Z"}