fix: linking globally a package with no name in package.json (#7637)

close #4761
close #4793
This commit is contained in:
Zoltan Kochan
2024-02-12 10:38:55 +01:00
committed by GitHub
parent 25524bd8f8
commit 22c7acc4dc
3 changed files with 38 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---
Link globally the command of a package that has no name in `package.json` [#4761](https://github.com/pnpm/pnpm/issues/4761).

View File

@@ -1156,7 +1156,7 @@ async function resolveDependency (
throw new PnpmError('MISSING_PACKAGE_JSON', `Can't install ${wantedDependency.pref}: Missing package.json file`)
}
return {
alias: wantedDependency.alias || manifest.name,
alias: wantedDependency.alias || manifest.name || path.basename(pkgResponse.body.resolution.directory),
depPath: pkgResponse.body.id,
dev: wantedDependency.dev,
isLinkedDependency: true,

31
pnpm/test/link.ts Normal file
View File

@@ -0,0 +1,31 @@
import path from 'path'
import PATH_NAME from 'path-name'
import fs from 'fs'
import { isExecutable } from '@pnpm/assert-project'
import { LAYOUT_VERSION } from '@pnpm/constants'
import { prepare } from '@pnpm/prepare'
import { execPnpm } from './utils'
test('link globally the command of a package that has no name in package.json', async () => {
prepare()
fs.mkdirSync('cmd')
process.chdir('cmd')
fs.writeFileSync('package.json', JSON.stringify({ bin: { cmd: 'bin.js' } }), 'utf8')
fs.writeFileSync('bin.js', `#!/usr/bin/env node
console.log("hello world");`, 'utf8')
const global = path.resolve('..', 'global')
const pnpmHome = path.join(global, 'pnpm')
fs.mkdirSync(global)
const env = { [PATH_NAME]: pnpmHome, PNPM_HOME: pnpmHome, XDG_DATA_HOME: global }
await execPnpm(['link', '--global'], { env })
const globalPrefix = path.join(global, `pnpm/global/${LAYOUT_VERSION}`)
expect(fs.existsSync(path.join(globalPrefix, 'node_modules/cmd'))).toBeTruthy()
const ok = (value: any) => { // eslint-disable-line
expect(value).toBeTruthy()
}
await isExecutable(ok, path.join(pnpmHome, 'cmd'))
})