{"record":{"id":"05067293de9581b6","repo":"hashicorp/terraform","slug":"failed-to-request-username-s","errorCode":null,"errorMessage":"Failed to request username: %s","messagePattern":"Failed to request username: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/command/login.go","lineNumber":552,"sourceCode":"func (c *LoginCommand) interactiveGetTokenByPassword(hostname svchost.Hostname, credsCtx *loginCredentialsContext, clientConfig *disco.OAuthClient) (*oauth2.Token, tfdiags.Diagnostics) {\n\tvar diags tfdiags.Diagnostics\n\n\tconfirm, confirmDiags := c.interactiveContextConsent(hostname, disco.OAuthOwnerPasswordGrant, credsCtx)\n\tdiags = diags.Append(confirmDiags)\n\tif !confirm {\n\t\tdiags = diags.Append(errors.New(\"Login cancelled\"))\n\t\treturn nil, diags\n\t}\n\n\tc.Ui.Output(\"\\n---------------------------------------------------------------------------------\\n\")\n\tc.Ui.Output(\"Terraform must temporarily use your password to request an API token.\\nThis password will NOT be saved locally.\\n\")\n\n\tusername, err := c.UIInput().Input(context.Background(), &terraform.InputOpts{\n\t\tId:    \"username\",\n\t\tQuery: fmt.Sprintf(\"Username for %s:\", hostname.ForDisplay()),\n\t})\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to request username: %s\", err))\n\t\treturn nil, diags\n\t}\n\tpassword, err := c.UIInput().Input(context.Background(), &terraform.InputOpts{\n\t\tId:     \"password\",\n\t\tQuery:  fmt.Sprintf(\"Password for %s:\", hostname.ForDisplay()),\n\t\tSecret: true,\n\t})\n\tif err != nil {\n\t\tdiags = diags.Append(fmt.Errorf(\"Failed to request password: %s\", err))\n\t\treturn nil, diags\n\t}\n\n\toauthConfig := &oauth2.Config{\n\t\tClientID: clientConfig.ID,\n\t\tEndpoint: clientConfig.Endpoint(),\n\t\tScopes:   clientConfig.Scopes,\n\t}\n\ttoken, err := oauthConfig.PasswordCredentialsToken(context.Background(), username, password)","sourceCodeStart":534,"sourceCodeEnd":570,"githubUrl":"https://github.com/hashicorp/terraform/blob/d32a084675427f5ac3f7d2868578ef8b2c1dc525/internal/command/login.go#L534-L570","documentation":"Thrown by the password-grant login flow in internal/command/login.go when c.UIInput().Input fails for the username prompt. UIInput abstracts the interactive prompt (CLI, mock, etc.); an error here means the prompt itself could not be issued or read, not that the credentials are wrong. Common when the process has no usable TTY.","triggerScenarios":"Running `terraform login` in a non-interactive environment (CI, container, pipe) with no TTY; the UIInput backend errored (EOF on stdin, cancelled context); a custom UIInput implementation returned an error.","commonSituations":"CI/CD pipelines or Docker runs that invoke `terraform login` instead of providing a token out-of-band; stdin redirected from /dev/null or a closed pipe; interrupted prompt (Ctrl-C/SIGINT).","solutions":["Provide credentials non-interactively: set a `TF_TOKEN_<hostname>` environment variable, or write the token to the credentials file (~/.terraform.d/credentials.tfrc.json).","Run `terraform login` in a real interactive terminal (allocate a TTY).","If automating, use the OAuth flow triggered by visiting the printed URL rather than the username/password prompt.","Ensure stdin is a TTY and not redirected when interactive login is required."],"exampleFix":"# before: terraform login run in CI with no TTY -> 'Failed to request username'\n\n# after: provide the token out-of-band\nexport TF_TOKEN_app_terraform_io=\"<token>\"\nterraform init","handlingStrategy":"validation","validationCode":"// Skip the interactive login path entirely when no TTY is present.\nimport \"os\"\n\nfunc canPrompt() bool {\n    fi, err := os.Stdin.Stat()\n    if err != nil {\n        return false\n    }\n    return (fi.Mode() & os.ModeCharDevice) != 0\n}\n\nif !canPrompt() {\n    return errors.New(\"no TTY: set TF_TOKEN_<hostname> or populate ~/.terraform.d/credentials.tfrc.json instead of 'terraform login'\")\n}","typeGuard":null,"tryCatchPattern":"username, err := c.UIInput().Input(ctx, opts)\nif err != nil {\n    if !canPrompt() {\n        return fmt.Errorf(\"login requires a TTY; set TF_TOKEN_%s or run in an interactive terminal: %w\", hostname, err)\n    }\n    return err\n}","preventionTips":["In CI, supply credentials via TF_TOKEN_<hostname> env vars or the credentials file; never call `terraform login`.","When login is needed, run it in a TTY-capable terminal.","Detect non-TTY up front and fail with an actionable message before prompting."],"tags":["terraform","login","auth","ui-input","tty","interactive"],"backgroundTag":null,"analyzedSha":"d32a084675427f5ac3f7d2868578ef8b2c1dc525","analyzedAt":"2026-08-11T18:43:52.779Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}