Skip to content

Commit 00b9f9f

Browse files
fix(arborist): symlink workspace file: deps on non-workspace local packages (#9591)
1 parent ed72962 commit 00b9f9f

2 files changed

Lines changed: 98 additions & 1 deletion

File tree

‎workspaces/arborist/lib/arborist/isolated-reifier.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,9 @@ module.exports = cls => class IsolatedReifier extends cls {
228228
}
229229

230230
// local `file:` deps (non-workspace fsChildren) should be treated as local dependencies, not external, so they get symlinked directly instead of being extracted into the store.
231-
const isLocal = (n) => n.isWorkspace || node.fsChildren?.has(n)
231+
// A file: dep surfaces as a Link edge whose resolved spec starts with file:; detect it from the edge so the target is treated as local even when it is absent from idealTree.fsChildren (a workspace consumer, or a target outside the repo root via npm link).
232+
const fileLinkTargets = new Set(edges.filter(e => e.to?.isLink && e.to.resolved?.startsWith('file:')).map(e => e.to.target))
233+
const isLocal = (n) => n.isWorkspace || node.fsChildren?.has(n) || fileLinkTargets.has(n)
232234
const optionalDeps = edges.filter(edge => edge.optional).map(edge => edge.to.target)
233235

234236
// Optional peers declared only in peerDependenciesMeta (e.g. `@types/react`) have no edge, so the materialization above misses them.

‎workspaces/arborist/test/isolated-mode.js‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,6 +1657,101 @@ tap.test('npm link (external file: dep) with linked strategy', async t => {
16571657
t.notOk(storeEntries.some(e => e.startsWith('external-pkg@')), 'external-pkg is NOT in the store')
16581658
})
16591659

1660+
tap.test('workspace file: dependency on a non-workspace local package with linked strategy', async t => {
1661+
// Regression test for https://github.com/npm/cli/issues/9589
1662+
// A workspace declaring a file: dep on a local package that is NOT itself a workspace was silently skipped: no symlink, no error.
1663+
const graph = {
1664+
registry: [],
1665+
root: {
1666+
name: 'mono',
1667+
version: '1.0.0',
1668+
},
1669+
workspaces: [
1670+
{ name: 'ws-a', version: '1.0.0', dependencies: { 'local-dep': 'file:../../local-dep' } },
1671+
],
1672+
}
1673+
1674+
const { dir, registry } = await getRepo(graph)
1675+
1676+
// Create the non-workspace local package on disk, outside the workspaces globs
1677+
const depDir = path.join(dir, 'local-dep')
1678+
fs.mkdirSync(depDir, { recursive: true })
1679+
fs.writeFileSync(path.join(depDir, 'package.json'), JSON.stringify({
1680+
name: 'local-dep',
1681+
version: '1.0.0',
1682+
}))
1683+
fs.writeFileSync(path.join(depDir, 'index.js'), "module.exports = 'local-dep'")
1684+
1685+
const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1686+
const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1687+
await arborist.reify({ installStrategy: 'linked' })
1688+
1689+
// The file dep should be symlinked into the workspace's node_modules
1690+
const linkPath = path.join(dir, 'packages', 'ws-a', 'node_modules', 'local-dep')
1691+
const stat = fs.lstatSync(linkPath)
1692+
t.ok(stat.isSymbolicLink(), 'local-dep is a symlink in the workspace node_modules')
1693+
1694+
// The symlink should resolve to the actual local directory
1695+
t.equal(fs.realpathSync(linkPath), fs.realpathSync(depDir), 'symlink points to the correct local directory')
1696+
1697+
// It must be symlinked directly, not extracted into the store
1698+
const storePath = path.join(dir, 'node_modules', '.store')
1699+
if (fs.existsSync(storePath)) {
1700+
t.notOk(fs.readdirSync(storePath).some(e => e.startsWith('local-dep@')), 'local-dep is NOT in the store')
1701+
}
1702+
1703+
// The package should be requireable from inside the workspace
1704+
t.ok(setupRequire(path.join(dir, 'packages', 'ws-a'))('local-dep'), 'local-dep can be required from the workspace')
1705+
})
1706+
1707+
tap.test('workspace file: dependency on a package outside the repo root with linked strategy', async t => {
1708+
// Regression test for the out-of-repo variant of https://github.com/npm/cli/issues/9589 (the real `npm --workspace link <external>` case, https://github.com/npm/cli/issues/9115).
1709+
// A workspace file: dep whose target resolves OUTSIDE the repo root was silently skipped.
1710+
// The target is not in idealTree.fsChildren, so the fix must detect it from the file: link edge.
1711+
const graph = {
1712+
registry: [],
1713+
root: {
1714+
name: 'mono',
1715+
version: '1.0.0',
1716+
},
1717+
workspaces: [
1718+
{ name: 'ws-a', version: '1.0.0', dependencies: { 'ext-pkg': 'file:../../../ext-pkg' } },
1719+
],
1720+
}
1721+
1722+
const { dir, registry } = await getRepo(graph)
1723+
1724+
// Create the external package OUTSIDE the repo root
1725+
const extDir = path.join(path.dirname(dir), 'ext-pkg')
1726+
fs.mkdirSync(extDir, { recursive: true })
1727+
fs.writeFileSync(path.join(extDir, 'package.json'), JSON.stringify({
1728+
name: 'ext-pkg',
1729+
version: '1.0.0',
1730+
}))
1731+
fs.writeFileSync(path.join(extDir, 'index.js'), "module.exports = 'ext-pkg'")
1732+
1733+
const cache = fs.mkdtempSync(`${getTempDir()}/test-`)
1734+
const arborist = new Arborist({ path: dir, registry, packumentCache: new Map(), cache })
1735+
await arborist.reify({ installStrategy: 'linked' })
1736+
1737+
// The file dep should be symlinked into the workspace's node_modules
1738+
const linkPath = path.join(dir, 'packages', 'ws-a', 'node_modules', 'ext-pkg')
1739+
const stat = fs.lstatSync(linkPath)
1740+
t.ok(stat.isSymbolicLink(), 'ext-pkg is a symlink in the workspace node_modules')
1741+
1742+
// The symlink should resolve to the actual external directory
1743+
t.equal(fs.realpathSync(linkPath), fs.realpathSync(extDir), 'symlink points to the correct external directory')
1744+
1745+
// It must be symlinked directly, not extracted into the store
1746+
const storePath = path.join(dir, 'node_modules', '.store')
1747+
if (fs.existsSync(storePath)) {
1748+
t.notOk(fs.readdirSync(storePath).some(e => e.startsWith('ext-pkg@')), 'ext-pkg is NOT in the store')
1749+
}
1750+
1751+
// The package should be requireable from inside the workspace
1752+
t.ok(setupRequire(path.join(dir, 'packages', 'ws-a'))('ext-pkg'), 'ext-pkg can be required from the workspace')
1753+
})
1754+
16601755
tap.test('subsequent linked install is a no-op', async t => {
16611756
const graph = {
16621757
registry: [

0 commit comments

Comments
 (0)