fix: linking package globally from workspace should work (#9521)

close #9506
close #9515
This commit is contained in:
Zoltan Kochan
2025-05-13 11:42:45 +02:00
committed by GitHub
parent bffe53896b
commit e4af08ce18
3 changed files with 31 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/config": patch
"pnpm": patch
---
`pnpm link` should work from inside a workspace [#9506](https://github.com/pnpm/pnpm/issues/9506).

View File

@@ -288,6 +288,7 @@ export async function getConfig (opts: {
}
pnpmConfig.globalPkgDir = path.join(globalDirRoot, LAYOUT_VERSION.toString())
if (cliOptions['global']) {
delete pnpmConfig.workspaceDir
pnpmConfig.dir = pnpmConfig.globalPkgDir
pnpmConfig.bin = npmConfig.get('global-bin-dir') ?? env.PNPM_HOME
if (pnpmConfig.bin) {

View File

@@ -3,7 +3,8 @@ 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 { prepare, preparePackages } from '@pnpm/prepare'
import { sync as writeYamlFile } from 'write-yaml-file'
import { execPnpm } from './utils'
const testLinkGlobal = (specifyGlobalOption: boolean) => async () => {
@@ -33,3 +34,25 @@ console.log("hello world");`, 'utf8')
test('link globally the command of a package that has no name in package.json', testLinkGlobal(true))
test('link a package globally without specifying the global option', testLinkGlobal(false))
test('link a package from a workspace to the global package', async () => {
preparePackages([
{
name: 'project-1',
version: '1.0.0',
},
])
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 }
writeYamlFile('pnpm-workspace.yaml', { packages: ['**', '!store/**'] })
process.chdir('project-1')
await execPnpm(['link'], { env })
const globalPrefix = path.join(global, `pnpm/global/${LAYOUT_VERSION}`)
expect(fs.existsSync(path.join(globalPrefix, 'node_modules/project-1'))).toBeTruthy()
})