Skip to content

Commit

Permalink
refactor: create a separate type for DepPath
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 17, 2024
1 parent 347d824 commit 021516a
Show file tree
Hide file tree
Showing 24 changed files with 136 additions and 99 deletions.
6 changes: 4 additions & 2 deletions deps/graph-builder/src/lockfileToDepGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
import { logger } from '@pnpm/logger'
import { type IncludedDependencies } from '@pnpm/modules-yaml'
import { packageIsInstallable } from '@pnpm/package-is-installable'
import { type DepPath, type SupportedArchitectures, type PatchFile, type Registries } from '@pnpm/types'
import { type DepPath, type SupportedArchitectures, type PatchFile, type Registries, type PkgId } from '@pnpm/types'
import {
type PkgRequestFetchResult,
type FetchPackageToStoreFunction,
Expand All @@ -38,7 +38,8 @@ export interface DependenciesGraphNode {
children: Record<string, string>
optionalDependencies: Set<string>
optional: boolean
depPath: string // this option is only needed for saving pendingBuild when running with --ignore-scripts flag
depPath: DepPath // this option is only needed for saving pendingBuild when running with --ignore-scripts flag
packageId: PkgId
isBuilt?: boolean
requiresBuild?: boolean
hasBin: boolean
Expand Down Expand Up @@ -183,6 +184,7 @@ export async function lockfileToDepGraph (
}
graph[dir] = {
children: {},
packageId,
depPath,
dir,
fetching: fetchResponse.fetching,
Expand Down
25 changes: 12 additions & 13 deletions exec/build-modules/src/buildSequence.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { graphSequencer } from '@pnpm/deps.graph-sequencer'
import { type PackageManifest, type PatchFile } from '@pnpm/types'
import { type DepPath, type PackageManifest, type PatchFile, type PkgId } from '@pnpm/types'
import filter from 'ramda/src/filter'

export interface DependenciesGraphNode {
children: Record<string, string>
depPath: string
export interface DependenciesGraphNode<T extends string> {
children: Record<string, T>
depPath: DepPath
packageId: PkgId
name: string
dir: string
fetchingBundledManifest?: () => Promise<PackageManifest | undefined>
Expand All @@ -20,14 +21,12 @@ export interface DependenciesGraphNode {
patchFile?: PatchFile
}

export interface DependenciesGraph {
[depPath: string]: DependenciesGraphNode
}
export type DependenciesGraph<T extends string> = Record<T, DependenciesGraphNode<T>>

export function buildSequence (
depGraph: Record<string, Pick<DependenciesGraphNode, 'children' | 'requiresBuild'>>,
export function buildSequence<T extends string> (
depGraph: Record<string, Pick<DependenciesGraphNode<T>, 'children' | 'requiresBuild'>>,
rootDepPaths: string[]
): string[][] {
): T[][] {
const nodesToBuild = new Set<string>()
getSubgraphToBuild(depGraph, rootDepPaths, nodesToBuild, new Set<string>())
const onlyFromBuildGraph = filter((depPath: string) => nodesToBuild.has(depPath))
Expand All @@ -37,12 +36,12 @@ export function buildSequence (
.map((depPath) => [depPath, onlyFromBuildGraph(Object.values(depGraph[depPath].children))])
)
const graphSequencerResult = graphSequencer(graph, nodesToBuildArray)
const chunks = graphSequencerResult.chunks as string[][]
const chunks = graphSequencerResult.chunks as T[][]
return chunks
}

function getSubgraphToBuild (
graph: Record<string, Pick<DependenciesGraphNode, 'children' | 'requiresBuild' | 'patchFile'>>,
function getSubgraphToBuild<T extends string> (
graph: Record<string, Pick<DependenciesGraphNode<T>, 'children' | 'requiresBuild' | 'patchFile'>>,
entryNodes: string[],
nodesToBuild: Set<string>,
walked: Set<string>
Expand Down
22 changes: 11 additions & 11 deletions exec/build-modules/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import { buildSequence, type DependenciesGraph, type DependenciesGraphNode } fro

export type { DepsStateCache }

export async function buildModules (
depGraph: DependenciesGraph,
export async function buildModules<T extends string> (
depGraph: DependenciesGraph<T>,
rootDepPaths: string[],
opts: {
allowBuild?: (pkgName: string) => boolean
Expand Down Expand Up @@ -55,7 +55,7 @@ export async function buildModules (
builtHoistedDeps: opts.hoistedLocations ? {} : undefined,
warn,
}
const chunks = buildSequence(depGraph, rootDepPaths)
const chunks = buildSequence<T>(depGraph, rootDepPaths)
const ignoredPkgs = new Set<string>()
const allowBuild = opts.allowBuild
? (pkgName: string) => {
Expand All @@ -73,7 +73,7 @@ export async function buildModules (
chunk = chunk.filter((depPath) => opts.depsToBuild!.has(depPath))
}

return chunk.map((depPath: string) =>
return chunk.map((depPath) =>
async () => {
return buildDependency(depPath, depGraph, {
...buildDepOpts,
Expand All @@ -91,9 +91,9 @@ export async function buildModules (
}
}

async function buildDependency (
depPath: string,
depGraph: DependenciesGraph,
async function buildDependency<T extends string> (
depPath: T,
depGraph: DependenciesGraph<T>,
opts: {
extraBinPaths?: string[]
extraNodePaths?: string[]
Expand Down Expand Up @@ -204,17 +204,17 @@ async function buildDependency (
}
}

export async function linkBinsOfDependencies (
depNode: DependenciesGraphNode,
depGraph: DependenciesGraph,
export async function linkBinsOfDependencies<T extends string> (
depNode: DependenciesGraphNode<T>,
depGraph: DependenciesGraph<T>,
opts: {
extraNodePaths?: string[]
optional: boolean
preferSymlinkedExecutables?: boolean
warn: (message: string) => void
}
): Promise<void> {
const childrenToLink: Record<string, string> = opts.optional
const childrenToLink: Record<string, T> = opts.optional
? depNode.children
: pickBy((child, childAlias) => !depNode.optionalDependencies.has(childAlias), depNode.children)

Expand Down
3 changes: 3 additions & 0 deletions lockfile/detect-dep-types/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../packages/dependency-path"
},
{
"path": "../../packages/types"
},
{
"path": "../lockfile-types"
}
Expand Down
2 changes: 1 addition & 1 deletion lockfile/lockfile-file/src/write.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs } from 'fs'
import path from 'path'
import { LockfileFileV9, type Lockfile, type LockfileFile } from '@pnpm/lockfile-types'
import { type LockfileFileV9, type Lockfile, type LockfileFile } from '@pnpm/lockfile-types'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import rimraf from '@zkochan/rimraf'
import yaml from 'js-yaml'
Expand Down
6 changes: 3 additions & 3 deletions lockfile/lockfile-utils/src/packageIdFromSnapshot.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { type DepPath } from '@pnpm/types'
import { type DepPath, type PkgId } from '@pnpm/types'
import { type PackageSnapshot } from '@pnpm/lockfile-types'
import * as dp from '@pnpm/dependency-path'

export function packageIdFromSnapshot (
depPath: DepPath,
pkgSnapshot: PackageSnapshot
): string {
if (pkgSnapshot.id) return pkgSnapshot.id
): PkgId {
if (pkgSnapshot.id) return pkgSnapshot.id as PkgId
return dp.tryGetPackageId(depPath) ?? depPath
}
3 changes: 3 additions & 0 deletions lockfile/merge-lockfile-changes/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"../../__typings__/**/*.d.ts"
],
"references": [
{
"path": "../../packages/types"
},
{
"path": "../lockfile-types"
}
Expand Down
3 changes: 2 additions & 1 deletion modules-mounter/daemon/src/createFuseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { type Lockfile, readWantedLockfile, type PackageSnapshot, type TarballRe
import {
nameVerFromPkgSnapshot,
} from '@pnpm/lockfile-utils'
import { type DepPath } from '@pnpm/types'
import * as schemas from 'hyperdrive-schemas'
import loadJsonFile from 'load-json-file'
import Fuse from 'fuse-native'
Expand Down Expand Up @@ -177,7 +178,7 @@ export function createFuseHandlersFromLockfile (lockfile: Lockfile, cafsDir: str
}
function getPkgInfo (depPath: string, cafsDir: string) {
if (!pkgSnapshotCache.has(depPath)) {
const pkgSnapshot = lockfile.packages?.[depPath]
const pkgSnapshot = lockfile.packages?.[depPath as DepPath]
if (pkgSnapshot == null) return undefined
const indexPath = getFilePathInCafs(cafsDir, (pkgSnapshot.resolution as TarballResolution).integrity!, 'index')
pkgSnapshotCache.set(depPath, {
Expand Down
2 changes: 1 addition & 1 deletion modules-mounter/daemon/src/makeVirtualNodeModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function createVirtualStoreDir (lockfile: Lockfile): Record<string, DirEntry> {
for (const [depName, ref] of Object.entries({ ...pkgSnapshot.dependencies, ...pkgSnapshot.optionalDependencies })) {
const symlink: DirEntry = {
entryType: 'symlink',
target: normalize(path.relative(`${currentPath}/node_modules/`, `${dp.depPathToFilename(dp.refToRelative(ref, depName)!, 120)}/node_modules/${depName}`)),
target: normalize(path.relative(`${currentPath}/node_modules/`, `${dp.depPathToFilename(dp.refToRelative(ref as string, depName)!, 120)}/node_modules/${depName}`)),
}
addDirEntry(pkgNodeModules, depName, symlink)
}
Expand Down
2 changes: 2 additions & 0 deletions packages/calc-dep-state/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@pnpm/crypto.object-hasher": "workspace:*",
"@pnpm/dependency-path": "workspace:*",
"@pnpm/lockfile-types": "workspace:*",
"@pnpm/lockfile-utils": "workspace:*",
"@pnpm/types": "workspace:*",
"sort-keys": "^4.2.0"
},
"devDependencies": {
Expand Down
44 changes: 22 additions & 22 deletions packages/calc-dep-state/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { ENGINE_NAME } from '@pnpm/constants'
import { refToRelative } from '@pnpm/dependency-path'
import { type Lockfile } from '@pnpm/lockfile-types'
import { packageIdFromSnapshot } from '@pnpm/lockfile-utils'
import { type DepPath, type PkgId } from '@pnpm/types'
import { hashObjectWithoutSorting } from '@pnpm/crypto.object-hasher'
import sortKeys from 'sort-keys'

export interface DepsGraph {
[depPath: string]: DepsGraphNode
}
export type DepsGraph<T extends string> = Record<T, DepsGraphNode<T>>

export interface DepsGraphNode {
children: { [alias: string]: string }
depPath: string
export interface DepsGraphNode<T extends string> {
children: { [alias: string]: T }
packageId: PkgId
}

export interface DepsStateCache {
Expand All @@ -21,8 +21,8 @@ export interface DepStateObj {
[depPath: string]: DepStateObj
}

export function calcDepState (
depsGraph: DepsGraph,
export function calcDepState<T extends string> (
depsGraph: DepsGraph<T>,
cache: DepsStateCache,
depPath: string,
opts: {
Expand All @@ -41,49 +41,49 @@ export function calcDepState (
return result
}

function calcDepStateObj (
depPath: string,
depsGraph: DepsGraph,
function calcDepStateObj<T extends string> (
depPath: T,
depsGraph: DepsGraph<T>,
cache: DepsStateCache,
parents: Set<string>
parents: Set<PkgId>
): DepStateObj {
if (cache[depPath]) return cache[depPath]
const node = depsGraph[depPath]
if (!node) return {}
const nextParents = new Set([...Array.from(parents), node.depPath])
const nextParents = new Set([...Array.from(parents), node.packageId])
const state: DepStateObj = {}
for (const childId of Object.values(node.children)) {
const child = depsGraph[childId]
if (!child) continue
if (parents.has(child.depPath)) {
state[child.depPath] = {}
if (parents.has(child.packageId)) {
state[child.packageId] = {}
continue
}
state[child.depPath] = calcDepStateObj(childId, depsGraph, cache, nextParents)
state[child.packageId] = calcDepStateObj(childId, depsGraph, cache, nextParents)
}
cache[depPath] = sortKeys(state)
return cache[depPath]
}

export function lockfileToDepGraph (lockfile: Lockfile): DepsGraph {
const graph: DepsGraph = {}
export function lockfileToDepGraph (lockfile: Lockfile): DepsGraph<DepPath> {
const graph: DepsGraph<DepPath> = {}
if (lockfile.packages != null) {
for (const [depPath, pkgSnapshot] of Object.entries(lockfile.packages)) {
const children = lockfileDepsToGraphChildren({
...pkgSnapshot.dependencies,
...pkgSnapshot.optionalDependencies,
})
graph[depPath] = {
graph[depPath as DepPath] = {
children,
depPath,
packageId: packageIdFromSnapshot(depPath as DepPath, pkgSnapshot),
}
}
}
return graph
}

function lockfileDepsToGraphChildren (deps: Record<string, string>): Record<string, string> {
const children: Record<string, string> = {}
function lockfileDepsToGraphChildren (deps: Record<string, string>): Record<string, DepPath> {
const children: Record<string, DepPath> = {}
for (const [alias, reference] of Object.entries(deps)) {
const depPath = refToRelative(reference, alias)
if (depPath) {
Expand Down
6 changes: 6 additions & 0 deletions packages/calc-dep-state/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@
{
"path": "../../lockfile/lockfile-types"
},
{
"path": "../../lockfile/lockfile-utils"
},
{
"path": "../constants"
},
{
"path": "../dependency-path"
},
{
"path": "../types"
}
],
"composite": true
Expand Down
18 changes: 11 additions & 7 deletions packages/dependency-path/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createBase32Hash } from '@pnpm/crypto.base32-hash'
import { type DepPath, type PkgResolutionId, type Registries } from '@pnpm/types'
import { type DepPath, type PkgResolutionId, type Registries, type PkgId } from '@pnpm/types'
import semver from 'semver'

export function isAbsolute (dependencyPath: string): boolean {
Expand All @@ -15,6 +15,9 @@ export function indexOfPeersSuffix (depPath: string): number {
} else if (depPath[i] === ')') {
open++
} else if (!open) {
if (depPath.substring(i + 1).startsWith('(patch_hash=')) {
return depPath.indexOf('(', i + 2)
}
return i + 1
}
}
Expand Down Expand Up @@ -48,15 +51,16 @@ export function removePeersSuffix (relDepPath: string): string {
return relDepPath
}

export function tryGetPackageId (relDepPath: string): string {
const sepIndex = indexOfPeersSuffix(relDepPath)
export function tryGetPackageId (relDepPath: DepPath): PkgId {
let pkgId: string = relDepPath
const sepIndex = indexOfPeersSuffix(pkgId)
if (sepIndex !== -1) {
relDepPath = relDepPath.substring(0, sepIndex)
pkgId = pkgId.substring(0, sepIndex)
}
if (relDepPath.includes(':')) {
relDepPath = relDepPath.substring(relDepPath.indexOf('@', 1) + 1)
if (pkgId.includes(':')) {
pkgId = pkgId.substring(pkgId.indexOf('@', 1) + 1)
}
return relDepPath
return pkgId as PkgId
}

export function getRegistryByPackageName (registries: Registries, packageName: string): string {
Expand Down
1 change: 1 addition & 0 deletions packages/dependency-path/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,5 @@ test('tryGetPackageId', () => {
expect(tryGetPackageId('/foo@1.0.0(@types/babel__core@7.1.14)')).toEqual('/foo@1.0.0')
expect(tryGetPackageId('/foo@1.0.0(@types/babel__core@7.1.14(is-odd@1.0.0))')).toEqual('/foo@1.0.0')
expect(tryGetPackageId('/@(-.-)/foo@1.0.0(@types/babel__core@7.1.14)')).toEqual('/@(-.-)/foo@1.0.0')
expect(tryGetPackageId('foo@1.0.0(patch=xxxx)(@types/babel__core@7.1.14)')).toEqual('foo@1.0.0')
})
2 changes: 2 additions & 0 deletions packages/types/src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ export interface PatchFile {

export type PkgResolutionId = string & { __brand: 'PkgResolutionId' }

export type PkgId = string & { __brand: 'PkgId' }

export type DepPath = string & { __brand: 'DepPath' }

0 comments on commit 021516a

Please sign in to comment.