Skip to content

Commit

Permalink
fix(env): use hard links instead of symlinks on Windows
Browse files Browse the repository at this point in the history
close #4315
  • Loading branch information
zkochan committed Apr 16, 2023
1 parent d43ccc4 commit 69d6a2f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/late-laws-approve.md
@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-env": patch
"pnpm": patch
---

Use hard links to link the node executable on Windows machines [#4315](https://github.com/pnpm/pnpm/issues/4315).
2 changes: 2 additions & 0 deletions env/plugin-commands-env/package.json
Expand Up @@ -41,6 +41,7 @@
"@pnpm/store-path": "workspace:*",
"@zkochan/cmd-shim": "^6.0.0",
"@zkochan/rimraf": "^2.1.2",
"is-windows": "^1.0.2",
"load-json-file": "^6.2.0",
"render-help": "^1.0.3",
"semver": "^7.4.0",
Expand All @@ -51,6 +52,7 @@
"@pnpm/plugin-commands-env": "workspace:*",
"@pnpm/prepare": "workspace:*",
"@types/adm-zip": "^0.5.0",
"@types/is-windows": "^1.0.0",
"@types/semver": "7.3.13",
"adm-zip": "^0.5.10",
"execa": "npm:safe-execa@0.1.2",
Expand Down
12 changes: 11 additions & 1 deletion env/plugin-commands-env/src/envUse.ts
Expand Up @@ -4,6 +4,7 @@ import { PnpmError } from '@pnpm/error'
import { createFetchFromRegistry } from '@pnpm/fetch'
import { resolveNodeVersion } from '@pnpm/node.resolver'
import cmdShim from '@zkochan/cmd-shim'
import isWindows from 'is-windows'
import { getNodeDir, type NvmNodeCommandOptions } from './node'
import { getNodeMirror } from './getNodeMirror'
import { parseNodeEditionSpecifier } from './parseNodeEditionSpecifier'
Expand All @@ -30,7 +31,7 @@ export async function envUse (opts: NvmNodeCommandOptions, params: string[]) {
try {
await fs.unlink(dest)
} catch (err) {}
await fs.symlink(src, dest, 'file')
await symlinkOrHardLink(src, dest)
try {
let npmDir = nodeDir
if (process.platform !== 'win32') {
Expand All @@ -52,3 +53,12 @@ export async function envUse (opts: NvmNodeCommandOptions, params: string[]) {
return `Node.js ${nodeVersion as string} is activated
${dest} -> ${src}`
}

// On Windows, symlinks only work with developer mode enabled
// or with admin permissions. So it is better to use hard links on Windows.
async function symlinkOrHardLink (existingPath: string, newPath: string) {
if (isWindows()) {
return fs.link(existingPath, newPath)
}
return fs.symlink(existingPath, newPath, 'file')
}
13 changes: 9 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 69d6a2f

Please sign in to comment.