feat: store ignored dependencies list in modules state file (#8903)

This commit is contained in:
Zoltan Kochan
2024-12-24 03:01:35 +01:00
committed by GitHub
parent 512465c4ce
commit 4771813309
7 changed files with 36 additions and 10 deletions

View File

@@ -0,0 +1,9 @@
---
"@pnpm/plugin-commands-rebuild": minor
"@pnpm/modules-yaml": minor
"@pnpm/headless": minor
"@pnpm/build-modules": minor
"@pnpm/core": minor
---
Store the list of ignored builds in `node_modules/.modules.yaml`.

View File

@@ -44,7 +44,7 @@ export async function buildModules<T extends string> (
rootModulesDir: string
hoistedLocations?: Record<string, string[]>
}
): Promise<void> {
): Promise<{ ignoredBuilds: string[] }> {
const warn = (message: string) => {
logger.warn({ message, prefix: opts.lockfileDir })
}
@@ -83,7 +83,9 @@ export async function buildModules<T extends string> (
)
})
await runGroups(opts.childConcurrency ?? 4, groups)
ignoredScriptsLogger.debug({ packageNames: Array.from(ignoredPkgs) })
const packageNames = Array.from(ignoredPkgs)
ignoredScriptsLogger.debug({ packageNames })
return { ignoredBuilds: packageNames }
}
async function buildDependency<T extends string> (

View File

@@ -151,7 +151,7 @@ export async function rebuildProjects (
idsToRebuild = Object.keys(ctx.currentLockfile.packages)
}
const pkgsThatWereRebuilt = await _rebuild(
const { pkgsThatWereRebuilt, ignoredPkgs } = await _rebuild(
{
pkgsToRebuild: new Set(idsToRebuild),
...ctx,
@@ -192,6 +192,7 @@ export async function rebuildProjects (
hoistedDependencies: ctx.hoistedDependencies,
hoistPattern: ctx.hoistPattern,
included: ctx.include,
ignoredBuilds: ignoredPkgs,
layoutVersion: LAYOUT_VERSION,
packageManager: `${opts.packageManager.name}@${opts.packageManager.version}`,
pendingBuilds: ctx.pendingBuilds,
@@ -246,7 +247,7 @@ async function _rebuild (
extraNodePaths: string[]
} & Pick<PnpmContext, 'modulesFile'>,
opts: StrictRebuildOptions
): Promise<Set<string>> {
): Promise<{ pkgsThatWereRebuilt: Set<string>, ignoredPkgs: string[] }> {
const depGraph = lockfileToDepGraph(ctx.currentLockfile)
const depsStateCache: DepsStateCache = {}
const pkgsThatWereRebuilt = new Set<string>()
@@ -286,7 +287,13 @@ async function _rebuild (
logger.info({ message, prefix: opts.dir })
}
const allowBuild = createAllowBuildFunction(opts) ?? (() => true)
const ignoredPkgs: string[] = []
const _allowBuild = createAllowBuildFunction(opts) ?? (() => true)
const allowBuild = (pkgName: string) => {
if (_allowBuild(pkgName)) return true
ignoredPkgs.push(pkgName)
return false
}
const builtDepPaths = new Set<string>()
const groups = chunks.map((chunk) => chunk.filter((depPath) => ctx.pkgsToRebuild.has(depPath) && !ctx.skipped.has(depPath)).map((depPath) =>
@@ -419,7 +426,7 @@ async function _rebuild (
})))
}
return pkgsThatWereRebuilt
return { pkgsThatWereRebuilt, ignoredPkgs }
}
function binDirsInAllParentDirs (pkgRoot: string, lockfileDir: string): string[] {

View File

@@ -1081,6 +1081,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
ctx.pendingBuilds = ctx.pendingBuilds
.filter((relDepPath) => !result.removedDepPaths.has(relDepPath))
let ignoredBuilds: string[] | undefined
if (result.newDepPaths?.length) {
if (opts.ignoreScripts) {
// we can use concat here because we always only append new packages, which are guaranteed to not be there by definition
@@ -1101,7 +1102,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(dependenciesGraph, rootNodes, {
ignoredBuilds = (await buildModules(dependenciesGraph, rootNodes, {
allowBuild: createAllowBuildFunction(opts),
childConcurrency: opts.childConcurrency,
depsStateCache,
@@ -1122,7 +1123,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
storeController: opts.storeController,
unsafePerm: opts.unsafePerm,
userAgent: opts.userAgent,
})
})).ignoredBuilds
}
}
@@ -1234,6 +1235,7 @@ const _installInContext: InstallFunction = async (projects, ctx, opts) => {
hoistPattern: ctx.hoistPattern,
included: ctx.include,
injectedDeps,
ignoredBuilds,
layoutVersion: LAYOUT_VERSION,
nodeLinker: opts.nodeLinker,
packageManager: `${opts.packageManager.name}@${opts.packageManager.version}`,

View File

@@ -498,6 +498,7 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
.map(({ depPath }) => depPath)
)
}
let ignoredBuilds: string[] | undefined
if ((!opts.ignoreScripts || Object.keys(opts.patchedDependencies ?? {}).length > 0) && opts.enableModulesDir !== false) {
const directNodes = new Set<string>()
for (const id of union(importerIds, ['.'])) {
@@ -519,7 +520,7 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
...makeNodeRequireOption(path.join(opts.lockfileDir, '.pnp.cjs')),
}
}
await buildModules(graph, Array.from(directNodes), {
ignoredBuilds = (await buildModules(graph, Array.from(directNodes), {
allowBuild: createAllowBuildFunction(opts),
childConcurrency: opts.childConcurrency,
extraBinPaths,
@@ -539,7 +540,7 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
storeController: opts.storeController,
unsafePerm: opts.unsafePerm,
userAgent: opts.userAgent,
})
})).ignoredBuilds
}
const projectsToBeBuilt = extendProjectsWithTargetDirs(selectedProjects, wantedLockfile, {
@@ -600,6 +601,7 @@ export async function headlessInstall (opts: HeadlessOptions): Promise<Installat
hoistPattern: opts.hoistPattern,
included: opts.include,
injectedDeps,
ignoredBuilds,
layoutVersion: LAYOUT_VERSION,
hoistedLocations,
nodeLinker: opts.nodeLinker,

View File

@@ -22,6 +22,7 @@ export interface Modules {
nodeLinker?: 'hoisted' | 'isolated' | 'pnp'
packageManager: string
pendingBuilds: string[]
ignoredBuilds?: string[]
prunedAt: string
registries?: Registries // nullable for backward compatibility
shamefullyHoist?: boolean // for backward compatibility

View File

@@ -15,6 +15,7 @@ test('writeModulesManifest() and readModulesManifest()', async () => {
devDependencies: true,
optionalDependencies: true,
},
ignoredBuilds: [],
layoutVersion: 1,
packageManager: 'pnpm@2',
pendingBuilds: [],
@@ -72,6 +73,7 @@ test('readModulesManifest() should not create a node_modules directory if it doe
devDependencies: true,
optionalDependencies: true,
},
ignoredBuilds: [],
layoutVersion: 1,
packageManager: 'pnpm@2',
pendingBuilds: [],
@@ -99,6 +101,7 @@ test('readModulesManifest() should create a node_modules directory if makeModule
devDependencies: true,
optionalDependencies: true,
},
ignoredBuilds: [],
layoutVersion: 1,
packageManager: 'pnpm@2',
pendingBuilds: [],