Skip to content

Commit 656f538

Browse files
authored
[rush] Consolidate pnpm options handling into InstallHelpers.resolvePnpmSettings (#5880)
* More cleanup. * [rush] Consolidate pnpm options handling into InstallHelpers.resolvePnpmSettings Read the pnpm options in a single place that derives both the common package.json "pnpm" field and a populated PnpmWorkspaceFile, removing the duplicated pnpm-version gating between generateCommonPackageJsonAsync and WorkspaceInstallManager.prepareCommonTempAsync. * Create a pnpmWorkspaceFileName constant. * Get rid of some path.joins * [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. * Add rush change file * Rush change.
1 parent f29d18d commit 656f538

10 files changed

Lines changed: 400 additions & 308 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "",
5+
"type": "none",
6+
"packageName": "@microsoft/rush"
7+
}
8+
],
9+
"packageName": "@microsoft/rush",
10+
"email": "iclanton@users.noreply.github.com"
11+
}

common/reviews/api/rush-lib.api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,7 @@ export class RushConstants {
15781578
static readonly pnpmSyncFilename: '.pnpm-sync.json';
15791579
static readonly pnpmV3ShrinkwrapFilename: 'pnpm-lock.yaml';
15801580
static readonly pnpmVirtualStoreFolderName: '.pnpm';
1581+
static readonly pnpmWorkspaceFileName: 'pnpm-workspace.yaml';
15811582
static readonly projectImpactGraphFilename: 'project-impact-graph.yaml';
15821583
static readonly projectRushFolderName: '.rush';
15831584
static readonly projectShrinkwrapFilename: 'shrinkwrap-deps.json';

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class RushPnpmCommandLineParser {
166166
this._subspace = subspace;
167167

168168
const workspaceFolder: string = subspace.getSubspaceTempFolderPath();
169-
const workspaceFilePath: string = path.join(workspaceFolder, 'pnpm-workspace.yaml');
169+
const workspaceFilePath: string = `${workspaceFolder}/${RushConstants.pnpmWorkspaceFileName}`;
170170

171171
if (!FileSystem.exists(workspaceFilePath)) {
172172
this._terminal.writeErrorLine('Error: The PNPM workspace file has not been generated:');
@@ -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(
549-
`${subspaceTempFolder}/pnpm-workspace.yaml`
548+
const workspaceFile: PnpmWorkspaceFile = await PnpmWorkspaceFile.loadAsync(
549+
`${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}/pnpm-workspace.yaml`;
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/RushConstants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,9 @@ export class RushConstants {
369369
* The filename ("pnpm-sync.json") used to store the state of the pnpm sync command.
370370
*/
371371
public static readonly pnpmSyncFilename: '.pnpm-sync.json' = '.pnpm-sync.json';
372+
373+
/**
374+
* The filename ("pnpm-workspace.yaml") used to store the state of the pnpm workspace configuration.
375+
*/
376+
public static readonly pnpmWorkspaceFileName: 'pnpm-workspace.yaml' = 'pnpm-workspace.yaml';
372377
}

0 commit comments

Comments
 (0)