{"record":{"id":"c8c14fafbbaa607d","repo":"charmbracelet/crush","slug":"failed-to-get-current-user-w","errorCode":null,"errorMessage":"failed to get current user: %w","messagePattern":"failed to get current user: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":199,"sourceCode":"\t\t}\n\n\t\tprojectStats = []ProjectStats{{ProjectPath: \"\", Stats: stats}}\n\t}\n\n\tif len(projectStats) == 0 {\n\t\treturn fmt.Errorf(\"no data available: no projects found\")\n\t}\n\n\t// Merge stats from all projects.\n\tmergedStats := mergeStats(projectStats)\n\n\tif mergedStats.Total.TotalSessions == 0 {\n\t\treturn fmt.Errorf(\"no data available: no sessions found in database\")\n\t}\n\n\tcurrentUser, err := user.Current()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get current user: %w\", err)\n\t}\n\tusername := currentUser.Username\n\n\tvar projName string\n\tswitch {\n\tcase crawlDir != \"\":\n\t\tprojName = crawlDir\n\tcase useAll:\n\t\tprojName = \"all projects\"\n\tdefault:\n\t\tproject, err := os.Getwd()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to get current directory: %w\", err)\n\t\t}\n\t\tprojName = strings.Replace(project, currentUser.HomeDir, \"~\", 1)\n\t}\n\n\toutputDataDir := dataDir","sourceCodeStart":181,"sourceCodeEnd":217,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/stats.go#L181-L217","documentation":"runStats wraps os/user.Current() failure, used to attribute stats to the current username (e.g. for filtering or display). os/user.Current fails when the process cannot resolve the invoking user — typically a missing/unreadable /etc/passwd entry, cgo-less builds falling back to file parsing that fails, or restricted environments with no valid uid.","triggerScenarios":"Running `crush stats` as a uid not present in /etc/passwd (containers running as arbitrary numeric UID, e.g. `docker run -u 1000:1000` with no matching entry); statically linked CGO_ENABLED=0 builds where the pure-Go passwd lookup fails; stripped-down minimal container images missing /etc/passwd or /etc/nsswitch.conf; LDAP/NSS setups where NSS lookups fail.","commonSituations":"Docker/Kubernetes sidecars or CI jobs running as a non-existent numeric user; Alpine/distroless images; running inside chroots without /etc/passwd; setuid or restricted sandboxes.","solutions":["Ensure the effective uid exists in /etc/passwd (add the user or run as a named user in the container).","Set HOME and USER env vars — some os/user fallbacks use them; verify with `id` inside the same environment.","In containers, avoid `--user <numeric>` without a passwd entry, or add `RUN echo 'app:x:1000:1000::/home/app:/bin/sh' >> /etc/passwd`.","Check /etc/nsswitch.conf and NSS modules if the machine uses directory services (LDAP/SSSD)."],"exampleFix":"// before (Dockerfile)\nUSER 1000:1000\nCMD [\"crush\", \"stats\"]\n// after (Dockerfile)\nRUN echo 'app:x:1000:1000::/home/app:/bin/sh' >> /etc/passwd && mkdir -p /home/app\nUSER app\nCMD [\"crush\", \"stats\"]","handlingStrategy":"validation","validationCode":"u, err := user.Current()\nif err != nil || u.Username == \"\" {\n\treturn fmt.Errorf(\"cannot resolve current user (uid %d missing from /etc/passwd?)\", os.Getuid())\n}","typeGuard":null,"tryCatchPattern":"currentUser, err := user.Current()\nif err != nil {\n\tusername = os.Getenv(\"USER\")\n\tif username == \"\" {\n\t\treturn fmt.Errorf(\"failed to get current user: %w\", err)\n\t}\n}","preventionTips":["In containers, run as a user that exists in /etc/passwd or add a passwd entry for the numeric UID.","Set USER and HOME env vars explicitly in minimal images.","Avoid arbitrary --user <uid> in Docker/K8s without a matching passwd entry.","Test commands in the same minimal image/sandbox they will run in."],"tags":["go","os-user","container","environment"],"backgroundTag":"user-lookup-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}