feat: excludeLinksFromLockfile (#6570)

This commit is contained in:
Zoltan Kochan
2023-05-21 12:23:31 +03:00
committed by GitHub
parent 9c4ae87bd1
commit 301b8e2da0
5 changed files with 92 additions and 3 deletions

View File

@@ -0,0 +1,8 @@
---
"@pnpm/config": minor
"pnpm": minor
---
A new setting, `exclude-links-from-lockfile`, is now supported. When enabled, specifiers of local linked dependencies won't be duplicated in the lockfile.
This setting was primarily added for use by [Bit CLI](https://github.com/teambit/bit), which links core aspects to `node_modules` from external directories. As such, the locations may vary across different machines, resulting in the generation of lockfiles with differing locations.

View File

@@ -19,6 +19,7 @@ export interface Config {
color: 'always' | 'auto' | 'never'
cliOptions: Record<string, any>, // eslint-disable-line
useBetaCli: boolean
excludeLinksFromLockfile: boolean
extraBinPaths: string[]
extraEnv: Record<string, string>
filter: string[]

View File

@@ -47,6 +47,7 @@ export const types = Object.assign({
dir: String,
'enable-modules-dir': Boolean,
'enable-pre-post-scripts': Boolean,
'exclude-links-from-lockfile': Boolean,
'extend-node-path': Boolean,
'fetch-timeout': Number,
'fetching-concurrency': Number,
@@ -192,6 +193,7 @@ export async function getConfig (
'dedupe-peer-dependents': true,
'dedupe-direct-deps': false,
'enable-modules-dir': true,
'exclude-links-from-lockfile': false,
'extend-node-path': true,
'fetch-retries': 2,
'fetch-retry-factor': 10,

View File

@@ -237,3 +237,73 @@ test('path to external link is not added to the lockfile, when it resolves a pee
expect(lockfile.packages[key]).toBeTruthy()
expect(lockfile.packages[key].dependencies?.['@pnpm.e2e/peer-a']).toBe('link:node_modules/@pnpm.e2e/peer-a')
})
test('links resolved from workspace protocol dependencies are not removed', async () => {
const pkg1 = {
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
'project-2': 'workspace:1.0.0',
},
}
const pkg2 = {
name: 'project-2',
version: '1.0.0',
dependencies: {
'is-negative': '1.0.0',
},
}
preparePackages([pkg1, pkg2])
const importers: MutatedProject[] = [
{
mutation: 'install',
rootDir: path.resolve('project-1'),
},
{
mutation: 'install',
rootDir: path.resolve('project-2'),
},
]
const allProjects = [
{
buildIndex: 0,
manifest: pkg1,
rootDir: path.resolve('project-1'),
},
{
buildIndex: 0,
manifest: pkg2,
rootDir: path.resolve('project-2'),
},
]
const workspacePackages = {
'project-1': {
'1.0.0': {
dir: path.resolve('project-1'),
manifest: pkg1,
},
},
'project-2': {
'1.0.0': {
dir: path.resolve('project-2'),
manifest: pkg2,
},
},
}
await mutateModules(importers, await testDefaults({
allProjects,
excludeLinksFromLockfile: true,
lockfileOnly: true,
workspacePackages,
}))
const lockfile: LockfileV6 = await readYamlFile(WANTED_LOCKFILE)
expect(lockfile.importers['project-1'].dependencies?.['project-2']).toStrictEqual({
specifier: 'workspace:1.0.0',
version: 'link:../project-2',
})
})

View File

@@ -387,8 +387,16 @@ function addDirectDependenciesToLockfile (
const allDeps = Array.from(new Set(Object.keys(getAllDependenciesFromManifest(newManifest))))
for (const alias of allDeps) {
if (directDependenciesByAlias[alias] && (!excludeLinksFromLockfile || !(directDependenciesByAlias[alias] as LinkedDependency).isLinkedDependency)) {
const dep = directDependenciesByAlias[alias]
const dep = directDependenciesByAlias[alias]
const spec = dep && getSpecFromPackageManifest(newManifest, dep.alias)
if (
dep &&
(
!excludeLinksFromLockfile ||
!(dep as LinkedDependency).isLinkedDependency ||
spec.startsWith('workspace:')
)
) {
const ref = depPathToRef(dep.pkgId, {
alias: dep.alias,
realName: dep.name,
@@ -402,7 +410,7 @@ function addDirectDependenciesToLockfile (
} else {
newProjectSnapshot.dependencies[dep.alias] = ref
}
newProjectSnapshot.specifiers[dep.alias] = getSpecFromPackageManifest(newManifest, dep.alias)
newProjectSnapshot.specifiers[dep.alias] = spec
} else if (projectSnapshot.specifiers[alias]) {
newProjectSnapshot.specifiers[alias] = projectSnapshot.specifiers[alias]
if (projectSnapshot.dependencies?.[alias]) {