charmbracelet/crush · error

failed to get current user: %w

Error message

failed to get current user: %w

What it means

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.

Source

Thrown at internal/cmd/stats.go:199

		}

		projectStats = []ProjectStats{{ProjectPath: "", Stats: stats}}
	}

	if len(projectStats) == 0 {
		return fmt.Errorf("no data available: no projects found")
	}

	// Merge stats from all projects.
	mergedStats := mergeStats(projectStats)

	if mergedStats.Total.TotalSessions == 0 {
		return fmt.Errorf("no data available: no sessions found in database")
	}

	currentUser, err := user.Current()
	if err != nil {
		return fmt.Errorf("failed to get current user: %w", err)
	}
	username := currentUser.Username

	var projName string
	switch {
	case crawlDir != "":
		projName = crawlDir
	case useAll:
		projName = "all projects"
	default:
		project, err := os.Getwd()
		if err != nil {
			return fmt.Errorf("failed to get current directory: %w", err)
		}
		projName = strings.Replace(project, currentUser.HomeDir, "~", 1)
	}

	outputDataDir := dataDir

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Ensure the effective uid exists in /etc/passwd (add the user or run as a named user in the container).
  2. Set HOME and USER env vars — some os/user fallbacks use them; verify with `id` inside the same environment.
  3. In containers, avoid `--user <numeric>` without a passwd entry, or add `RUN echo 'app:x:1000:1000::/home/app:/bin/sh' >> /etc/passwd`.
  4. Check /etc/nsswitch.conf and NSS modules if the machine uses directory services (LDAP/SSSD).

Example fix

// before (Dockerfile)
USER 1000:1000
CMD ["crush", "stats"]
// after (Dockerfile)
RUN echo 'app:x:1000:1000::/home/app:/bin/sh' >> /etc/passwd && mkdir -p /home/app
USER app
CMD ["crush", "stats"]
Defensive patterns

Strategy: validation

Validate before calling

u, err := user.Current()
if err != nil || u.Username == "" {
	return fmt.Errorf("cannot resolve current user (uid %d missing from /etc/passwd?)", os.Getuid())
}

Try / catch

currentUser, err := user.Current()
if err != nil {
	username = os.Getenv("USER")
	if username == "" {
		return fmt.Errorf("failed to get current user: %w", err)
	}
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/c8c14fafbbaa607d. Report an issue: GitHub.