Skip to content

Commit

Permalink
refactor: hoisting
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Dec 18, 2022
1 parent e96fb10 commit ed24205
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 18 deletions.
1 change: 1 addition & 0 deletions fs/indexed-pkg-importer/src/importIndexedDir.ts
Expand Up @@ -126,6 +126,7 @@ async function moveOrMergeModulesDirs (src: string, dest: string) {
return
case 'ENOTEMPTY':
case 'EPERM': // This error code is thrown on Windows
// The newly added dependency might have node_modules if it has bundled dependencies.
await mergeModulesDirs(src, dest)
return
default:
Expand Down
4 changes: 2 additions & 2 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -344,7 +344,7 @@ export async function mutateModules (
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
},
currentDependenciesLocations: ctx.modulesFile?.locations,
currentHoistedLocations: ctx.modulesFile?.hoistedLocations,
patchedDependencies: patchedDependenciesWithResolvedPath,
selectedProjectDirs: projects.map((project) => project.rootDir),
allProjects: ctx.projects,
Expand Down Expand Up @@ -1178,7 +1178,7 @@ const installInContext: InstallFunction = async (projects, ctx, opts) => {
nodeVersion: opts.nodeVersion,
pnpmVersion: opts.packageManager.name === 'pnpm' ? opts.packageManager.version : '',
},
currentDependenciesLocations: ctx.modulesFile?.locations,
currentHoistedLocations: ctx.modulesFile?.hoistedLocations,
selectedProjectDirs: projects.map((project) => project.rootDir),
allProjects: ctx.projects,
prunedAt: ctx.modulesFile?.prunedAt,
Expand Down
6 changes: 3 additions & 3 deletions pkg-manager/headless/src/index.ts
Expand Up @@ -120,7 +120,7 @@ export interface HeadlessOptions {
hoistedDependencies: HoistedDependencies
hoistPattern?: string[]
publicHoistPattern?: string[]
currentDependenciesLocations?: Record<string, string[]>
currentHoistedLocations?: Record<string, string[]>
lockfileDir: string
modulesDir?: string
virtualStoreDir?: string
Expand Down Expand Up @@ -284,7 +284,7 @@ export async function headlessInstall (opts: HeadlessOptions) {
directDependenciesByImporterId,
graph,
hierarchy,
locations,
hoistedLocations,
pkgLocationsByDepPath,
prevGraph,
symlinkedDirectDependenciesByImporterId,
Expand Down Expand Up @@ -530,7 +530,7 @@ export async function headlessInstall (opts: HeadlessOptions) {
included: opts.include,
injectedDeps,
layoutVersion: LAYOUT_VERSION,
locations,
hoistedLocations,
nodeLinker: opts.nodeLinker,
packageManager: `${opts.packageManager.name}@${opts.packageManager.version}`,
pendingBuilds: opts.pendingBuilds,
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/headless/src/lockfileToDepGraph.ts
Expand Up @@ -80,7 +80,7 @@ export interface LockfileToDepGraphResult {
directDependenciesByImporterId: DirectDependenciesByImporterId
graph: DependenciesGraph
hierarchy?: DepHierarchy
locations?: Record<string, string[]>
hoistedLocations?: Record<string, string[]>
symlinkedDirectDependenciesByImporterId?: DirectDependenciesByImporterId
prevGraph?: DependenciesGraph
pkgLocationsByDepPath?: Record<string, string[]>
Expand Down
22 changes: 11 additions & 11 deletions pkg-manager/headless/src/lockfileToHoistedDepGraph.ts
Expand Up @@ -32,7 +32,7 @@ export interface LockfileToHoistedDepGraphOptions {
externalDependencies?: Set<string>
importerIds: string[]
include: IncludedDependencies
currentDependenciesLocations?: Record<string, string[]>
currentHoistedLocations?: Record<string, string[]>
lockfileDir: string
nodeVersion: string
pnpmVersion: string
Expand Down Expand Up @@ -74,10 +74,10 @@ async function _lockfileToHoistedDepGraph (
lockfile,
graph,
pkgLocationsByDepPath: {},
hoistedLocations: {} as Record<string, string[]>,
}
const locations: Record<string, string[]> = {}
const hierarchy = {
[opts.lockfileDir]: await fetchDeps(fetchDepsOpts, modulesDir, tree.dependencies, locations),
[opts.lockfileDir]: await fetchDeps(fetchDepsOpts, modulesDir, tree.dependencies),
}
const directDependenciesByImporterId: DirectDependenciesByImporterId = {
'.': directDepsMap(Object.keys(hierarchy[opts.lockfileDir]), graph),
Expand All @@ -89,7 +89,7 @@ async function _lockfileToHoistedDepGraph (
const importerId = reference.replace('workspace:', '')
const projectDir = path.join(opts.lockfileDir, importerId)
const modulesDir = path.join(projectDir, 'node_modules')
const nextHierarchy = (await fetchDeps(fetchDepsOpts, modulesDir, rootDep.dependencies, locations))
const nextHierarchy = (await fetchDeps(fetchDepsOpts, modulesDir, rootDep.dependencies))
hierarchy[projectDir] = nextHierarchy

const importer = lockfile.importers[importerId]
Expand All @@ -104,7 +104,7 @@ async function _lockfileToHoistedDepGraph (
hierarchy,
pkgLocationsByDepPath: fetchDepsOpts.pkgLocationsByDepPath,
symlinkedDirectDependenciesByImporterId,
locations,
hoistedLocations: fetchDepsOpts.hoistedLocations,
}
}

Expand Down Expand Up @@ -139,10 +139,10 @@ async function fetchDeps (
graph: DependenciesGraph
lockfile: Lockfile
pkgLocationsByDepPath: Record<string, string[]>
hoistedLocations: Record<string, string[]>
} & LockfileToHoistedDepGraphOptions,
modules: string,
deps: Set<HoisterResult>,
locations: Record<string, string[]>
): Promise<DepHierarchy> {
const depHierarchy = {}
await Promise.all(Array.from(deps).map(async (dep) => {
Expand Down Expand Up @@ -180,7 +180,7 @@ async function fetchDeps (
const depLocation = path.relative(opts.lockfileDir, dir)
const resolution = pkgSnapshotToResolution(depPath, pkgSnapshot, opts.registries)
let fetchResponse!: ReturnType<FetchPackageToStoreFunction>
const skipFetch = opts.currentDependenciesLocations?.[depPath]?.includes(depLocation)
const skipFetch = opts.currentHoistedLocations?.[depPath]?.includes(depLocation)
if (skipFetch) {
fetchResponse = {} as any // eslint-disable-line @typescript-eslint/no-explicit-any
} else {
Expand Down Expand Up @@ -225,11 +225,11 @@ async function fetchDeps (
opts.pkgLocationsByDepPath[depPath] = []
}
opts.pkgLocationsByDepPath[depPath].push(dir)
depHierarchy[dir] = await fetchDeps(opts, path.join(dir, 'node_modules'), dep.dependencies, locations)
if (!locations[depPath]) {
locations[depPath] = []
depHierarchy[dir] = await fetchDeps(opts, path.join(dir, 'node_modules'), dep.dependencies)
if (!opts.hoistedLocations[depPath]) {
opts.hoistedLocations[depPath] = []
}
locations[depPath].push(depLocation)
opts.hoistedLocations[depPath].push(depLocation)
opts.graph[dir].children = getChildren(pkgSnapshot, opts.pkgLocationsByDepPath, opts)
}))
return depHierarchy
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/modules-yaml/src/index.ts
Expand Up @@ -31,7 +31,7 @@ export interface Modules {
storeDir: string
virtualStoreDir: string
injectedDeps?: Record<string, string[]>
locations?: Record<string, string[]>
hoistedLocations?: Record<string, string[]>
}

export async function readModulesManifest (modulesDir: string): Promise<Modules | null> {
Expand Down

0 comments on commit ed24205

Please sign in to comment.