nodejs / corepack

Zero-runtime-dependency package acting as bridge between Node projects and their package managers
MIT License
2.43k stars 159 forks source link

Windows Powershell Disabled by default #213

Open jacobaagaardbennike opened 1 year ago

jacobaagaardbennike commented 1 year ago

Info Windows: 10 v19045.2251 Node: v18.12.1 Yarn: v3.3.0

Issue Windows does not allow powershell scripts to be run by default.

In full:

yarn : File C:\Program Files\nodejs\yarn.ps1 cannot be loaded because running scripts is disabled on this system. For more inf 
ormation, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ yarn -v
+ ~~~~
    + CategoryInfo          : SecurityError: (:) [], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

Replication As administrator run the following console commands: corepack enable corepack prepare yarn@stable --activate yarn -v

Sollution If you have access to overwrite the global powershell policies, this is easy to solve. In many Enterprice settings this isn't going to be an option, however, even when you have the local administrative rights to run the "corepack" commands on your local machine.

What you can do, is to rename the "yarn.ps1" file to something like "yarnABC.ps1". Then the system seems to default to the "yarn.CMD" file instead, which works just fine.

Powershell files in corepack I'm not sure why "corepack enable" creates .ps1 files for the corepack utilities, when there aren't any for npx or npm, but this will cause an issue in a lot of Enterprise settings, and if the .ps1 files aren't necessary, then maybe they could be removed in a future update?

aduh95 commented 1 year ago

What you can do, is to rename the "yarn.ps1" file to something like "yarnABC.ps1". Then the system seems to default to the "yarn.CMD" file instead, which works just fine.

Another "workaround" is to always prefix the yarn command with "corepack", e.g. corepack yarn start. Not ideal of course, but it might unblock you as it should work in all cases corepack itself is available.

We could introduce a new env variable to disable the creation of .ps1, or maybe a CLI flag (e.g. corepack enable --no-powershell). I'm not familiar enough with Windows to know if not having .ps1 by default would make sense, but making it opt-out would be a good first step I think. Would you like to contribute a PR?

asvishnyakov commented 6 months ago

This is a definitely a problem for Windows users after upgrading to yarn 4.

merceyz commented 6 months ago

Another option is to add aliases to your PowerShell profile as documented here: https://github.com/nodejs/corepack/tree/b9eea589b4535d32b8e9ca1e884c3a44bf7d99ea#corepack-enable--name

asvishnyakov commented 6 months ago

It's a require additional steps by developers, while previously it was just clone and go solution with yarnPath

merceyz commented 6 months ago

This is off-topic but you can still use yarnPath if you want https://yarnpkg.com/cli/set/version#options.

asvishnyakov commented 6 months ago

I think the real solution for this issue will be just sign this scripts with the same key as used for Node.js itself.

zkochan commented 3 days ago

Isn't this script basically dynamically created? I don't think that it is a static file that can be presigned. Just don't create a ps1 file.