-
Notifications
You must be signed in to change notification settings - Fork 5k
Honor live $HOME mutations in os.homedir() #29248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
7
commits into
main
Choose a base branch
from
farm/eb38f683/fix-os-homedir-stale-env
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+111
−10
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
670d8ce
Honor live $HOME mutations in os.homedir()
robobun 5a280e5
homedir: drop the Windows pass-through wrapper, tighten comments
robobun 0cd7f84
homedir: revert passwd lookup to geteuid(), matching libuv
robobun c06bfae
test: drop bug-history sentence from empty-HOME comment
robobun 58e7023
test: port homedir env tests to node:test for direct Node parity
robobun b9236a5
test: rename homedir env test to .test.js (CJS)
robobun 578bd78
test: set quiet-log env vars in homedir test spawns
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // https://github.com/oven-sh/bun/issues/29244 | ||
| // os.homedir() must reflect live process.env.HOME (HOME="" → ""; only absent | ||
| // HOME falls through to passwd); os.userInfo().homedir must NOT honor HOME. | ||
| // Uses node:test so the same file runs under Node.js to verify parity: | ||
| // node test/js/node/os/os-homedir-env.test.mjs | ||
| // Each check spawns process.execPath so HOME mutations can't leak out. | ||
| import assert from "node:assert"; | ||
| import { spawnSync } from "node:child_process"; | ||
| import { test } from "node:test"; | ||
|
|
||
| const isWindows = process.platform === "win32"; | ||
|
|
||
| function runWithEnv(source, envOverride = {}) { | ||
| const env = { ...process.env }; | ||
| for (const [key, value] of Object.entries(envOverride)) { | ||
| if (value === undefined) delete env[key]; | ||
| else env[key] = value; | ||
| } | ||
| const { stdout, stderr, status } = spawnSync(process.execPath, ["-e", source], { | ||
| env, | ||
| encoding: "utf8", | ||
| }); | ||
| assert.strictEqual(status, 0, stderr); | ||
| return JSON.parse(stdout); | ||
| } | ||
|
|
||
| test("homedir() reflects HOME mutation after require", { skip: isWindows }, () => { | ||
| const result = runWithEnv(` | ||
| const os = require("node:os"); | ||
| const before = os.homedir(); | ||
| process.env.HOME = "/tmp/test-home-29244"; | ||
| console.log(JSON.stringify({ before, after: os.homedir() })); | ||
| `); | ||
| assert.strictEqual(result.after, "/tmp/test-home-29244"); | ||
| assert.notStrictEqual(result.before, "/tmp/test-home-29244"); | ||
| assert.ok(result.before.length > 0); | ||
| }); | ||
|
|
||
| test("homedir() reflects HOME mutation before require", { skip: isWindows }, () => { | ||
| const result = runWithEnv(` | ||
| process.env.HOME = "/tmp/before-require-29244"; | ||
| console.log(JSON.stringify(require("node:os").homedir())); | ||
| `); | ||
| assert.strictEqual(result, "/tmp/before-require-29244"); | ||
| }); | ||
|
|
||
| test("homedir() honors HOME from parent env", { skip: isWindows }, () => { | ||
| const result = runWithEnv(`console.log(JSON.stringify(require("node:os").homedir()));`, { | ||
| HOME: "/tmp/inherited-29244", | ||
| }); | ||
| assert.strictEqual(result, "/tmp/inherited-29244"); | ||
| }); | ||
|
|
||
| test("homedir() returns '' when HOME is set to empty string", { skip: isWindows }, () => { | ||
| // uv_os_homedir returns getenv("HOME") verbatim whenever it is non-NULL, | ||
| // including ""; only an absent HOME falls through to the passwd entry. | ||
| const result = runWithEnv(` | ||
| process.env.HOME = ""; | ||
| console.log(JSON.stringify(require("node:os").homedir())); | ||
| `); | ||
| assert.strictEqual(result, ""); | ||
| }); | ||
|
|
||
| test("homedir() falls back to passwd when HOME is deleted", { skip: isWindows }, () => { | ||
| // Seed a sentinel so a silently-ignored delete is detectable, and | ||
| // cross-check against userInfo().homedir (the passwd entry). | ||
| const sentinel = "/tmp/sentinel-deleted-29244"; | ||
| const result = runWithEnv( | ||
| ` | ||
| delete process.env.HOME; | ||
| const os = require("node:os"); | ||
| console.log(JSON.stringify({ h: os.homedir(), passwd: os.userInfo().homedir })); | ||
| `, | ||
| { HOME: sentinel }, | ||
| ); | ||
| assert.notStrictEqual(result.h, sentinel); | ||
| assert.strictEqual(result.h, result.passwd); | ||
| assert.ok(result.h.startsWith("/")); | ||
| }); | ||
|
|
||
| test("userInfo().homedir ignores HOME mutation", { skip: isWindows }, () => { | ||
| const result = runWithEnv(` | ||
| process.env.HOME = "/tmp/should-not-appear-29244"; | ||
| console.log(JSON.stringify(require("node:os").userInfo().homedir)); | ||
| `); | ||
| assert.notStrictEqual(result, "/tmp/should-not-appear-29244"); | ||
| assert.ok(result.length > 0); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.