fix: update the lockfile if a new project is added to the workspace with no deps (#6110)

This commit is contained in:
Zoltan Kochan
2023-02-20 11:39:58 +02:00
committed by GitHub
parent b9ab2e0bf2
commit 972de58abf
5 changed files with 126 additions and 1 deletions

View File

@@ -0,0 +1,7 @@
---
"@pnpm/headless": patch
"@pnpm/core": patch
"pnpm": patch
---
Update the lockfile if a workspace has a new project with no dependencies.

View File

@@ -0,0 +1,5 @@
---
"@pnpm/headless": minor
---
New option added: useLockfile.

View File

@@ -335,6 +335,8 @@ export async function mutateModules (
})
}
if (opts.lockfileOnly) {
// The lockfile will only be changed if the workspace will have new projects with no dependencies.
await writeWantedLockfile(ctx.lockfileDir, ctx.wantedLockfile)
return projects.map((mutatedProject) => ctx.projects[mutatedProject.rootDir])
}
if (!ctx.existsWantedLockfile) {

View File

@@ -14,6 +14,8 @@ import {
install,
mutateModules,
mutateModulesInSingleProject,
MutatedProject,
ProjectOptions,
} from '@pnpm/core'
import rimraf from '@zkochan/rimraf'
import loadJsonFile from 'load-json-file'
@@ -1423,3 +1425,99 @@ test('lockfile v5 is converted to lockfile v6', async () => {
expect(lockfile.packages).toHaveProperty(['/@pnpm.e2e/pkg-with-1-dep@100.0.0'])
}
})
test('update the lockfile when a new project is added to the workspace', async () => {
preparePackages([
{
location: 'project-1',
package: { name: 'project-1' },
},
])
const importers: MutatedProject[] = [
{
mutation: 'install',
rootDir: path.resolve('project-1'),
},
]
const allProjects: ProjectOptions[] = [
{
buildIndex: 0,
manifest: {
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
},
rootDir: path.resolve('project-1'),
},
]
await mutateModules(importers, await testDefaults({ allProjects }))
importers.push({
mutation: 'install',
rootDir: path.resolve('project-2'),
})
allProjects.push({
buildIndex: 0,
manifest: {
name: 'project-2',
version: '1.0.0',
},
rootDir: path.resolve('project-2'),
})
await mutateModules(importers, await testDefaults({ allProjects }))
const lockfile: Lockfile = await readYamlFile(WANTED_LOCKFILE)
expect(Object.keys(lockfile.importers)).toStrictEqual(['project-1', 'project-2'])
})
test('update the lockfile when a new project is added to the workspace and lockfile-only installation is used', async () => {
preparePackages([
{
location: 'project-1',
package: { name: 'project-1' },
},
])
const importers: MutatedProject[] = [
{
mutation: 'install',
rootDir: path.resolve('project-1'),
},
]
const allProjects: ProjectOptions[] = [
{
buildIndex: 0,
manifest: {
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
},
rootDir: path.resolve('project-1'),
},
]
await mutateModules(importers, await testDefaults({ allProjects, lockfileOnly: true }))
importers.push({
mutation: 'install',
rootDir: path.resolve('project-2'),
})
allProjects.push({
buildIndex: 0,
manifest: {
name: 'project-2',
version: '1.0.0',
},
rootDir: path.resolve('project-2'),
})
await mutateModules(importers, await testDefaults({ allProjects, lockfileOnly: true }))
const lockfile: Lockfile = await readYamlFile(WANTED_LOCKFILE)
expect(Object.keys(lockfile.importers)).toStrictEqual(['project-1', 'project-2'])
})

View File

@@ -28,6 +28,7 @@ import {
Lockfile,
readCurrentLockfile,
readWantedLockfile,
writeLockfiles,
writeCurrentLockfile,
PatchFile,
} from '@pnpm/lockfile-file'
@@ -152,6 +153,7 @@ export interface HeadlessOptions {
enableModulesDir?: boolean
nodeLinker?: 'isolated' | 'hoisted' | 'pnp'
useGitBranchLockfile?: boolean
useLockfile?: boolean
}
export async function headlessInstall (opts: HeadlessOptions) {
@@ -543,7 +545,18 @@ export async function headlessInstall (opts: HeadlessOptions) {
storeDir: opts.storeDir,
virtualStoreDir,
})
await writeCurrentLockfile(virtualStoreDir, filteredLockfile)
if (opts.useLockfile) {
// We need to write the wanted lockfile as well.
// Even though it will only be changed if the workspace will have new projects with no dependencies.
await writeLockfiles({
wantedLockfileDir: opts.lockfileDir,
currentLockfileDir: virtualStoreDir,
wantedLockfile,
currentLockfile: filteredLockfile,
})
} else {
await writeCurrentLockfile(virtualStoreDir, filteredLockfile)
}
}
// waiting till package requests are finished