{"record":{"id":"b545479da835a1de","repo":"hashicorp/terraform","slug":"couldn-t-create-initial-workspace-w","errorCode":null,"errorMessage":"Couldn't create initial workspace: %w","messagePattern":"Couldn't create initial workspace: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/meta_backend.go","lineNumber":262,"sourceCode":"\t}\n\tif diags.HasErrors() {\n\t\treturn fmt.Errorf(\"Failed to get existing workspaces: %s\", diags.Err())\n\t}\n\tif diags.HasWarnings() {\n\t\tlog.Printf(\"[WARN] selectWorkspace: warning(s) returned when getting workspaces: %s\", diags.ErrWithWarnings())\n\t}\n\tif len(workspaces) == 0 {\n\t\tif c, ok := b.(*cloud.Cloud); ok && m.input {\n\t\t\t// len is always 1 if using Name; 0 means we're using Tags and there\n\t\t\t// aren't any matching workspaces. Which might be normal and fine, so\n\t\t\t// let's just ask:\n\t\t\tname, err := m.UIInput().Input(context.Background(), &terraform.InputOpts{\n\t\t\t\tId:          \"create-workspace\",\n\t\t\t\tQuery:       \"\\n[reset][bold][yellow]No workspaces found.[reset]\",\n\t\t\t\tDescription: fmt.Sprintf(inputCloudInitCreateWorkspace, c.WorkspaceMapping.DescribeTags()),\n\t\t\t})\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"Couldn't create initial workspace: %w\", err)\n\t\t\t}\n\t\t\tname = strings.TrimSpace(name)\n\t\t\tif name == \"\" {\n\t\t\t\treturn fmt.Errorf(\"Couldn't create initial workspace: no name provided\")\n\t\t\t}\n\t\t\tlog.Printf(\"[TRACE] Meta.selectWorkspace: selecting the new HCP Terraform workspace requested by the user (%s)\", name)\n\t\t\treturn m.SetWorkspace(name)\n\t\t} else {\n\t\t\treturn &errBackendNoExistingWorkspaces{}\n\t\t}\n\t}\n\n\t// Get the currently selected workspace.\n\tworkspace, err := m.Workspace()\n\tif err != nil {\n\t\treturn err\n\t}\n","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/meta_backend.go#L244-L280","documentation":"Returned from `Meta.selectWorkspace` when the backend is `*cloud.Cloud`, no workspaces matched, interactive input is enabled, and `m.UIInput().Input` (the prompt asking the user to name a new workspace) itself returns an error. The `%w` wrap preserves the original error for `errors.Is`/`errors.As` unwrapping. This is an input-mechanism failure, not a workspace problem.","triggerScenarios":"`UIInput().Input` fails during the 'No workspaces found' prompt — stdin is closed, the terminal is not a TTY, an interrupt occurs, or the UIInput implementation errors internally. The cloud branch is entered only when `len(workspaces) == 0` and `m.input` is true.","commonSituations":"First `terraform` run against a brand-new HCP Terraform organization with zero workspaces, in a non-interactive context (CI, container) where the create prompt cannot be answered; an interrupt during the prompt; stdin redirected from a closed pipe.","solutions":["Pre-create the workspace in HCP Terraform so the prompt is not triggered, then re-run.","Set `workspaces.name = \"<explicit>\"` in the `cloud` block to avoid the 'No workspaces found' path.","Run in a real TTY so the interactive prompt can be answered.","If automating, set `-input=false` and specify the workspace explicitly (the cloud branch requires `m.input`)."],"exampleFix":"// before: zero workspaces + non-interactive -> prompt fails\ncloud { organization = \"acme\" workspaces { tags = [\"env:prod\"] } }\n// after: name the workspace explicitly\ncloud { organization = \"acme\" workspaces { name = \"prod\" } }\nterraform init","handlingStrategy":"validation","validationCode":"// Avoid the create-workspace prompt by ensuring >=1 workspace matches,\n// or by setting workspaces.name explicitly.\nif nonInteractive && len(workspaces) == 0 {\n    return errors.New(\"no workspaces and input disabled; set cloud.workspaces.name or pre-create a workspace\")\n}","typeGuard":"null","tryCatchPattern":"// UIInput errors during the create prompt are not retryable; pre-create the workspace.\nif err != nil {\n    return fmt.Errorf(\"Couldn't create initial workspace: %w\", err)\n}","preventionTips":["Pre-create at least one workspace in new HCP Terraform organizations.","Set `workspaces.name` explicitly in the `cloud` block for CI.","Run the first `terraform init` interactively to handle onboarding prompts."],"tags":["terraform","cloud","workspace","input","interactive","onboarding"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}