Skip to content

Commit 498e8ce

Browse files
committed
Initial prototype of per-project shrinkwrap.
1 parent db180f4 commit 498e8ce

4 files changed

Lines changed: 144 additions & 16 deletions

File tree

‎apps/rush-lib/src/logic/RushConstants.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,10 @@ export namespace RushConstants {
143143
* before asynchronously deleting them.
144144
*/
145145
export const rushRecyclerFolderName: string = 'rush-recycler';
146+
147+
/**
148+
* The name of the file to drop in project-folder/.rush/temp/ containing a listing of the projects direct
149+
* and indirect dependencies. This is used to detect if a project's dependencies have changed since the last build.
150+
*/
151+
export const projectDependencyManifestFilename: string = 'project-dependencies.json';
146152
}

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

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { RushConstants } from '../../logic/RushConstants';
2424
import { IRushLinkJson } from '../../api/RushConfiguration';
2525
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
2626
import { PnpmShrinkwrapFile } from './PnpmShrinkwrapFile';
27+
import { PnpmProjectDependencyManifest } from './PnpmProjectDependencyManifest';
2728

2829
// special flag for debugging, will print extra diagnostic information,
2930
// but comes with performance cost
@@ -72,7 +73,8 @@ export class PnpmLinkManager extends BaseLinkManager {
7273
private _linkProject(
7374
project: RushConfigurationProject,
7475
rushLinkJson: IRushLinkJson,
75-
pnpmShrinkwrapFile: PnpmShrinkwrapFile): Promise<void> {
76+
pnpmShrinkwrapFile: PnpmShrinkwrapFile
77+
): Promise<void> {
7678

7779
// first, read the temp package.json information
7880

@@ -87,8 +89,12 @@ export class PnpmLinkManager extends BaseLinkManager {
8789
const packageJsonFilename: string = path.join(extractedFolder, FileConstants.PackageJson);
8890

8991
// Example: "C:\MyRepo\common\temp\node_modules\@rush-temp\project1"
90-
const installFolderName: string = path.join(this._rushConfiguration.commonTempFolder,
91-
RushConstants.nodeModulesFolderName, RushConstants.rushTempNpmScope, unscopedTempProjectName);
92+
const installFolderName: string = path.join(
93+
this._rushConfiguration.commonTempFolder,
94+
RushConstants.nodeModulesFolderName,
95+
RushConstants.rushTempNpmScope,
96+
unscopedTempProjectName
97+
);
9298

9399
const commonPackage: BasePackage = BasePackage.createVirtualTempPackage(packageJsonFilename, installFolderName);
94100

@@ -102,7 +108,6 @@ export class PnpmLinkManager extends BaseLinkManager {
102108

103109
// first, start with the rush dependencies, we just need to link to the project folder
104110
for (const dependencyName of Object.keys(commonPackage.packageJson!.rushDependencies || {})) {
105-
106111
const matchedRushPackage: RushConfigurationProject | undefined =
107112
this._rushConfiguration.getProjectByName(dependencyName);
108113

@@ -130,8 +135,9 @@ export class PnpmLinkManager extends BaseLinkManager {
130135
newLocalPackage.symlinkTargetFolderPath = matchedRushPackage.projectFolder;
131136
localPackage.children.push(newLocalPackage);
132137
} else {
133-
throw new InternalError(`Cannot find dependency "${dependencyName}" for "${project.packageName}" in `
134-
+ ` the Rush configuration`);
138+
throw new InternalError(
139+
`Cannot find dependency "${dependencyName}" for "${project.packageName}" in the Rush configuration`
140+
);
135141
}
136142
}
137143

@@ -155,8 +161,9 @@ export class PnpmLinkManager extends BaseLinkManager {
155161
// file:projects/bentleyjs-core.tgz
156162
// file:projects/build-tools.tgz_dc21d88642e18a947127a751e00b020a
157163
// file:projects/imodel-from-geojson.tgz_request@2.88.0
158-
const tempProjectDependencyKey: string | undefined =
159-
pnpmShrinkwrapFile.getTempProjectDependencyKey(project.tempProjectName);
164+
const tempProjectDependencyKey: string | undefined = pnpmShrinkwrapFile.getTempProjectDependencyKey(
165+
project.tempProjectName
166+
);
160167

161168
if (!tempProjectDependencyKey) {
162169
throw new Error(`Cannot get dependency key for temp project: ${project.tempProjectName}`);
@@ -188,8 +195,9 @@ export class PnpmLinkManager extends BaseLinkManager {
188195
// '' [empty string]
189196
// _jsdom@11.12.0
190197
// _2a665c89609864b4e75bc5365d7f8f56
191-
const folderNameSuffix: string = (tarballEntry && tarballEntry.length < tempProjectDependencyKey.length ?
192-
tempProjectDependencyKey.slice(tarballEntry.length) : '');
198+
const folderNameSuffix: string = tarballEntry && tarballEntry.length < tempProjectDependencyKey.length
199+
? tempProjectDependencyKey.slice(tarballEntry.length)
200+
: '';
193201

194202
// e.g.:
195203
// C%3A%2Fwbt%2Fcommon%2Ftemp%2Fprojects%2Fapi-documenter.tgz
@@ -205,7 +213,13 @@ export class PnpmLinkManager extends BaseLinkManager {
205213
RushConstants.nodeModulesFolderName,
206214
'.local',
207215
folderNameInLocalInstallationRoot,
208-
RushConstants.nodeModulesFolderName);
216+
RushConstants.nodeModulesFolderName
217+
);
218+
219+
const pnpmProjectDependencyManifest: PnpmProjectDependencyManifest = new PnpmProjectDependencyManifest({
220+
pnpmShrinkwrapFile,
221+
project
222+
});
209223

210224
for (const dependencyName of Object.keys(commonPackage.packageJson!.dependencies || {})) {
211225
// the dependency we are looking for should have already created a symlink here
@@ -233,24 +247,25 @@ export class PnpmLinkManager extends BaseLinkManager {
233247
// it pointed, rather than linking to a link.
234248
const dependencyLocalInstallationRealpath: string = FileSystem.getRealPath(dependencyLocalInstallationSymlink);
235249

236-
const newLocalFolderPath: string = path.join(
237-
localPackage.folderPath, 'node_modules', dependencyName);
250+
const newLocalFolderPath: string = path.join(localPackage.folderPath, 'node_modules', dependencyName);
238251

239252
let version: string | undefined = undefined;
240-
if (DEBUG) {
253+
// if (DEBUG) {
241254
// read the version number for diagnostic purposes
242255
const packageJsonForDependency: IPackageJson = JsonFile.load(
243256
path.join(dependencyLocalInstallationRealpath, FileConstants.PackageJson));
244257

245258
version = packageJsonForDependency.version;
246-
}
259+
// }
247260

248261
const newLocalPackage: BasePackage = BasePackage.createLinkedPackage(
249262
dependencyName,
250263
version,
251264
newLocalFolderPath
252265
);
253266

267+
pnpmProjectDependencyManifest.addDependency(newLocalPackage);
268+
254269
newLocalPackage.symlinkTargetFolderPath = dependencyLocalInstallationRealpath;
255270
localPackage.addChild(newLocalPackage);
256271
}
@@ -261,6 +276,8 @@ export class PnpmLinkManager extends BaseLinkManager {
261276

262277
PnpmLinkManager._createSymlinksForTopLevelProject(localPackage);
263278

279+
pnpmProjectDependencyManifest.save();
280+
264281
// Also symlink the ".bin" folder
265282
const projectFolder: string = path.join(localPackage.folderPath, 'node_modules');
266283
const projectBinFolder: string = path.join(localPackage.folderPath, 'node_modules', '.bin');
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import * as path from 'path';
5+
import {
6+
JsonFile,
7+
InternalError
8+
} from '@microsoft/node-core-library';
9+
10+
import {
11+
PnpmShrinkwrapFile,
12+
IPnpmShrinkwrapDependencyYaml
13+
} from './PnpmShrinkwrapFile';
14+
import { RushConfigurationProject } from '../../api/RushConfigurationProject';
15+
import { RushConstants } from '../RushConstants';
16+
import { BasePackage } from '../base/BasePackage';
17+
18+
export interface IPnpmProjectDependencyManifestOptions {
19+
pnpmShrinkwrapFile: PnpmShrinkwrapFile;
20+
project: RushConfigurationProject;
21+
}
22+
23+
export class PnpmProjectDependencyManifest {
24+
private static _cache: Map<string, string> = new Map<string, string>();
25+
26+
private readonly _projectDependencyManifestFilename: string;
27+
private readonly _pnpmShrinkwrapFile: PnpmShrinkwrapFile;
28+
private readonly _project: RushConfigurationProject;
29+
30+
private _projectDependencyManifestFile: Map<string, string>;
31+
32+
public constructor(options: IPnpmProjectDependencyManifestOptions) {
33+
this._pnpmShrinkwrapFile = options.pnpmShrinkwrapFile;
34+
this._project = options.project;
35+
this._projectDependencyManifestFilename = path.join(
36+
this._project.projectRushTempFolder,
37+
RushConstants.projectDependencyManifestFilename
38+
);
39+
40+
this._projectDependencyManifestFile = new Map<string, string>();
41+
}
42+
43+
public addDependency(pkg: BasePackage): void {
44+
if (!pkg.version) {
45+
throw new InternalError(`Version missing from dependency ${pkg.name}`);
46+
}
47+
48+
this._addDependencyInternal(pkg.name, pkg.version);
49+
}
50+
51+
public save(): void {
52+
const file: { [specifier: string]: string } = {};
53+
const keys: string[] = Array.from(this._projectDependencyManifestFile.keys()).sort();
54+
for (const key of keys) {
55+
file[key] = this._projectDependencyManifestFile.get(key)!;
56+
}
57+
58+
JsonFile.save(
59+
file,
60+
this._projectDependencyManifestFilename,
61+
{ ensureFolderExists: true }
62+
);
63+
}
64+
65+
private _addDependencyInternal(
66+
name: string,
67+
version: string
68+
): void {
69+
const specifier: string = `${name}@${version}`;
70+
if (!PnpmProjectDependencyManifest._cache.has(specifier)) {
71+
const shrinkwrapEntry: IPnpmShrinkwrapDependencyYaml | undefined = this._pnpmShrinkwrapFile.getShrinkwrapEntry(
72+
name,
73+
version
74+
);
75+
76+
if (!shrinkwrapEntry) {
77+
throw new InternalError(`Unable to find dependency ${name} with version ${version} in shrinkwrap.`);
78+
}
79+
80+
PnpmProjectDependencyManifest._cache.set(specifier, shrinkwrapEntry.resolution.integrity);
81+
82+
for (const dependencyName in shrinkwrapEntry.dependencies) {
83+
if (shrinkwrapEntry.dependencies.hasOwnProperty(dependencyName)) {
84+
const dependencyVersion: string = shrinkwrapEntry.dependencies[dependencyName];
85+
this._addDependencyInternal(dependencyName, dependencyVersion);
86+
}
87+
}
88+
}
89+
90+
const integrity: string = PnpmProjectDependencyManifest._cache.get(specifier)!;
91+
92+
if (
93+
this._projectDependencyManifestFile.has(specifier) &&
94+
this._projectDependencyManifestFile.get(specifier) !== integrity
95+
) {
96+
throw new Error(`Collision: ${specifier} already exists in with a different integrity`);
97+
}
98+
99+
this._projectDependencyManifestFile.set(specifier, integrity);
100+
}
101+
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const SHRINKWRAP_YAML_FORMAT: yaml.DumpOptions = {
1515
sortKeys: true
1616
};
1717

18-
interface IPnpmShrinkwrapDependencyYaml {
18+
export interface IPnpmShrinkwrapDependencyYaml {
1919
/** Information about the resolved package */
2020
resolution: {
2121
/** The hash of the tarball, to ensure archive integrity */
@@ -226,6 +226,10 @@ export class PnpmShrinkwrapFile extends BaseShrinkwrapFile {
226226
return undefined;
227227
}
228228

229+
public getShrinkwrapEntry(name: string, version: string): IPnpmShrinkwrapDependencyYaml | undefined {
230+
return this._shrinkwrapJson.packages[`/${name}/${version}`];
231+
}
232+
229233
/**
230234
* Serializes the PNPM Shrinkwrap file
231235
*

0 commit comments

Comments
 (0)