{"record":{"id":"434cc0b2bf8c3d68","repo":"nikivdev/code","slug":"could-not-determine-github-username","errorCode":null,"errorMessage":"Could not determine GitHub username","messagePattern":"Could not determine GitHub username","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/publish.rs","lineNumber":148,"sourceCode":"    let cwd = std::env::current_dir()?;\n    let folder_name = cwd\n        .file_name()\n        .and_then(|n| n.to_str())\n        .unwrap_or(\"repo\")\n        .to_string();\n\n    // Check if already a git repo\n    let is_git_repo = cwd.join(\".git\").exists();\n\n    // Get GitHub username (fallback owner)\n    let gh_user = Command::new(\"gh\")\n        .args([\"api\", \"user\", \"-q\", \".login\"])\n        .output()\n        .context(\"failed to get GitHub username\")?;\n\n    let username = String::from_utf8_lossy(&gh_user.stdout).trim().to_string();\n    if username.is_empty() {\n        bail!(\"Could not determine GitHub username\");\n    }\n    let mut owner = opts.owner.clone().unwrap_or_else(|| username.clone());\n    let mut repo_name_from_url: Option<String> = None;\n    let mut remote_from_url: Option<String> = None;\n    if let Some(url) = opts.url.as_ref() {\n        let (parsed_owner, parsed_name, parsed_remote) = parse_github_repo(url)?;\n        owner = parsed_owner;\n        repo_name_from_url = Some(parsed_name);\n        remote_from_url = Some(parsed_remote);\n    }\n\n    // Determine repo name\n    let repo_name = if let Some(name) = opts.name {\n        name\n    } else if let Some(name) = repo_name_from_url.clone() {\n        name\n    } else if opts.yes {\n        folder_name.clone()","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/nikivdev/code/blob/a747e741ae92c09071d0ae946ab48488adcff1ce/src/publish.rs#L130-L166","documentation":"run_github shells out to `gh api user -q .login` to learn the authenticated GitHub username, used as the default owner for the new repository. If the command succeeds but prints nothing (empty stdout), the tool cannot name an owner and bails with this error. It usually means `gh` is not authenticated even though an earlier auth check appeared to pass, or the token has no user scope.","triggerScenarios":"`gh api user -q .login` returns exit 0 with empty stdout: e.g. a GH_TOKEN/GITHUB_TOKEN env var is set to an invalid or scope-less token, `gh` is authenticated to a non-GitHub (GHES) host with no user, or `gh auth login` was skipped while a stub `gh` is on PATH.","commonSituations":"CI containers where GH_TOKEN is set but expired/revoked; gh authenticated against a different hostname than expected; running after `gh auth logout`; a wrapper script shadowing `gh` that outputs nothing on stdout.","solutions":["Run `gh api user -q .login` manually; if empty, run `gh auth login` (or `gh auth login --hostname <host>`) to re-authenticate","Check `gh auth status` and verify the correct host and token scopes (needs `repo`/`user`)","If GH_TOKEN/GITHUB_TOKEN is set in the environment, verify the token is valid and has read:user scope, or unset it so `gh` uses its stored credentials","Pass `--owner <name>` to publish so the username lookup is not needed for the owner"],"exampleFix":"// before (CI)\nexport GH_TOKEN=ghp_expiredtoken\nf publish\n// after: refresh the token or authenticate interactively\ngh auth login\n# or pass owner explicitly\nf publish --owner myuser","handlingStrategy":"validation","validationCode":"let out = std::process::Command::new(\"gh\").args([\"api\",\"user\",\"-q\",\".login\"]).output()?;\nlet login = String::from_utf8_lossy(&out.stdout).trim().to_string();\nif !out.status.success() || login.is_empty() {\n    eprintln!(\"gh is not authenticated; run `gh auth login` first\");\n    std::process::exit(1);\n}","typeGuard":null,"tryCatchPattern":"match run_github(opts) {\n    Err(e) if e.to_string().contains(\"Could not determine GitHub username\") => {\n        eprintln!(\"Run `gh auth login`, or pass --owner <name> to skip the lookup\");\n    }\n    other => other?,\n}","preventionTips":["Run `gh auth status` before scripting publish commands","Avoid scope-less/expired GH_TOKEN env vars in CI; rotate tokens regularly","Pass --owner explicitly in automation to skip the username lookup"],"tags":["github","gh-cli","authentication","shell-out"],"backgroundTag":"gh-cli-unauthenticated","analyzedSha":"a747e741ae92c09071d0ae946ab48488adcff1ce","analyzedAt":"2026-09-01T22:43:55.719Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}