Skip to content

Commit 945f648

Browse files
committed
fix: allPreferredVerions and allowedAlternativeVersions are not recorded in the subspace pnpmfileSettings.json
1 parent 485be33 commit 945f648

10 files changed

Lines changed: 685 additions & 3 deletions

File tree

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export interface IWorkspaceProjectInfo
3232
* The `settings` parameter passed to {@link IPnpmfileShim.hooks.readPackage} and
3333
* {@link IPnpmfileShim.hooks.afterAllResolved}.
3434
*/
35-
export interface ISubspacePnpmfileShimSettings {
36-
semverPath: string;
35+
export interface ISubspacePnpmfileShimSettings extends Omit<IPnpmfileShimSettings, 'workspaceVersions'> {
3736
workspaceProjects: Record<string, IWorkspaceProjectInfo>;
3837
subspaceProjects: Record<string, IWorkspaceProjectInfo>;
3938
userPnpmfilePath?: string;

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33

44
import * as path from 'node:path';
55

6-
import { FileSystem, Import, JsonFile, type IDependenciesMetaTable } from '@rushstack/node-core-library';
6+
import * as semver from 'semver';
7+
8+
import {
9+
FileSystem,
10+
Import,
11+
JsonFile,
12+
MapExtensions,
13+
type IDependenciesMetaTable
14+
} from '@rushstack/node-core-library';
715

816
import { subspacePnpmfileShimFilename, scriptsFolderPath } from '../../utilities/PathConstants';
917
import type { ISubspacePnpmfileShimSettings, IWorkspaceProjectInfo } from './IPnpmfile';
1018
import type { RushConfiguration } from '../../api/RushConfiguration';
1119
import type { RushConfigurationProject } from '../../api/RushConfigurationProject';
1220
import type { PnpmPackageManager } from '../../api/packageManager/PnpmPackageManager';
21+
import type { CommonVersionsConfiguration } from '../../api/CommonVersionsConfiguration';
1322
import { RushConstants } from '../RushConstants';
1423
import type { Subspace } from '../../api/Subspace';
1524
import type { PnpmOptionsConfiguration } from './PnpmOptionsConfiguration';
@@ -80,9 +89,35 @@ export class SubspacePnpmfileConfiguration {
8089
(subspace.contains(project) ? subspaceProjects : workspaceProjects)[packageName] = workspaceProjectInfo;
8190
}
8291

92+
let allPreferredVersions: { [dependencyName: string]: string } = {};
93+
let allowedAlternativeVersions: { [dependencyName: string]: readonly string[] } = {};
94+
95+
// Populate preferred versions from subspace's common-versions.json (same as non-subspace pnpmfile shim)
96+
const pnpmOptions: PnpmOptionsConfiguration =
97+
rushConfiguration.packageManagerOptions as PnpmOptionsConfiguration;
98+
if (pnpmOptions?.useWorkspaces) {
99+
const commonVersionsConfiguration: CommonVersionsConfiguration = subspace.getCommonVersions(variant);
100+
const preferredVersions: Map<string, string> = new Map();
101+
MapExtensions.mergeFromMap(
102+
preferredVersions,
103+
rushConfiguration.getImplicitlyPreferredVersions(subspace, variant)
104+
);
105+
for (const [name, version] of commonVersionsConfiguration.getAllPreferredVersions()) {
106+
if (!preferredVersions.has(name) || semver.subset(version, preferredVersions.get(name)!)) {
107+
preferredVersions.set(name, version);
108+
}
109+
}
110+
allPreferredVersions = MapExtensions.toObject(preferredVersions);
111+
allowedAlternativeVersions = MapExtensions.toObject(
112+
commonVersionsConfiguration.allowedAlternativeVersions
113+
);
114+
}
115+
83116
const settings: ISubspacePnpmfileShimSettings = {
84117
workspaceProjects,
85118
subspaceProjects,
119+
allPreferredVersions,
120+
allowedAlternativeVersions,
86121
semverPath: Import.resolveModule({ modulePath: 'semver', baseFolderPath: __dirname })
87122
};
88123

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 { RushConfiguration } from '../../../api/RushConfiguration';
5+
import { SubspacePnpmfileConfiguration } from '../SubspacePnpmfileConfiguration';
6+
import { JsonFile, type JsonObject } from '@rushstack/node-core-library';
7+
8+
describe(SubspacePnpmfileConfiguration.name, () => {
9+
const repoPath: string = `${__dirname}/repo-with-subspace`;
10+
const rushFilename: string = `${repoPath}/rush.json`;
11+
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(rushFilename);
12+
const shimPath: string = `${rushConfiguration.defaultSubspace.getSubspaceTempFolderPath()}/pnpmfileSettings.json`;
13+
14+
beforeAll(async () => {
15+
const subspace = rushConfiguration.defaultSubspace;
16+
await SubspacePnpmfileConfiguration.writeCommonTempSubspaceGlobalPnpmfileAsync(
17+
rushConfiguration,
18+
subspace,
19+
undefined
20+
);
21+
});
22+
23+
it('should use the smallest-available SemVer range (preferredVersions)', async () => {
24+
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
25+
expect(shimJson.allPreferredVersions).toHaveProperty('@rushstack/terminal', '0.19.2');
26+
});
27+
28+
it('should record allPreferredVersions in pnpmfileSettings.json', async () => {
29+
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
30+
expect(shimJson.allPreferredVersions).toHaveProperty('@rushstack/terminal', '0.19.2');
31+
});
32+
33+
it('should record allowedAlternativeVersions in pnpmfileSettings.json', async () => {
34+
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
35+
const allowedAlternativeVersions = shimJson.allowedAlternativeVersions as
36+
| Record<string, readonly string[]>
37+
| undefined;
38+
expect(allowedAlternativeVersions).toBeDefined();
39+
expect(allowedAlternativeVersions).toHaveProperty('foo', ['1.0.0']);
40+
});
41+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "a",
3+
"version": "1.0.0",
4+
"description": "Test package a to test subspace pnpmfile shim with preferred versions",
5+
"dependencies": {
6+
"@rushstack/terminal": "~0.19.0"
7+
}
8+
}

0 commit comments

Comments
 (0)