Skip to content

fix: lazy cache initialization to support completion commands with invalid HOME#744

Open
replicated-software-factory[bot] wants to merge 2 commits into
mainfrom
fix-535-lazy-cache-init
Open

fix: lazy cache initialization to support completion commands with invalid HOME#744
replicated-software-factory[bot] wants to merge 2 commits into
mainfrom
fix-535-lazy-cache-init

Conversation

@replicated-software-factory
Copy link
Copy Markdown
Contributor

Summary

Fixes #535

The CLI was calling InitCache() in the init() function, which would fail when HOME was set to an invalid path (e.g., during Nix builds). This caused commands like replicated completion zsh to panic even though they don't need the cache at all.

Changes

  • Made cache initialization lazy using sync.Once - it only happens when the cache is actually needed
  • Added a getCache() helper function that handles the lazy initialization
  • Updated all call sites to use getCache() instead of the global cache variable
  • Added a regression test to verify completion commands work when HOME is unset

Testing

  • Verified HOME=/invalid replicated completion zsh now works without panic
  • All existing tests pass
  • Added new test TestShellCompletionsNoHome to prevent regression

Impact

This allows Nix builds (and other environments where HOME is not writable) to generate shell completions without workarounds.

…valid HOME

The CLI was calling InitCache() in the init() function, which would fail
when HOME was set to an invalid path (e.g., during Nix builds). This caused
commands like 'replicated completion zsh' to panic even though they don't
need the cache at all.

This change makes cache initialization lazy - it only happens when the cache
is actually needed. This allows completion commands to work in environments
where HOME is not writable.

Fixes #535
Comment thread cli/cmd/root.go Outdated
Per review feedback, the lazy-init singleton state (once/instance/err)
and GetInstance helper now live in the cache package itself, removing the
ambiguity between local cache variables, the former global cacheInstance,
and the replicatedcache import alias. Call sites use cache.GetInstance();
tests use cache.ResetForTesting().

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
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.

InitCache should not be called for completion commands

2 participants