fix(link-bins): don't throw error during install when bin points to path that doesn't exist (#4172)

close #3763
This commit is contained in:
Kenrick
2021-12-29 18:53:04 +08:00
committed by GitHub
parent b5734a4a7c
commit 701ea07461
4 changed files with 42 additions and 10 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/link-bins": patch
"pnpm": patch
---
Don't throw an error during install when the bin of a dependency points to a path that doesn't exist [#3763](https://github.com/pnpm/pnpm/issues/3763).

View File

@@ -203,15 +203,15 @@ async function getPackageBinsFromManifest (manifest: DependencyManifest, pkgDir:
async function linkBin (cmd: CommandInfo, binsDir: string, opts?: { extendNodePath?: boolean }) {
const externalBinPath = path.join(binsDir, cmd.name)
let nodePath: string[] | undefined
if (opts?.extendNodePath !== false) {
nodePath = await getBinNodePaths(cmd.path)
const binsParentDir = path.dirname(binsDir)
if (path.relative(cmd.path, binsParentDir) !== '') {
nodePath = union(nodePath, await getBinNodePaths(binsParentDir))
}
}
try {
let nodePath: string[] | undefined
if (opts?.extendNodePath !== false) {
nodePath = await getBinNodePaths(cmd.path)
const binsParentDir = path.dirname(binsDir)
if (path.relative(cmd.path, binsParentDir) !== '') {
nodePath = union(nodePath, await getBinNodePaths(binsParentDir))
}
}
await cmdShim(cmd.path, externalBinPath, {
createPwshFile: cmd.makePowerShellShim,
nodePath,

View File

@@ -0,0 +1,5 @@
{
"name": "meow",
"version": "1.0.0",
"bin": "dist/not-exist.js"
}

View File

@@ -2,7 +2,7 @@
import { promisify } from 'util'
import { promises as fs } from 'fs'
import path from 'path'
import logger from '@pnpm/logger'
import logger, { globalWarn } from '@pnpm/logger'
import linkBins, {
linkBinsOfPackages,
} from '@pnpm/link-bins'
@@ -15,7 +15,13 @@ import tempy from 'tempy'
jest.mock('@pnpm/logger', () => {
const debug = jest.fn()
return () => ({ debug })
const globalWarn = jest.fn()
return {
__esModule: true,
default: () => ({ debug }),
globalWarn,
}
})
const binsConflictLogger = logger('bins-conflict')
@@ -35,6 +41,7 @@ const exoticManifestFixture = path.join(fixtures, 'exotic-manifest')
const noNameFixture = path.join(fixtures, 'no-name')
const noBinFixture = path.join(fixtures, 'no-bin')
const windowShebangFixture = path.join(fixtures, 'bin-window-shebang')
const binNotExistFixture = path.join(fixtures, 'bin-not-exist')
const POWER_SHELL_IS_SUPPORTED = isWindows()
const IS_WINDOWS = isWindows()
@@ -355,3 +362,17 @@ test('linkBins() fix window shebang line', async () => {
}
}
})
test("linkBins() emits global warning when bin points to path that doesn't exist", async () => {
const binTarget = tempy.directory()
await linkBins(path.join(binNotExistFixture, 'node_modules'), binTarget, {
allowExoticManifests: true,
warn: () => {},
})
expect(await fs.readdir(binTarget)).toEqual(getExpectedBins([]))
expect(
globalWarn
).toHaveBeenCalled()
})