Skip to content

fix(config): respect $HOME when resolving the default storage directory - #704

Open
NopeCharon wants to merge 1 commit into
modelpack:mainfrom
NopeCharon:fix/config-respect-home-env
Open

NopeCharon wants to merge 1 commit into
modelpack:mainfrom
NopeCharon:fix/config-respect-home-env

Conversation

@NopeCharon

Copy link
Copy Markdown

Problem

config.NewRoot() resolves the default --storage-dir and --log-dir through os/user.Current(). With cgo enabled (as in the release builds), that queries the user database via getpwuid_r and ignores $HOME. As a result, every command, including modctl version, creates ~/.modctl under the system home directory even when $HOME points somewhere else, for example a temporary directory in tests or a sandbox.

Fixes #580

Fix

  • Resolve the home directory with os.UserHomeDir(), which honors $HOME (%USERPROFILE% on Windows).
  • Fall back to os/user.Current() only when the environment variable is unset. Environments without $HOME (some service managers or minimal containers) keep working as before instead of failing at startup.

Tests

  • New pkg/config/root_test.go:
    • TestNewRootRespectsHomeEnv sets $HOME to a temp dir and checks StorageDir/LogDir. It fails on main (it resolves to the real home directory) and passes with this change.
    • TestNewRootFallsBackToCurrentUser clears $HOME and checks the os/user fallback.
  • go test -tags static,system_libgit2 ./pkg/config ./cmd passes.
  • golangci-lint run (v2.4) reports 0 issues.
  • Manual check on Linux: HOME=/tmp/fakehome modctl version now creates /tmp/fakehome/.modctl.

config.NewRoot resolved the default storage and log directories through
os/user.Current. With cgo enabled (as in the release builds) that queries
the user database via getpwuid_r and ignores the $HOME environment
variable, so overriding $HOME, for example in tests or sandboxed
environments, still created ~/.modctl under the system home directory.
This was observed on aarch64-darwin, where every command, including
`modctl version`, touched the real home directory.

Resolve the home directory with os.UserHomeDir instead, which honors
$HOME (%USERPROFILE% on Windows), and fall back to os/user.Current only
when the environment variable is unset so that environments without
$HOME keep working as before.

Add unit tests for both the $HOME override and the fallback path.

Fixes modelpack#580

Signed-off-by: NopeCharon <115794254+NopeCharon@users.noreply.github.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: os/user.Current() ignores $HOME, breaks on aarch64-darwin

1 participant