Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read pnpm options from subspace configurations #4571

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1394,7 +1394,7 @@ export class Subspace {
// @beta
getPnpmfilePath(): string;
// @beta
getPnpmOptions(): PnpmOptionsConfiguration | undefined;
getPnpmOptions(): PnpmOptionsConfiguration;
// @beta
getProjects(): RushConfigurationProject[];
// @beta
Expand Down
6 changes: 3 additions & 3 deletions libraries/rush-lib/src/api/LastInstallFlag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ export class LastInstallFlagFactory {
};

if (currentState.packageManager === 'pnpm' && rushConfiguration.pnpmOptions) {
currentState.storePath = rushConfiguration.pnpmOptions.pnpmStorePath;
if (rushConfiguration.pnpmOptions.useWorkspaces) {
currentState.workspaces = rushConfiguration.pnpmOptions.useWorkspaces;
currentState.storePath = subspace.getPnpmOptions().pnpmStorePath;
if (subspace.getPnpmOptions().useWorkspaces) {
currentState.workspaces = subspace.getPnpmOptions().useWorkspaces;
}
}

Expand Down
4 changes: 2 additions & 2 deletions libraries/rush-lib/src/api/RushConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { PackageNameParsers } from './PackageNameParsers';
import type { RepoStateFile } from '../logic/RepoStateFile';
import { LookupByPath } from '../logic/LookupByPath';
import { RushPluginsConfiguration } from './RushPluginsConfiguration';
import { type IPnpmOptionsJson, PnpmOptionsConfiguration } from '../logic/pnpm/PnpmOptionsConfiguration';
import { PnpmOptionsConfiguration, type IPnpmOptionsJson } from '../logic/pnpm/PnpmOptionsConfiguration';
import { type INpmOptionsJson, NpmOptionsConfiguration } from '../logic/npm/NpmOptionsConfiguration';
import { type IYarnOptionsJson, YarnOptionsConfiguration } from '../logic/yarn/YarnOptionsConfiguration';
import schemaJson from '../schemas/rush.schema.json';
Expand Down Expand Up @@ -501,7 +501,7 @@ export class RushConfiguration {
* The configuration options used by the current package manager.
* @remarks
* For package manager specific variants, reference {@link RushConfiguration.npmOptions | npmOptions},
* {@link RushConfiguration.pnpmOptions | pnpmOptions}, or {@link RushConfiguration.yarnOptions | yarnOptions}.
* or {@link RushConfiguration.yarnOptions | yarnOptions}.
*/
public readonly packageManagerOptions!: PackageManagerOptionsConfigurationBase;

Expand Down
19 changes: 9 additions & 10 deletions libraries/rush-lib/src/api/Subspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ export class Subspace {

private _detail: ISubspaceDetail | undefined;

private _cachedPnpmOptions: PnpmOptionsConfiguration | undefined = undefined;
// If true, then _cachedPnpmOptions has been initialized.
private _cachedPnpmOptionsInitialized: boolean = false;
private _pnpmOptions: PnpmOptionsConfiguration | undefined;

public constructor(options: ISubspaceOptions) {
this.subspaceName = options.subspaceName;
Expand All @@ -65,24 +63,25 @@ export class Subspace {
* Returns the parsed contents of the pnpm-config.json config file.
* @beta
*/
public getPnpmOptions(): PnpmOptionsConfiguration | undefined {
if (!this._cachedPnpmOptionsInitialized) {
public getPnpmOptions(): PnpmOptionsConfiguration {
if (!this._pnpmOptions) {
try {
this._cachedPnpmOptions = PnpmOptionsConfiguration.loadFromJsonFileOrThrow(
this._pnpmOptions = PnpmOptionsConfiguration.loadFromJsonFileOrThrow(
`${this.getSubspaceConfigFolder()}/${RushConstants.pnpmConfigFilename}`,
this.getSubspaceTempFolder()
);
this._cachedPnpmOptionsInitialized = true;
} catch (e) {
if (FileSystem.isNotExistError(e as Error)) {
this._cachedPnpmOptions = undefined;
this._cachedPnpmOptionsInitialized = true;
this._pnpmOptions = PnpmOptionsConfiguration.loadFromJsonObject(
this._rushConfiguration.rushConfigurationJson.pnpmOptions || {},
this._rushConfiguration.commonTempFolder
);
} else {
throw new Error(`The subspace has an invalid pnpm-config.json file: ${this.subspaceName}`);
}
}
}
return this._cachedPnpmOptions;
return this._pnpmOptions;
}

private _ensureDetail(): ISubspaceDetail {
Expand Down
42 changes: 23 additions & 19 deletions libraries/rush-lib/src/api/test/RushConfiguration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ describe(RushConfiguration.name, () => {
assertPathProperty('commonTempFolder', rushConfiguration.commonTempFolder, './repo/common/temp');
assertPathProperty('npmCacheFolder', rushConfiguration.npmCacheFolder, './repo/common/temp/npm-cache');
assertPathProperty('npmTmpFolder', rushConfiguration.npmTmpFolder, './repo/common/temp/npm-tmp');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('local');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('local');
assertPathProperty(
'pnpmStorePath',
rushConfiguration.pnpmOptions.pnpmStorePath,
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath,
'./repo/common/temp/pnpm-store'
);
assertPathProperty(
Expand Down Expand Up @@ -130,10 +130,10 @@ describe(RushConfiguration.name, () => {
assertPathProperty('commonTempFolder', rushConfiguration.commonTempFolder, './repo/common/temp');
assertPathProperty('npmCacheFolder', rushConfiguration.npmCacheFolder, './repo/common/temp/npm-cache');
assertPathProperty('npmTmpFolder', rushConfiguration.npmTmpFolder, './repo/common/temp/npm-tmp');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('local');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('local');
assertPathProperty(
'pnpmStorePath',
rushConfiguration.pnpmOptions.pnpmStorePath,
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath,
'./repo/common/temp/pnpm-store'
);
assertPathProperty(
Expand Down Expand Up @@ -206,10 +206,10 @@ describe(RushConfiguration.name, () => {
);
assertPathProperty('npmTmpFolder', rushConfiguration.npmTmpFolder, path.join(expectedValue, 'npm-tmp'));

expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('local');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('local');
assertPathProperty(
'pnpmStorePath',
rushConfiguration.pnpmOptions.pnpmStorePath,
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath,
path.join(expectedValue, 'pnpm-store')
);
assertPathProperty(
Expand Down Expand Up @@ -240,11 +240,13 @@ describe(RushConfiguration.name, () => {
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);

expect(rushConfiguration.packageManager).toEqual('pnpm');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('local');
expect(Path.convertToSlashes(rushConfiguration.pnpmOptions.pnpmStorePath)).toEqual(
Path.convertToSlashes(EXPECT_STORE_PATH)
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('local');
expect(
Path.convertToSlashes(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath)
).toEqual(Path.convertToSlashes(EXPECT_STORE_PATH));
expect(path.isAbsolute(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath)).toEqual(
true
);
expect(path.isAbsolute(rushConfiguration.pnpmOptions.pnpmStorePath)).toEqual(true);
});

it('loads the correct path when environment variable is defined', () => {
Expand All @@ -255,9 +257,11 @@ describe(RushConfiguration.name, () => {
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);

expect(rushConfiguration.packageManager).toEqual('pnpm');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('local');
expect(rushConfiguration.pnpmOptions.pnpmStorePath).toEqual(EXPECT_STORE_PATH);
expect(path.isAbsolute(rushConfiguration.pnpmOptions.pnpmStorePath)).toEqual(true);
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('local');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath).toEqual(EXPECT_STORE_PATH);
expect(path.isAbsolute(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath)).toEqual(
true
);
});
});

Expand All @@ -270,8 +274,8 @@ describe(RushConfiguration.name, () => {
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);

expect(rushConfiguration.packageManager).toEqual('pnpm');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('global');
expect(rushConfiguration.pnpmOptions.pnpmStorePath).toEqual(EXPECT_STORE_PATH);
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('global');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath).toEqual(EXPECT_STORE_PATH);
});

it('loads the correct path when environment variable is defined', () => {
Expand All @@ -282,8 +286,8 @@ describe(RushConfiguration.name, () => {
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);

expect(rushConfiguration.packageManager).toEqual('pnpm');
expect(rushConfiguration.pnpmOptions.pnpmStore).toEqual('global');
expect(rushConfiguration.pnpmOptions.pnpmStorePath).toEqual(EXPECT_STORE_PATH);
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore).toEqual('global');
expect(rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath).toEqual(EXPECT_STORE_PATH);
});
});

Expand All @@ -298,12 +302,12 @@ describe(RushConfiguration.name, () => {
});
});

it('reject "pnpmOptions" in rush.json if the file pnpm-config.json exists', () => {
it('reject "defaultSubspace.getPnpmOptions()" in rush.json if the file pnpm-config.json exists', () => {
const RUSH_JSON_FILENAME: string = `${__dirname}/pnpmConfigThrow/rush.json`;
expect(() => {
RushConfiguration.loadFromConfigurationFile(RUSH_JSON_FILENAME);
}).toThrow(
'Because the new config file "common/config/rush/pnpm-config.json" is being used, you must remove the old setting "pnpmOptions" from rush.json'
'Because the new config file "common/config/rush/pnpm-config.json" is being used, you must remove the old setting "defaultSubspace.getPnpmOptions()" from rush.json'
);
});

Expand Down
36 changes: 24 additions & 12 deletions libraries/rush-lib/src/cli/RushPnpmCommandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import type { BaseInstallManager } from '../logic/base/BaseInstallManager';
import type { IInstallManagerOptions } from '../logic/base/BaseInstallManagerTypes';
import { objectsAreDeepEqual } from '../utilities/objectUtilities';
import { Utilities } from '../utilities/Utilities';
import type { IConfigurationEnvironment } from '../logic/base/BasePackageManagerOptionsConfiguration';

const RUSH_SKIP_CHECKS_PARAMETER: string = '--rush-skip-checks';

Expand Down Expand Up @@ -97,9 +98,9 @@ export class RushPnpmCommandLineParser {
);
}

if (!rushConfiguration.pnpmOptions.useWorkspaces) {
if (!rushConfiguration.defaultSubspace.getPnpmOptions().useWorkspaces) {
const pnpmConfigFilename: string =
rushConfiguration.pnpmOptions.jsonFilename || RushConstants.rushJsonFilename;
rushConfiguration.defaultSubspace.getPnpmOptions().jsonFilename || RushConstants.rushJsonFilename;
throw new Error(
`The "rush-pnpm" command requires the "useWorkspaces" setting to be enabled in ${pnpmConfigFilename}`
);
Expand Down Expand Up @@ -354,16 +355,25 @@ export class RushPnpmCommandLineParser {
const pnpmEnvironmentMap: EnvironmentMap = new EnvironmentMap(process.env);
pnpmEnvironmentMap.set('NPM_CONFIG_WORKSPACE_DIR', workspaceFolder);

if (rushConfiguration.pnpmOptions.pnpmStorePath) {
pnpmEnvironmentMap.set('NPM_CONFIG_STORE_DIR', rushConfiguration.pnpmOptions.pnpmStorePath);
pnpmEnvironmentMap.set('NPM_CONFIG_CACHE_DIR', rushConfiguration.pnpmOptions.pnpmStorePath);
pnpmEnvironmentMap.set('NPM_CONFIG_STATE_DIR', rushConfiguration.pnpmOptions.pnpmStorePath);
if (rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath) {
pnpmEnvironmentMap.set(
'NPM_CONFIG_STORE_DIR',
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath
);
pnpmEnvironmentMap.set(
'NPM_CONFIG_CACHE_DIR',
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath
);
pnpmEnvironmentMap.set(
'NPM_CONFIG_STATE_DIR',
rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath
);
}

if (rushConfiguration.pnpmOptions.environmentVariables) {
for (const [envKey, { value: envValue, override }] of Object.entries(
rushConfiguration.pnpmOptions.environmentVariables
)) {
const environmentVariables: IConfigurationEnvironment | undefined =
rushConfiguration.defaultSubspace.getPnpmOptions().environmentVariables;
if (environmentVariables) {
for (const [envKey, { value: envValue, override }] of Object.entries(environmentVariables)) {
if (override) {
pnpmEnvironmentMap.set(envKey, envValue);
} else {
Expand Down Expand Up @@ -420,7 +430,7 @@ export class RushPnpmCommandLineParser {
const newGlobalPatchedDependencies: Record<string, string> | undefined =
commonPackageJson?.pnpm?.patchedDependencies;
const currentGlobalPatchedDependencies: Record<string, string> | undefined =
this._rushConfiguration.pnpmOptions.globalPatchedDependencies;
this._rushConfiguration.defaultSubspace.getPnpmOptions().globalPatchedDependencies;

if (!objectsAreDeepEqual(currentGlobalPatchedDependencies, newGlobalPatchedDependencies)) {
const commonTempPnpmPatchesFolder: string = `${this._rushConfiguration.commonTempFolder}/${RushConstants.pnpmPatchesFolderName}`;
Expand All @@ -445,7 +455,9 @@ export class RushPnpmCommandLineParser {
}

// Update patchedDependencies to pnpm configuration file
this._rushConfiguration.pnpmOptions.updateGlobalPatchedDependencies(newGlobalPatchedDependencies);
this._rushConfiguration.defaultSubspace
.getPnpmOptions()
.updateGlobalPatchedDependencies(newGlobalPatchedDependencies);

// Rerun installation to update
await this._doRushUpdateAsync();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ export class PhasedScriptAction extends BaseScriptAction<IPhasedCommandConfig> {
// Only check for a valid link flag when subspaces is not enabled
if (!lastLinkFlag.isValid() && !this.rushConfiguration.subspacesFeatureEnabled) {
const useWorkspaces: boolean =
this.rushConfiguration.pnpmOptions && this.rushConfiguration.pnpmOptions.useWorkspaces;
this.rushConfiguration.defaultSubspace.getPnpmOptions() &&
this.rushConfiguration.defaultSubspace.getPnpmOptions().useWorkspaces;
if (useWorkspaces) {
throw new Error('Link flag invalid.\nDid you run "rush install" or "rush update"?');
} else {
Expand Down
4 changes: 2 additions & 2 deletions libraries/rush-lib/src/logic/InstallManagerFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class InstallManagerFactory {
): Promise<BaseInstallManager> {
if (
rushConfiguration.packageManager === 'pnpm' &&
rushConfiguration.pnpmOptions &&
rushConfiguration.pnpmOptions.useWorkspaces
rushConfiguration.defaultSubspace.getPnpmOptions() &&
rushConfiguration.defaultSubspace.getPnpmOptions().useWorkspaces
) {
return new WorkspaceInstallManager(rushConfiguration, rushGlobalFolder, purgeManager, options);
}
Expand Down
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/logic/PackageJsonUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,8 @@ export class PackageJsonUpdater {
);

const useWorkspaces: boolean = !!(
this._rushConfiguration.pnpmOptions && this._rushConfiguration.pnpmOptions.useWorkspaces
this._rushConfiguration.defaultSubspace.getPnpmOptions() &&
this._rushConfiguration.defaultSubspace.getPnpmOptions().useWorkspaces
);
const workspacePrefix: string = 'workspace:';

Expand Down
8 changes: 5 additions & 3 deletions libraries/rush-lib/src/logic/PurgeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ export class PurgeManager {

if (
this._rushConfiguration.packageManager === 'pnpm' &&
this._rushConfiguration.pnpmOptions.pnpmStore === 'global' &&
this._rushConfiguration.pnpmOptions.pnpmStorePath
this._rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStore === 'global' &&
this._rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath
) {
// eslint-disable-next-line no-console
console.warn(Colorize.yellow(`Purging the global pnpm-store`));
this._rushUserFolderRecycler.moveAllItemsInFolder(this._rushConfiguration.pnpmOptions.pnpmStorePath);
this._rushUserFolderRecycler.moveAllItemsInFolder(
this._rushConfiguration.defaultSubspace.getPnpmOptions().pnpmStorePath
);
}
}

Expand Down
7 changes: 3 additions & 4 deletions libraries/rush-lib/src/logic/RepoStateFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ export class RepoStateFile {
// Only support saving the pnpm shrinkwrap hash if it was enabled
const preventShrinkwrapChanges: boolean =
rushConfiguration.packageManager === 'pnpm' &&
rushConfiguration.pnpmOptions &&
rushConfiguration.pnpmOptions.preventManualShrinkwrapChanges;
subspace.getPnpmOptions() &&
subspace.getPnpmOptions().preventManualShrinkwrapChanges;
if (preventShrinkwrapChanges) {
const pnpmShrinkwrapFile: PnpmShrinkwrapFile | undefined = PnpmShrinkwrapFile.loadFromFile(
subspace.getCommittedShrinkwrapFilename()
Expand All @@ -168,8 +168,7 @@ export class RepoStateFile {
}

// Currently, only support saving the preferred versions hash if using workspaces
const useWorkspaces: boolean =
rushConfiguration.pnpmOptions && rushConfiguration.pnpmOptions.useWorkspaces;
const useWorkspaces: boolean = subspace.getPnpmOptions() && subspace.getPnpmOptions().useWorkspaces;
if (useWorkspaces) {
const commonVersions: CommonVersionsConfiguration = subspace.getCommonVersions();
const preferredVersionsHash: string = commonVersions.getPreferredVersionsHash();
Expand Down
3 changes: 2 additions & 1 deletion libraries/rush-lib/src/logic/UnlinkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export class UnlinkManager {
*/
public unlink(force: boolean = false): boolean {
const useWorkspaces: boolean =
this._rushConfiguration.pnpmOptions && this._rushConfiguration.pnpmOptions.useWorkspaces;
this._rushConfiguration.defaultSubspace.getPnpmOptions() &&
this._rushConfiguration.defaultSubspace.getPnpmOptions().useWorkspaces;
if (!force && useWorkspaces) {
// eslint-disable-next-line no-console
console.log(
Expand Down