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

PNPM_HOME does not expand on Windows #5696

Open
maxichrome opened this issue Nov 26, 2022 · 6 comments
Open

PNPM_HOME does not expand on Windows #5696

maxichrome opened this issue Nov 26, 2022 · 6 comments

Comments

@maxichrome
Copy link

maxichrome commented Nov 26, 2022

pnpm version:

7.17.0

Code to reproduce the issue:

n/a

Expected behavior:

%PNPM_HOME% expands when inserted into other environment variables such as Path.

Example

If Path is set to %PNPM_HOME%;%USERPROFILE%\.bin, when opening a shell it becomes C:\Users\user\AppData\Local\pnpm;C:\Users\user\.bin

Actual behavior:

Example

If Path is set to %PNPM_HOME%;%USERPROFILE%\.bin, when opening a shell it becomes %PNPM_HOME%;C:\Users\user\.bin

Additional information:

  • node -v prints: v19.1.0
  • Windows, macOS, or Linux?: Windows

Suspected cause

This has been reproduced by myself and @maplefins in further testing:

During pnpm setup, pnpm sets an environment variable called PNPM_HOME. On Windows, it does this by directly calling reg.exe instead of using a native system API (see path-extender-windows.ts; also see Workaround below).

Reproduced on

  • Windows Server 2022 [20348.1311]
  • Windows 11 [22621.898]

Workaround

Update the PNPM_HOME variable via a native API (in a shell or the Environment Variables editor UI).

In PowerShell:

$pnpmHome = [System.Environment]::GetEnvironmentVariable("PNPM_HOME", [System.EnvironmentVariableTarget]::User)
[System.Environment]::SetEnvironmentVariable("PNPM_HOME", $pnpmHome, [System.EnvironmentVariableTarget]::User)
@maxichrome maxichrome changed the title PNPM_HOME in Path does not expand on Windows PNPM_HOME does not expand on Windows Nov 26, 2022
@zkochan
Copy link
Member

zkochan commented Nov 27, 2022

How can we run then native API? Should we generate a powershell file and run it? As far as I remember Windows doesn't allow to run unsigned ps1 files.

@maxichrome
Copy link
Author

I'm unsure what this looks like practically in terms of security-related permissions but you should be able to run the PowerShell executable with the necessary function as an argument

@maxichrome
Copy link
Author

maxichrome commented Nov 28, 2022

Based on the fact this works:

powershell -command "[System.Environment]::SetEnvironmentVariable('PNPM_HOME', '$pnpmHome', [System.EnvironmentVariableTarget]::User)"

path-extender-windows.ts could be modified in a way similar to this:

I know this looks silly, but it's probably the best solution here unless you want to embed a native binary or FFI that somehow calls the API

await execa('powershell', ['-command', '"[System.Environment]::SetEnvironmentVariable(\'${envVarName}\', \'${envVarValue}\', [System.EnvironmentVariableTarget]::User)"'], EXEC_OPTS)

You may want to split this up somehow to avoid the long single string, but that's all for however you want to style your own code :P

@zkochan
Copy link
Member

zkochan commented Dec 13, 2022

By the way, for this issue there was this suggestion: #4658 (comment)

that I released in v7.17.1

That didn't help?

@zkochan
Copy link
Member

zkochan commented Jan 25, 2023

Looks like it is solved but only if PNPM_HOME wasn't configured by pnpm older than v7.17

#4658 (comment)

@maxichrome
Copy link
Author

yes the findings in that comment make sense with my previous findings! maybe a separate change needs to be made to retrieve, unset, and reset PNPM_HOME to the same value using the current method

(or would unsetting be necessary at all?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants