fix: fall back to copying if linking fails

This commit is contained in:
Zoltan Kochan
2020-06-01 19:16:09 +03:00
parent eb82084e1e
commit 429c5a560b
2 changed files with 17 additions and 3 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/package-store": patch
---
If creating a hard-link to a file from the store fails, fall back to copying the file.

View File

@@ -5,8 +5,6 @@ import fs = require('mz/fs')
import pLimit from 'p-limit'
import path = require('path')
import exists = require('path-exists')
import pathTemp = require('path-temp')
import renameOverwrite = require('rename-overwrite')
import importIndexedDir from '../fs/importIndexedDir'
const limitLinking = pLimit(16)
@@ -112,7 +110,18 @@ async function hardlinkPkg (
if (!opts.fromStore || opts.force || !await exists(pkgJsonPath) || !await pkgLinkedToStore(pkgJsonPath, opts.filesMap['package.json'], to)) {
importingLogger.debug({ to, method: 'hardlink' })
await importIndexedDir(fs.link, to, opts.filesMap)
await importIndexedDir(linkOrCopy, to, opts.filesMap)
}
}
async function linkOrCopy (existingPath: string, newPath: string) {
try {
await fs.link(existingPath, newPath)
} catch (err) {
// In some VERY rare cases (1 in a thousand), hard-link creation fails on Windows.
// In that case, we just fall back to copying.
// This issue is reproducible with "pnpm add @material-ui/icons@4.9.1"
await fs.copyFile(existingPath, newPath)
}
}