fix: detect Bun runtime so fs operations use node:fs#1281
Open
ZLeventer wants to merge 1 commit into
Open
Conversation
Bun exposes `self` on globalThis as an alias for globalThis itself for Web API compatibility. The previous `'self' in globalThis` check incorrectly identified Bun as a web environment, routing all filesystem calls through memfs and breaking keychain access (and any other code that touches the real filesystem). isWeb() now short-circuits when process.versions.node or process.versions.bun is present, so server runtimes are never mistaken for the browser. The FORCE_MEMFS escape hatch and the existing window/self detection for real web environments are preserved. Fixes forcedotcom/cli#3535
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes forcedotcom/cli#3535.
Bun exposes
selfonglobalThisas an alias forglobalThisitself for Web API compatibility. The previousisWeb()check insrc/fs/fs.tstreated any environment withselfonglobalThisas a web environment, which caused all filesystem calls under Bun to be routed throughmemfsinstead ofnode:fs. The first visible symptom is keychain access failing withMissingCredentialProgramError: Unable to find required security software: /usr/bin/securitybecausememfshas no/usr/bin/security.How
isWeb()now short-circuits tofalsewhen eitherprocess.versions.nodeorprocess.versions.bunis set, so server runtimes are never mistaken for the browser. TheFORCE_MEMFSescape hatch and the'window' in globalThis || 'self' in globalThisdetection for real web/worker environments are preserved.Tests
Added
test/unit/fs/fs.test.tswith three behavioral tests that write/read through the real filesystem vs.memfs:selfis onglobalThisandprocess.versions.bunis set (regression test for #3535).memfswhenFORCE_MEMFS=true(escape hatch preserved).Verification on this branch:
yarn compile— cleanyarn lint— clean (5 pre-existing warnings in unrelated files)test/nut/**) including the existingvirtualfs.nut.ts— all passingReproduction (before the fix)
Under Bun, the previous
isWeb()returnedtrue, so any call into@salesforce/corethat touched the filesystem (auth, keychain, config) failed.