Skip to content

Commit eda7d76

Browse files
committed
Add support for peerDependencies and optionalDependencies
1 parent afbc10f commit eda7d76

3 files changed

Lines changed: 99 additions & 8 deletions

File tree

‎apps/rush-lib/src/logic/pnpm/PnpmLinkManager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export class PnpmLinkManager extends BaseLinkManager {
271271
);
272272

273273
if (!this._rushConfiguration.experimentsConfiguration.configuration.legacyIncrementalBuildDependencyDetection) {
274-
pnpmProjectDependencyManifest.addDependency(newLocalPackage);
274+
pnpmProjectDependencyManifest.addDependency(newLocalPackage, shrinkwrapEntry);
275275
}
276276

277277
newLocalPackage.symlinkTargetFolderPath = dependencyLocalInstallationRealpath;

‎apps/rush-lib/src/logic/pnpm/PnpmProjectDependencyManifest.ts‎

Lines changed: 88 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See LICENSE in the project root for license information.
33

44
import * as path from 'path';
5+
import * as semver from 'semver';
56
import {
67
JsonFile,
78
InternalError,
@@ -10,11 +11,13 @@ import {
1011

1112
import {
1213
PnpmShrinkwrapFile,
13-
IPnpmShrinkwrapDependencyYaml
14+
IPnpmShrinkwrapDependencyYaml,
15+
parsePnpmDependencyKey
1416
} from './PnpmShrinkwrapFile';
1517
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
1618
import { RushConstants } from '../RushConstants';
1719
import { BasePackage } from '../base/BasePackage';
20+
import { DependencySpecifier } from '../DependencySpecifier';
1821

1922
export interface IPnpmProjectDependencyManifestOptions {
2023
pnpmShrinkwrapFile: PnpmShrinkwrapFile;
@@ -54,12 +57,12 @@ export class PnpmProjectDependencyManifest {
5457
this._projectDependencyManifestFile = new Map<string, string>();
5558
}
5659

57-
public addDependency(pkg: BasePackage): void {
60+
public addDependency(pkg: BasePackage, parentShrinkwrapEntry: IPnpmShrinkwrapDependencyYaml): void {
5861
if (!pkg.version) {
5962
throw new InternalError(`Version missing from dependency ${pkg.name}`);
6063
}
6164

62-
this._addDependencyInternal(pkg.name, pkg.version);
65+
this._addDependencyInternal(pkg.name, pkg.version, parentShrinkwrapEntry);
6366
}
6467

6568
/**
@@ -88,7 +91,9 @@ export class PnpmProjectDependencyManifest {
8891

8992
private _addDependencyInternal(
9093
name: string,
91-
version: string
94+
version: string,
95+
parentShrinkwrapEntry: IPnpmShrinkwrapDependencyYaml,
96+
throwIfShrinkwrapEntryMissing: boolean = true
9297
): void {
9398
const specifier: string = `${name}@${version}`;
9499
if (!PnpmProjectDependencyManifest._cache.has(specifier)) {
@@ -98,15 +103,92 @@ export class PnpmProjectDependencyManifest {
98103
);
99104

100105
if (!shrinkwrapEntry) {
101-
throw new InternalError(`Unable to find dependency ${name} with version ${version} in shrinkwrap.`);
106+
if (throwIfShrinkwrapEntryMissing) {
107+
throw new InternalError(`Unable to find dependency ${name} with version ${version} in shrinkwrap.`);
108+
}
109+
return;
102110
}
103111

104112
PnpmProjectDependencyManifest._cache.set(specifier, shrinkwrapEntry.resolution.integrity);
105113

106114
for (const dependencyName in shrinkwrapEntry.dependencies) {
107115
if (shrinkwrapEntry.dependencies.hasOwnProperty(dependencyName)) {
108116
const dependencyVersion: string = shrinkwrapEntry.dependencies[dependencyName];
109-
this._addDependencyInternal(dependencyName, dependencyVersion);
117+
this._addDependencyInternal(dependencyName, dependencyVersion, shrinkwrapEntry);
118+
}
119+
}
120+
121+
for (const optionalDependencyName in shrinkwrapEntry.optionalDependencies) {
122+
if (shrinkwrapEntry.optionalDependencies.hasOwnProperty(optionalDependencyName)) {
123+
// Optional dependencies may not exist. Don't blow up if it can't be found
124+
const dependencyVersion: string = shrinkwrapEntry.optionalDependencies[optionalDependencyName];
125+
this._addDependencyInternal(
126+
optionalDependencyName,
127+
dependencyVersion,
128+
shrinkwrapEntry,
129+
throwIfShrinkwrapEntryMissing = false);
130+
}
131+
}
132+
133+
for (const peerDependencyName in shrinkwrapEntry.peerDependencies) {
134+
if (shrinkwrapEntry.peerDependencies.hasOwnProperty(peerDependencyName)) {
135+
// Peer dependencies come in the form of a semantic version range
136+
const dependencySemVer: string = shrinkwrapEntry.peerDependencies[peerDependencyName];
137+
// Check the current package to see if the dependency is already satisfied
138+
if (
139+
shrinkwrapEntry.dependencies &&
140+
shrinkwrapEntry.dependencies.hasOwnProperty(peerDependencyName)
141+
) {
142+
const dependencySpecifier: DependencySpecifier | undefined = parsePnpmDependencyKey(
143+
peerDependencyName,
144+
shrinkwrapEntry.dependencies[peerDependencyName]
145+
);
146+
if (
147+
dependencySpecifier &&
148+
semver.valid(dependencySpecifier.versionSpecifier) &&
149+
semver.satisfies(dependencySpecifier.versionSpecifier, dependencySemVer)
150+
) {
151+
continue;
152+
}
153+
}
154+
155+
// If not, check the parent.
156+
if (
157+
parentShrinkwrapEntry.dependencies &&
158+
parentShrinkwrapEntry.dependencies.hasOwnProperty(peerDependencyName)
159+
) {
160+
const dependencySpecifier: DependencySpecifier | undefined = parsePnpmDependencyKey(
161+
peerDependencyName,
162+
parentShrinkwrapEntry.dependencies[peerDependencyName]
163+
);
164+
if (
165+
dependencySpecifier &&
166+
semver.valid(dependencySpecifier.versionSpecifier) &&
167+
semver.satisfies(dependencySpecifier.versionSpecifier, dependencySemVer)
168+
) {
169+
continue;
170+
}
171+
}
172+
173+
// The parent doesn't have a version that satisfies the range. As a last attempt, check
174+
// if it's been hoisted up as a top-level dependency
175+
const topLevelDependencySpecifier: DependencySpecifier | undefined =
176+
this._pnpmShrinkwrapFile.getTopLevelDependencyVersion(peerDependencyName);
177+
if (
178+
!topLevelDependencySpecifier ||
179+
!semver.valid(topLevelDependencySpecifier.versionSpecifier) ||
180+
!semver.satisfies(topLevelDependencySpecifier.versionSpecifier, dependencySemVer)
181+
) {
182+
throw new InternalError(
183+
`Could not find peer dependency '${peerDependencyName}' that satisfies version '${dependencySemVer}'`
184+
);
185+
}
186+
187+
this._addDependencyInternal(
188+
peerDependencyName,
189+
this._pnpmShrinkwrapFile.getTopLevelDependencyKey(peerDependencyName)!,
190+
shrinkwrapEntry
191+
);
110192
}
111193
}
112194
}

‎apps/rush-lib/src/logic/pnpm/PnpmShrinkwrapFile.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ export interface IPnpmShrinkwrapDependencyYaml {
2525
};
2626
/** The list of dependencies and the resolved version */
2727
dependencies: { [dependency: string]: string };
28+
/** The list of optional dependencies and the resolved version */
29+
optionalDependencies: { [dependency: string]: string };
30+
/** The list of peer dependencies and the resolved version */
31+
peerDependencies: { [dependency: string]: string };
2832
}
2933

3034
/**
@@ -180,6 +184,10 @@ export class PnpmShrinkwrapFile extends BaseShrinkwrapFile {
180184
return dependency.resolution.tarball;
181185
}
182186

187+
public getTopLevelDependencyKey(dependencyName: string): string | undefined {
188+
return BaseShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
189+
}
190+
183191
/**
184192
* Gets the version number from the list of top-level dependencies in the "dependencies" section
185193
* of the shrinkwrap file. Sample return values:
@@ -196,7 +204,8 @@ export class PnpmShrinkwrapFile extends BaseShrinkwrapFile {
196204
public getTopLevelDependencyVersion(dependencyName: string): DependencySpecifier | undefined {
197205
const value: string | undefined = BaseShrinkwrapFile.tryGetValue(this._shrinkwrapJson.dependencies, dependencyName);
198206
if (value) {
199-
return new DependencySpecifier(dependencyName, value);
207+
const dependencySpecifier: DependencySpecifier | undefined = parsePnpmDependencyKey(dependencyName, value);
208+
return !!dependencySpecifier ? dependencySpecifier : new DependencySpecifier(dependencyName, value);
200209
}
201210
return undefined;
202211
}

0 commit comments

Comments
 (0)