Skip to content

fix: set-once-freeze for PKG_NATIVE_CACHE_PATH resolution#232

Open
olegcS wants to merge 1 commit intoyao-pkg:mainfrom
olegcS:fix/set-once-native-cache-path
Open

fix: set-once-freeze for PKG_NATIVE_CACHE_PATH resolution#232
olegcS wants to merge 1 commit intoyao-pkg:mainfrom
olegcS:fix/set-once-native-cache-path

Conversation

@olegcS
Copy link
Copy Markdown

@olegcS olegcS commented Apr 10, 2026

Problem

PKG_NATIVE_CACHE_PATH is captured once in the bootstrap IIFE, before any application code runs. This means a packaged app cannot programmatically redirect its native addon cache at startup — the only way to set the path is via the process environment before launch.

This is a problem for apps that need to determine the cache path at runtime (e.g. based on install location) and for security-sensitive apps that must ensure the cache points to a protected directory regardless of what environment variables a user has set.

Solution

Defer resolution of PKG_NATIVE_CACHE_PATH to the first dlopen call, then freeze it. This gives the application's entry point a window to set process.env.PKG_NATIVE_CACHE_PATH before any native addon is loaded, after which the value is locked.

Existing behavior (env var set before process starts) is unchanged.

Relation to #228

This is the set-once-freeze portion of #228, split per reviewer feedback. The SHA-256 integrity check is in a separate PR (#230).

Test plan

  • Package an app that sets PKG_NATIVE_CACHE_PATH in its entry point before loading native addons — verify the runtime value is used
  • Package an app with PKG_NATIVE_CACHE_PATH pre-set in the environment — verify it works as before
  • Package an app without PKG_NATIVE_CACHE_PATH set — verify default ~/.cache fallback works
  • Verify that changing PKG_NATIVE_CACHE_PATH after the first native addon load has no effect

Resolve PKG_NATIVE_CACHE_PATH lazily on the first dlopen call instead
of eagerly at bootstrap time, then freeze the value for all subsequent
calls.

This gives applications a window to set process.env.PKG_NATIVE_CACHE_PATH
in their init code (before any native addon is loaded) without requiring
the variable to be present in the environment before the process starts.
Once the first native addon triggers dlopen, the resolved path is frozen
and further changes to the env var are ignored.

Use case: packaged apps that run as a privileged service (e.g. Windows
SYSTEM) need to redirect the native cache from the default user-writable
~/.cache to a protected directory. The app's entry point sets the env var
programmatically based on the install location, but under the current
eager capture the IIFE runs before any user code.

If PKG_NATIVE_CACHE_PATH is already set before the process starts (the
existing workflow), behavior is unchanged — the bootstrap-time value is
captured as the default and confirmed on first dlopen.

Co-Authored-By: Claude Opus 4.6 <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.

1 participant