Skip to content

Commit d2cc786

Browse files
committed
[rush] Add PnpmWorkspaceFile.loadAsync to read pnpm-workspace.yaml once
Replace the per-field loadPatchedDependenciesAsync/loadAllowBuildsAsync helpers with a single loadAsync that parses the file once and returns a populated PnpmWorkspaceFile, and use it from the rush-pnpm patch-commit and approve-builds commands.
1 parent ed71358 commit d2cc786

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,10 @@ export class RushPnpmCommandLineParser {
545545
let newGlobalPatchedDependencies: Record<string, string> | undefined;
546546
if (semver.gte(pnpmVersion, '11.0.0')) {
547547
// PNPM 11+ stores patchedDependencies in pnpm-workspace.yaml instead of the package.json "pnpm" field
548-
newGlobalPatchedDependencies = await PnpmWorkspaceFile.loadPatchedDependenciesAsync(
548+
const workspaceFile: PnpmWorkspaceFile = await PnpmWorkspaceFile.loadAsync(
549549
`${subspaceTempFolder}/${RushConstants.pnpmWorkspaceFileName}`
550550
);
551+
newGlobalPatchedDependencies = workspaceFile.patchedDependencies;
551552
} else {
552553
// PNPM 10.x and earlier store patchedDependencies in the package.json "pnpm" field
553554
// Example: "C:\MyRepo\common\temp\package.json"
@@ -612,13 +613,10 @@ export class RushPnpmCommandLineParser {
612613

613614
if (semver.gte(pnpmVersion, '11.0.0')) {
614615
// PNPM 11+ uses allowBuilds in pnpm-workspace.yaml instead of onlyBuiltDependencies in package.json
615-
const workspaceYamlFilename: string = `${subspaceTempFolder}/${RushConstants.pnpmWorkspaceFileName}`;
616-
const yamlModule: typeof import('js-yaml') = await import('js-yaml');
617-
const workspaceYamlContent: string = await FileSystem.readFileAsync(workspaceYamlFilename);
618-
const workspaceYaml: { allowBuilds?: Record<string, boolean> } = (yamlModule.load(
619-
workspaceYamlContent
620-
) ?? {}) as { allowBuilds?: Record<string, boolean> };
621-
const newGlobalAllowBuilds: Record<string, boolean> | undefined = workspaceYaml?.allowBuilds;
616+
const workspaceFile: PnpmWorkspaceFile = await PnpmWorkspaceFile.loadAsync(
617+
`${subspaceTempFolder}/${RushConstants.pnpmWorkspaceFileName}`
618+
);
619+
const newGlobalAllowBuilds: Record<string, boolean> | undefined = workspaceFile.allowBuilds;
622620
const currentGlobalAllowBuilds: Record<string, boolean> | undefined =
623621
pnpmOptions?.globalAllowBuilds;
624622

libraries/rush-lib/src/logic/pnpm/PnpmWorkspaceFile.ts

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,37 @@ export class PnpmWorkspaceFile extends BaseWorkspaceFile {
114114
}
115115

116116
/**
117-
* Reads the `patchedDependencies` field from an existing `pnpm-workspace.yaml` file.
117+
* Reads an existing `pnpm-workspace.yaml` file and returns a {@link PnpmWorkspaceFile} whose
118+
* settings properties are populated from its contents.
119+
*
120+
* @remarks
121+
* The workspace `packages` list is not loaded; the returned instance is intended for reading the
122+
* generated pnpm settings (such as `allowBuilds` and `patchedDependencies`), not for
123+
* re-serialization.
124+
*
118125
* @param workspaceYamlFilename - The path to the `pnpm-workspace.yaml` file
119126
*/
120-
public static async loadPatchedDependenciesAsync(
121-
workspaceYamlFilename: string
122-
): Promise<Record<string, string> | undefined> {
127+
public static async loadAsync(workspaceYamlFilename: string): Promise<PnpmWorkspaceFile> {
123128
const workspaceYamlContent: string = await FileSystem.readFileAsync(workspaceYamlFilename);
124129
const yamlModule: typeof import('js-yaml') = await import('js-yaml');
125130
const workspaceYaml: IPnpmWorkspaceYaml | undefined = yamlModule.load(workspaceYamlContent) as
126131
| IPnpmWorkspaceYaml
127132
| undefined;
128-
return workspaceYaml?.patchedDependencies;
133+
134+
const workspaceFile: PnpmWorkspaceFile = new PnpmWorkspaceFile(workspaceYamlFilename);
135+
if (workspaceYaml) {
136+
workspaceFile.catalogs = workspaceYaml.catalogs;
137+
workspaceFile.allowBuilds = workspaceYaml.allowBuilds;
138+
workspaceFile.overrides = workspaceYaml.overrides;
139+
workspaceFile.packageExtensions = workspaceYaml.packageExtensions;
140+
workspaceFile.peerDependencyRules = workspaceYaml.peerDependencyRules;
141+
workspaceFile.allowedDeprecatedVersions = workspaceYaml.allowedDeprecatedVersions;
142+
workspaceFile.patchedDependencies = workspaceYaml.patchedDependencies;
143+
workspaceFile.minimumReleaseAge = workspaceYaml.minimumReleaseAge;
144+
workspaceFile.minimumReleaseAgeExclude = workspaceYaml.minimumReleaseAgeExclude;
145+
}
146+
147+
return workspaceFile;
129148
}
130149

131150
public override addPackage(packagePath: string): void {

libraries/rush-lib/src/logic/pnpm/test/PnpmWorkspaceFile.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ describe(PnpmWorkspaceFile.name, () => {
350350
});
351351
});
352352

353-
describe(PnpmWorkspaceFile.loadPatchedDependenciesAsync.name, () => {
353+
describe(PnpmWorkspaceFile.loadAsync.name, () => {
354354
let mockReadFileAsync: jest.SpyInstance;
355355

356356
beforeEach(() => {
@@ -375,7 +375,8 @@ describe(PnpmWorkspaceFile.name, () => {
375375
};
376376
await workspaceFile.saveAsync(workspaceFilePath, { onlyIfChanged: true });
377377

378-
await expect(PnpmWorkspaceFile.loadPatchedDependenciesAsync(workspaceFilePath)).resolves.toEqual({
378+
const loadedWorkspaceFile: PnpmWorkspaceFile = await PnpmWorkspaceFile.loadAsync(workspaceFilePath);
379+
expect(loadedWorkspaceFile.patchedDependencies).toEqual({
379380
'lodash@4.17.21': 'patches/lodash@4.17.21.patch'
380381
});
381382
});
@@ -385,9 +386,8 @@ describe(PnpmWorkspaceFile.name, () => {
385386
workspaceFile.addPackage(`${projectsDir}/app1`);
386387
await workspaceFile.saveAsync(workspaceFilePath, { onlyIfChanged: true });
387388

388-
await expect(
389-
PnpmWorkspaceFile.loadPatchedDependenciesAsync(workspaceFilePath)
390-
).resolves.toBeUndefined();
389+
const loadedWorkspaceFile: PnpmWorkspaceFile = await PnpmWorkspaceFile.loadAsync(workspaceFilePath);
390+
expect(loadedWorkspaceFile.patchedDependencies).toBeUndefined();
391391
});
392392
});
393393

0 commit comments

Comments
 (0)