Skip to content

Commit

Permalink
refactor: resolve dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed May 16, 2024
1 parent bcd4c4e commit 922c6ee
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function parentIdsContainsSequence (pkgIds: string[], pkgId1: string, pkgId2: string): boolean {
export function parentIdsContainSequence (pkgIds: string[], pkgId1: string, pkgId2: string): boolean {
const pkg1Index = pkgIds.indexOf(pkgId1)
if (pkg1Index === -1 || pkg1Index === pkgIds.length - 1) {
return false
Expand Down
12 changes: 6 additions & 6 deletions pkg-manager/resolve-dependencies/src/resolveDependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import { encodePkgId } from './encodePkgId'
import { getNonDevWantedDependencies, type WantedDependency } from './getNonDevWantedDependencies'
import { safeIntersect } from './mergePeers'
import { nextNodeId } from './nextNodeId'
import { parentIdsContainsSequence } from './parentIdsContainsSequence'
import { parentIdsContainSequence } from './parentIdsContainSequence'
import { hoistPeers, getHoistableOptionalPeers } from './hoistPeers'
import { wantedDepIsLocallyAvailable } from './wantedDepIsLocallyAvailable'
import { replaceVersionInPref } from './replaceVersionInPref'
Expand Down Expand Up @@ -243,7 +243,7 @@ export interface ResolvedPackage {
parentImporterIds: Set<string>
}

type ParentPkg = Pick<PkgAddress, 'nodeId' | 'installable' | 'depPath' | 'rootDir' | 'optional' | 'pkgId'>
type ParentPkg = Pick<PkgAddress, 'nodeId' | 'installable' | 'rootDir' | 'optional' | 'pkgId'>

export type ParentPkgAliases = Record<string, PkgAddress | true>

Expand Down Expand Up @@ -1200,7 +1200,7 @@ async function resolveDependency (
dependencyResolvedLogger.debug({
resolution: pkgResponse.body.id,
wanted: {
dependentId: options.parentPkg.depPath,
dependentId: options.parentPkg.pkgId,
name: wantedDependency.alias,
rawSpec: wantedDependency.pref,
},
Expand Down Expand Up @@ -1295,11 +1295,11 @@ async function resolveDependency (
// because zoo is a new parent package:
// foo > bar > qar > zoo > qar
if (
parentIdsContainsSequence(
parentIdsContainSequence(
options.parentIds,
options.parentPkg.pkgId,
pkgResponse.body.id
) || depPath === options.parentPkg.depPath
) || pkgResponse.body.id === options.parentPkg.pkgId
) {
return null
}
Expand Down Expand Up @@ -1348,7 +1348,7 @@ async function resolveDependency (
}
// In case of leaf dependencies (dependencies that have no prod deps or peer deps),
// we only ever need to analyze one leaf dep in a graph, so the nodeId can be short and stateless.
const nodeId = pkgIsLeaf(pkg) ? depPath : nextNodeId()
const nodeId = pkgIsLeaf(pkg) ? pkgResponse.body.id : nextNodeId()

const parentIsInstallable = options.parentPkg.installable === undefined || options.parentPkg.installable
const installable = parentIsInstallable && pkgResponse.body.isInstallable !== false
Expand Down
7 changes: 3 additions & 4 deletions pkg-manager/resolve-dependencies/src/resolveDependencyTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import partition from 'ramda/src/partition'
import zipObj from 'ramda/src/zipObj'
import { type WantedDependency } from './getNonDevWantedDependencies'
import { nextNodeId } from './nextNodeId'
import { parentIdsContainsSequence } from './parentIdsContainsSequence'
import { parentIdsContainSequence } from './parentIdsContainSequence'
import {
type ChildrenByParentId,
type DependenciesTree,
Expand Down Expand Up @@ -174,7 +174,6 @@ export async function resolveDependencyTree<T> (
installable: true,
nodeId: `>${importer.id}>`,
optional: false,
depPath: importer.id,
pkgId: importer.id,
rootDir: importer.rootDir,
},
Expand Down Expand Up @@ -206,7 +205,7 @@ export async function resolveDependencyTree<T> (

ctx.pendingNodes.forEach((pendingNode) => {
ctx.dependenciesTree.set(pendingNode.nodeId, {
children: () => buildTree(ctx, pendingNode.resolvedPackage.depPath,
children: () => buildTree(ctx, pendingNode.resolvedPackage.id,
pendingNode.parentIds,
ctx.childrenByParentId[pendingNode.resolvedPackage.id], pendingNode.depth + 1, pendingNode.installable),
depth: pendingNode.depth,
Expand Down Expand Up @@ -278,7 +277,7 @@ function buildTree (
childrenNodeIds[child.alias] = child.id
continue
}
if (parentIdsContainsSequence(parentIds, parentId, child.id) || parentId === child.id) {
if (parentIdsContainSequence(parentIds, parentId, child.id) || parentId === child.id) {
continue
}
const childNodeId = nextNodeId()
Expand Down
5 changes: 0 additions & 5 deletions pkg-manager/resolve-dependencies/test/nodeIdUtils.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { parentIdsContainSequence } from '../lib/parentIdsContainSequence'

test('parentIdsContainSequence()', () => {
expect(parentIdsContainSequence(['.', 'b', 'a', 'c', 'b', 'a'], 'a', 'b')).toBeTruthy()
})

0 comments on commit 922c6ee

Please sign in to comment.