Skip to content

Commit

Permalink
perf: don't fetch deps that are already in node_modules, when node-li…
Browse files Browse the repository at this point in the history
…nker=hoisted is used
  • Loading branch information
zkochan committed Dec 14, 2022
1 parent 29fc688 commit 3e3261f
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 45 deletions.
2 changes: 2 additions & 0 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -344,6 +344,7 @@ export async function mutateModules (
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
},
currentDependenciesLocations: ctx.modulesFile?.locations,
patchedDependencies: patchedDependenciesWithResolvedPath,
selectedProjectDirs: projects.map((project) => project.rootDir),
allProjects: ctx.projects,
Expand Down Expand Up @@ -1177,6 +1178,7 @@ const installInContext: InstallFunction = async (projects, ctx, opts) => {
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
},
currentDependenciesLocations: ctx.modulesFile?.locations,
selectedProjectDirs: projects.map((project) => project.rootDir),
allProjects: ctx.projects,
prunedAt: ctx.modulesFile?.prunedAt,
Expand Down
1 change: 1 addition & 0 deletions pkg-manager/headless/src/index.ts
Expand Up @@ -120,6 +120,7 @@ export interface HeadlessOptions {
hoistedDependencies: HoistedDependencies
hoistPattern?: string[]
publicHoistPattern?: string[]
currentDependenciesLocations?: Record<string, string[]>
lockfileDir: string
modulesDir?: string
virtualStoreDir?: string
Expand Down
56 changes: 29 additions & 27 deletions pkg-manager/headless/src/linkHoistedModules.ts
Expand Up @@ -95,36 +95,38 @@ async function linkAllPkgsInOrder (
await Promise.all(
Object.entries(hierarchy).map(async ([dir, deps]) => {
const depNode = graph[dir]
let filesResponse!: PackageFilesResponse
try {
filesResponse = await depNode.fetchingFiles()
} catch (err: any) { // eslint-disable-line
if (depNode.optional) return
throw err
}
if (depNode.fetchingFiles) {
let filesResponse!: PackageFilesResponse
try {
filesResponse = await depNode.fetchingFiles()
} catch (err: any) { // eslint-disable-line
if (depNode.optional) return
throw err
}

let sideEffectsCacheKey: string | undefined
if (opts.sideEffectsCacheRead && filesResponse.sideEffects && !isEmpty(filesResponse.sideEffects)) {
sideEffectsCacheKey = _calcDepState(dir, {
isBuilt: !opts.ignoreScripts && depNode.requiresBuild,
patchFileHash: depNode.patchFile?.hash,
})
}
const { importMethod, isBuilt } = await storeController.importPackage(depNode.dir, {
filesResponse,
force: opts.force || depNode.depPath !== prevGraph[dir]?.depPath,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
sideEffectsCacheKey,
})
if (importMethod) {
progressLogger.debug({
method: importMethod,
requester: opts.lockfileDir,
status: 'imported',
to: depNode.dir,
let sideEffectsCacheKey: string | undefined
if (opts.sideEffectsCacheRead && filesResponse.sideEffects && !isEmpty(filesResponse.sideEffects)) {
sideEffectsCacheKey = _calcDepState(dir, {
isBuilt: !opts.ignoreScripts && depNode.requiresBuild,
patchFileHash: depNode.patchFile?.hash,
})
}
const { importMethod, isBuilt } = await storeController.importPackage(depNode.dir, {
filesResponse,
force: opts.force || depNode.depPath !== prevGraph[dir]?.depPath,
requiresBuild: depNode.requiresBuild || depNode.patchFile != null,
sideEffectsCacheKey,
})
if (importMethod) {
progressLogger.debug({
method: importMethod,
requester: opts.lockfileDir,
status: 'imported',
to: depNode.dir,
})
}
depNode.isBuilt = isBuilt
}
depNode.isBuilt = isBuilt
return linkAllPkgsInOrder(storeController, graph, prevGraph, deps, dir, opts)
})
)
Expand Down
43 changes: 25 additions & 18 deletions pkg-manager/headless/src/lockfileToHoistedDepGraph.ts
Expand Up @@ -32,6 +32,7 @@ export interface LockfileToHoistedDepGraphOptions {
externalDependencies?: Set<string>
importerIds: string[]
include: IncludedDependencies
currentDependenciesLocations?: Record<string, string[]>
lockfileDir: string
nodeVersion: string
pnpmVersion: string
Expand Down Expand Up @@ -176,25 +177,31 @@ async function fetchDeps (
return
}
const dir = path.join(modules, dep.name)
const depLocation = path.relative(opts.lockfileDir, dir)
const resolution = pkgSnapshotToResolution(depPath, pkgSnapshot, opts.registries)
let fetchResponse!: ReturnType<FetchPackageToStoreFunction>
try {
fetchResponse = opts.storeController.fetchPackage({
force: false,
lockfileDir: opts.lockfileDir,
pkg: {
id: packageId,
resolution,
},
expectedPkg: {
name: pkgName,
version: pkgVersion,
},
})
if (fetchResponse instanceof Promise) fetchResponse = await fetchResponse
} catch (err: any) { // eslint-disable-line
if (pkgSnapshot.optional) return
throw err
const skipFetch = opts.currentDependenciesLocations?.[depPath]?.includes(depLocation)
if (skipFetch) {
fetchResponse = {} as any // eslint-disable-line @typescript-eslint/no-explicit-any
} else {
try {
fetchResponse = opts.storeController.fetchPackage({
force: false,
lockfileDir: opts.lockfileDir,
pkg: {
id: packageId,
resolution,
},
expectedPkg: {
name: pkgName,
version: pkgVersion,
},
})
if (fetchResponse instanceof Promise) fetchResponse = await fetchResponse
} catch (err: any) { // eslint-disable-line
if (pkgSnapshot.optional) return
throw err
}
}
opts.graph[dir] = {
alias: dep.name,
Expand Down Expand Up @@ -222,7 +229,7 @@ async function fetchDeps (
if (!locations[depPath]) {
locations[depPath] = []
}
locations[depPath].push(path.relative(opts.lockfileDir, dir))
locations[depPath].push(depLocation)
opts.graph[dir].children = getChildren(pkgSnapshot, opts.pkgLocationsByDepPath, opts)
}))
return depHierarchy
Expand Down

0 comments on commit 3e3261f

Please sign in to comment.