Skip to content
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

fix(ext/node): homedir() getpwuid/SHGetKnownFolderPath fallback #23841

Merged
merged 3 commits into from
May 16, 2024

Conversation

littledivy
Copy link
Member

Unix: Returns the value of the HOME environment variable if it is set even if it is an empty string. Otherwise, it tries to determine the home directory by invoking the getpwuid_r function with the UID of the current user.

Windows: Returns the value of the USERPROFILE environment variable if it is set and it is not an empty string. Otherwise, it tries to determine the home directory by invoking the SHGetKnownFolderPath function with FOLDERID_Profile.

Fixes #23824

Copy link
Member

@satyarohith satyarohith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I wonder if we need to expose a similar API in deno (not for now though).

@littledivy littledivy merged commit bba553b into denoland:main May 16, 2024
17 checks passed
@littledivy littledivy deleted the homedir branch May 16, 2024 05:24
bartlomieju pushed a commit that referenced this pull request May 16, 2024
…23841)

**Unix**: Returns the value of the HOME environment variable if it is
set even if it is an empty string. Otherwise, it tries to determine the
home directory by invoking the
[getpwuid_r](https://linux.die.net/man/3/getpwuid_r) function with the
UID of the current user.

**Windows**: Returns the value of the USERPROFILE environment variable
if it is set and it is not an empty string. Otherwise, it tries to
determine the home directory by invoking the
[SHGetKnownFolderPath](https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath)
function with
[FOLDERID_Profile](https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid).

Fixes #23824
@prabhu
Copy link

prabhu commented May 17, 2024

We are seeing some build failures such as this with the latest deno.

error: Uncaught (in promise) PermissionDenied: Requires sys access to "homedir", run again with the --allow-sys flag
    at Object.homedir (node:os:46:10)
    at Object.<anonymous> (file:///home/runner/work/cdxgen/cdxgen/node_modules/.deno/clean-stack@2.2.0/node_modules/clean-stack/index.js:6:61)
    at Object.<anonymous> (file:///home/runner/work/cdxgen/cdxgen/node_modules/.deno/clean-stack@2.2.0/node_modules/clean-stack/index.js:42:4)
    at Module._compile (node:module:659:34)
    at Object.Module._extensions..js (node:module:673:10)
    at Module.load (node:module:597:32)
    at Function.Module._load (node:module:484:12)
    at Module.require (node:module:609:19)
    at require (node:module:715:16)

Is it related to this PR?

@littledivy
Copy link
Member Author

@prabhu yes, it requires the --allow-sys=homedir permission flag because of the use of OS-level APIs.

deno run --allow-sys=homedir or deno run --allow-sys

@prabhu
Copy link

prabhu commented May 18, 2024

@littledivy, thank you for clarifying. Reading the source code for cargo home, it appears like this permission is required only for Windows. Can you correct me, if I am wrong? If sys permission isn't available, can it fallback to using the old env variable approach?

@littledivy
Copy link
Member Author

It is also required on unix: See https://docs.rs/home/0.5.9/src/home/lib.rs.html#68-72 which calls https://doc.rust-lang.org/std/env/fn.home_dir.html.

This is marked as a fix because without a generic permission --allow-sys any internal changes to the implementation would break permission requirements.

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.

homedir() returns null on Amazon Linux 2023
3 participants