{"record":{"id":"2f8cae5fb32545cf","repo":"GoogleContainerTools/skaffold","slug":"failed-to-unmarshal-gcloud-command-result-into-acc","errorCode":null,"errorMessage":"failed to unmarshal gcloud command result into access token %v","messagePattern":"failed to unmarshal gcloud command result into access token (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/skaffold/gcp/auth.go","lineNumber":82,"sourceCode":"type token struct {\n\tToken string `json:\"token\"`\n}\n\ntype tokenSource struct {\n}\n\nfunc (ts tokenSource) Token() (*oauth2.Token, error) {\n\t// the command return a json object containing token\n\tcmd := exec.Command(\"gcloud\", \"auth\", \"print-access-token\", \"--format=json\")\n\tvar body bytes.Buffer\n\tcmd.Stdout = &body\n\terr := util.RunCmd(context.TODO(), cmd)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to get access token %v\", err)\n\t}\n\tvar t token\n\tif err := json.Unmarshal(body.Bytes(), &t); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to unmarshal gcloud command result into access token %v\", err)\n\t}\n\treturn &oauth2.Token{AccessToken: t.Token}, nil\n}\n\nfunc activeUserCredentialsOnce() (*google.Credentials, error) {\n\tcredsOnce.Do(func() {\n\t\tc, err := activeUserCredentials()\n\t\tif err != nil {\n\t\t\tlog.Entry(context.TODO()).Infof(\"unable to retrieve gcloud access token: %v\", err)\n\t\t\tlog.Entry(context.TODO()).Info(\"falling back to application default credentials\")\n\t\t\tcredsErr = fmt.Errorf(\"retrieving gcloud access token: %w\", err)\n\t\t\treturn\n\t\t}\n\t\tcreds = c\n\t})\n\n\treturn creds, credsErr\n}","sourceCodeStart":64,"sourceCodeEnd":100,"githubUrl":"https://github.com/GoogleContainerTools/skaffold/blob/a1189de023efc32d4b8e11f395acc678aa555011/pkg/skaffold/gcp/auth.go#L64-L100","documentation":"Fires in tokenSource.Token when the JSON emitted by `gcloud auth print-access-token --format=json` cannot be unmarshalled into the expected {token: ...} structure — gcloud produced unexpected output (version mismatch, error text on stdout, or login prompt).","triggerScenarios":"Calling Token when gcloud succeeds (exit 0) but prints non-JSON output — e.g. an old gcloud version lacking --format=json support for this command, warnings/interactive prompts mixed into stdout, or a wrapper script altering output.","commonSituations":"Outdated Google Cloud SDK where `gcloud auth print-access-token --format=json` prints plain text; gcloud prompting for consent on first run in a non-TTY; shell wrappers/aliases injecting banners; locale/encoding issues corrupting output.","solutions":["Update gcloud: `gcloud components update`, then re-run.","Run the command manually (`gcloud auth print-access-token --format=json`) and confirm valid JSON on stdout.","Complete any pending gcloud consent/login prompts interactively once so non-TTY runs no longer prompt.","Bypass gcloud user auth by using GOOGLE_APPLICATION_CREDENTIALS / ADC instead."],"exampleFix":"// before: output = \"Your browser has been opened...\" (prompt)\n// after\n$ gcloud auth login   # complete once interactively\n$ gcloud components update","handlingStrategy":"try-catch","validationCode":"// Pre-flight: confirm the command emits JSON\nout, err := exec.Command(\"gcloud\", \"auth\", \"print-access-token\", \"--format=json\").Output()\nif err == nil && !json.Valid(out) {\n    return fmt.Errorf(\"gcloud output is not JSON; update gcloud SDK\")\n}","typeGuard":null,"tryCatchPattern":"tok, err := tokenSource.Token()\nif err != nil && strings.Contains(err.Error(), \"failed to unmarshal gcloud command result\") {\n    return fmt.Errorf(\"update gcloud (`gcloud components update`) and clear interactive prompts: %w\", err)\n}","preventionTips":["Keep the Google Cloud SDK up to date.","Complete first-run consent prompts interactively once so non-TTY runs never prompt.","Avoid shell wrappers/aliases that inject extra output into gcloud stdout.","Prefer ADC over parsing gcloud output when possible."],"tags":["gcp","gcloud","json","exec","parsing"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"a1189de023efc32d4b8e11f395acc678aa555011","analyzedAt":"2026-09-05T12:09:27.064Z","contentChangedAt":"2026-09-05T12:09:27.064Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}