Skip to content

Commit

Permalink
Rename workspace -> softLink
Browse files Browse the repository at this point in the history
  • Loading branch information
larixer committed Sep 15, 2021
1 parent 603b82a commit a92490c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
34 changes: 17 additions & 17 deletions packages/yarnpkg-nm/sources/buildNodeModulesTree.ts
Expand Up @@ -68,7 +68,7 @@ const WORKSPACE_NAME_SUFFIX = `$wsroot$`;
/** Package locator key for usage inside maps */
type LocatorKey = string;

type WorkspaceTree = {workspaceLocator?: PhysicalPackageLocator, children: Map<Filename, WorkspaceTree>};
type SoftLinkTree = {locator?: PhysicalPackageLocator, children: Map<Filename, SoftLinkTree>};

/**
* Returns path to archive, if package location is inside the archive.
Expand Down Expand Up @@ -158,7 +158,7 @@ const buildPackageTree = (pnp: PnpApi, options: NodeModulesTreeOptions): { packa
let preserveSymlinksRequired = false;

const hoistingLimits = new Map<LocatorKey, Set<string>>();
const workspaceDependenciesMap = new Map<LocatorKey, Set<PhysicalPackageLocator>>();
const softLinkDependenciesMap = new Map<LocatorKey, Set<PhysicalPackageLocator>>();

const topPkg = pnp.getPackageInformation(pnp.topLevel);
if (topPkg === null)
Expand All @@ -170,13 +170,13 @@ const buildPackageTree = (pnp: PnpApi, options: NodeModulesTreeOptions): { packa

const topPkgPortableLocation = npath.toPortablePath(topPkg.packageLocation.slice(0, -1));

const workspaceTree: WorkspaceTree = {children: new Map()};
const softLinkTree: SoftLinkTree = {children: new Map()};
const cwdSegments = topPkgPortableLocation.split(ppath.sep);
for (const locator of pnpRoots) {
const pkg = pnp.getPackageInformation(locator)!;
const location = npath.toPortablePath(pkg.packageLocation.slice(0, -1));
const segments = location.split(ppath.sep).slice(cwdSegments.length);
let node = workspaceTree;
let node = softLinkTree;
for (const segment of segments) {
let nextNode = node.children.get(segment as Filename);
if (!nextNode) {
Expand All @@ -185,34 +185,34 @@ const buildPackageTree = (pnp: PnpApi, options: NodeModulesTreeOptions): { packa
}
node = nextNode;
}
node.workspaceLocator = locator;
node.locator = locator;
}

const addWorkspace = (node: WorkspaceTree, parentWorkspaceLocator: PhysicalPackageLocator) => {
if (node.workspaceLocator) {
const parentLocatorKey = stringifyLocator(parentWorkspaceLocator);
let dependencies = workspaceDependenciesMap.get(parentLocatorKey);
const addSoftLink = (node: SoftLinkTree, parentSoftLinkLocator: PhysicalPackageLocator) => {
if (node.locator) {
const parentLocatorKey = stringifyLocator(parentSoftLinkLocator);
let dependencies = softLinkDependenciesMap.get(parentLocatorKey);
if (!dependencies) {
dependencies = new Set();
workspaceDependenciesMap.set(parentLocatorKey, dependencies);
softLinkDependenciesMap.set(parentLocatorKey, dependencies);
}
dependencies.add(node.workspaceLocator);
dependencies.add(node.locator);
}
for (const child of node.children.values()) {
addWorkspace(child, node.workspaceLocator || parentWorkspaceLocator);
addSoftLink(child, node.locator || parentSoftLinkLocator);
}
};

for (const child of workspaceTree.children.values())
addWorkspace(child, workspaceTree.workspaceLocator!);
for (const child of softLinkTree.children.values())
addSoftLink(child, softLinkTree.locator!);

const packageTree: HoisterTree = {
name: topLocator.name,
identName: topLocator.name,
reference: topLocator.reference,
peerNames: topPkg.packagePeers,
dependencies: new Set<HoisterTree>(),
isWorkspace: true,
isSoftLink: topPkg.linkType === LinkType.SOFT,
};

const nodes = new Map<string, HoisterTree>();
Expand Down Expand Up @@ -244,7 +244,7 @@ const buildPackageTree = (pnp: PnpApi, options: NodeModulesTreeOptions): { packa
reference: locator.reference,
dependencies: new Set(),
peerNames: pkg.packagePeers,
isWorkspace: pkg.linkType === LinkType.SOFT && locator.name.endsWith(WORKSPACE_NAME_SUFFIX),
isSoftLink: pkg.linkType === LinkType.SOFT && locator.name.endsWith(WORKSPACE_NAME_SUFFIX),
};

nodes.set(nodeKey, node);
Expand Down Expand Up @@ -287,7 +287,7 @@ const buildPackageTree = (pnp: PnpApi, options: NodeModulesTreeOptions): { packa
}

const locatorKey = stringifyLocator({name: locator.name.replace(WORKSPACE_NAME_SUFFIX, ``), reference: locator.reference});
const workspaceDependencies = workspaceDependenciesMap.get(locatorKey);
const workspaceDependencies = softLinkDependenciesMap.get(locatorKey);
if (workspaceDependencies) {
for (const workspaceLocator of workspaceDependencies) {
allDependencies.set(`${workspaceLocator.name}${WORKSPACE_NAME_SUFFIX}`, workspaceLocator.reference);
Expand Down
30 changes: 13 additions & 17 deletions packages/yarnpkg-nm/sources/hoist.ts
Expand Up @@ -46,11 +46,11 @@
* until you run out of candidates for current tree root.
*/
type PackageName = string;
export type HoisterTree = {name: PackageName, identName: PackageName, reference: string, dependencies: Set<HoisterTree>, peerNames: Set<PackageName>, hoistPriority?: number, isWorkspace?: boolean};
export type HoisterTree = {name: PackageName, identName: PackageName, reference: string, dependencies: Set<HoisterTree>, peerNames: Set<PackageName>, hoistPriority?: number, isSoftLink?: boolean};
export type HoisterResult = {name: PackageName, identName: PackageName, references: Set<string>, dependencies: Set<HoisterResult>};
type Locator = string;
type Ident = string;
type HoisterWorkTree = {name: PackageName, references: Set<string>, ident: Ident, locator: Locator, dependencies: Map<PackageName, HoisterWorkTree>, originalDependencies: Map<PackageName, HoisterWorkTree>, hoistedDependencies: Map<PackageName, HoisterWorkTree>, peerNames: ReadonlySet<PackageName>, decoupled: boolean, reasons: Map<PackageName, string>, isHoistBorder: boolean, hoistedFrom: Array<string>, hoistPriority: number, isWorkspace: boolean};
type HoisterWorkTree = {name: PackageName, references: Set<string>, ident: Ident, locator: Locator, dependencies: Map<PackageName, HoisterWorkTree>, originalDependencies: Map<PackageName, HoisterWorkTree>, hoistedDependencies: Map<PackageName, HoisterWorkTree>, peerNames: ReadonlySet<PackageName>, decoupled: boolean, reasons: Map<PackageName, string>, isHoistBorder: boolean, hoistedFrom: Array<string>, hoistPriority: number, isSoftLink: boolean};

/**
* Mapping which packages depend on a given package alias + ident. It is used to determine hoisting weight,
Expand Down Expand Up @@ -237,7 +237,7 @@ const decoupleGraphNode = (parent: HoisterWorkTree, node: HoisterWorkTree): Hois
if (node.decoupled)
return node;

const {name, references, ident, locator, dependencies, originalDependencies, hoistedDependencies, peerNames, reasons, isHoistBorder, hoistPriority, isWorkspace, hoistedFrom} = node;
const {name, references, ident, locator, dependencies, originalDependencies, hoistedDependencies, peerNames, reasons, isHoistBorder, hoistPriority, isSoftLink, hoistedFrom} = node;
// To perform node hoisting from parent node we must clone parent nodes up to the root node,
// because some other package in the tree might depend on the parent package where hoisting
// cannot be performed
Expand All @@ -254,7 +254,7 @@ const decoupleGraphNode = (parent: HoisterWorkTree, node: HoisterWorkTree): Hois
decoupled: true,
isHoistBorder,
hoistPriority,
isWorkspace,
isSoftLink,
hoistedFrom,
};
const selfDep = clone.dependencies.get(name);
Expand Down Expand Up @@ -448,20 +448,16 @@ const getNodeHoistInfo = (rootNode: HoisterWorkTree, rootNodePathLocators: Set<L
reason = `- self-reference`;

if (isHoistable) {
isHoistable = !rootNode.peerNames.has(node.name);
isHoistable = !node.isSoftLink;
if (outputReason && !isHoistable) {
reason = `- cannot shadow peer: ${prettyPrintLocator(rootNode.originalDependencies.get(node.name)!.locator)} at ${reasonRoot}`;
reason = `- soft link`;
}
}

if (isHoistable && node.isWorkspace) {
for (let idx = nodePath.length - 1; idx >= 0; idx--) {
const parent = nodePath[idx];
if (parent.isWorkspace) {
isHoistable = false;
reason = ` - cannot hoist over parent: ${prettyPrintLocator(parent.locator)}`;
break;
}
if (isHoistable) {
isHoistable = !rootNode.peerNames.has(node.name);
if (outputReason && !isHoistable) {
reason = `- cannot shadow peer: ${prettyPrintLocator(rootNode.originalDependencies.get(node.name)!.locator)} at ${reasonRoot}`;
}
}

Expand Down Expand Up @@ -738,7 +734,7 @@ const cloneTree = (tree: HoisterTree, options: InternalHoistOptions): HoisterWor
decoupled: true,
isHoistBorder: true,
hoistPriority: 0,
isWorkspace: true,
isSoftLink: true,
hoistedFrom: [],
};

Expand All @@ -748,7 +744,7 @@ const cloneTree = (tree: HoisterTree, options: InternalHoistOptions): HoisterWor
let workNode = seenNodes.get(node);
const isSeen = !!workNode;
if (!workNode) {
const {name, identName, reference, peerNames, hoistPriority, isWorkspace} = node;
const {name, identName, reference, peerNames, hoistPriority, isSoftLink} = node;
const dependenciesNmHoistingLimits = options.hoistingLimits.get(parentNode.locator);
workNode = {
name,
Expand All @@ -763,7 +759,7 @@ const cloneTree = (tree: HoisterTree, options: InternalHoistOptions): HoisterWor
decoupled: true,
isHoistBorder: dependenciesNmHoistingLimits ? dependenciesNmHoistingLimits.has(name) : false,
hoistPriority: hoistPriority || 0,
isWorkspace: isWorkspace || false,
isSoftLink: isSoftLink || false,
hoistedFrom: [],
};
seenNodes.set(node, workNode);
Expand Down

0 comments on commit a92490c

Please sign in to comment.