Skip to content

[rush-lib] Fix PreferredVersions being ignored when projects have overlapping dependencies #4835

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

Merged
merged 6 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/changes/@microsoft/rush/fix-3205_2024-07-17-01-52.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Fix an issue where PreferredVersions are ignored when a project contains an overlapping dependency entry (https://github.com/microsoft/rushstack/issues/3205)",
"type": "none"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See LICENSE in the project root for license information.

import * as path from 'path';
import * as semver from 'semver';
import { FileSystem, Import, type IPackageJson, JsonFile, MapExtensions } from '@rushstack/node-core-library';

import type { PnpmPackageManager } from '../../api/packageManager/PnpmPackageManager';
Expand Down Expand Up @@ -91,11 +92,16 @@ export class PnpmfileConfiguration {
if ((rushConfiguration.packageManagerOptions as PnpmOptionsConfiguration).useWorkspaces) {
const commonVersionsConfiguration: CommonVersionsConfiguration = subspace.getCommonVersions();
const preferredVersions: Map<string, string> = new Map();
MapExtensions.mergeFromMap(preferredVersions, commonVersionsConfiguration.getAllPreferredVersions());
MapExtensions.mergeFromMap(
preferredVersions,
rushConfiguration.getImplicitlyPreferredVersions(subspace)
);
for (const [name, version] of commonVersionsConfiguration.getAllPreferredVersions()) {
// Use the most restrictive version range available
if (!preferredVersions.has(name) || semver.subset(version, preferredVersions.get(name)!)) {
preferredVersions.set(name, version);
}
}
allPreferredVersions = MapExtensions.toObject(preferredVersions);
allowedAlternativeVersions = MapExtensions.toObject(
commonVersionsConfiguration.allowedAlternativeVersions
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { RushConfiguration } from '../../../api/RushConfiguration';
import { PnpmfileConfiguration } from '../PnpmfileConfiguration';
import { JsonFile, type JsonObject } from '@rushstack/node-core-library';

describe(PnpmfileConfiguration.name, () => {
const repoPath: string = `${__dirname}/repo`;
const rushFilename: string = `${repoPath}/rush3.json`;
const rushConfiguration: RushConfiguration = RushConfiguration.loadFromConfigurationFile(rushFilename);
const shimPath: string = `${rushConfiguration.defaultSubspace.getSubspaceTempFolderPath()}/pnpmfileSettings.json`;

beforeAll(async () => {
const subspace = rushConfiguration.defaultSubspace;
await PnpmfileConfiguration.writeCommonTempPnpmfileShimAsync(
rushConfiguration,
subspace.getSubspaceTempFolderPath(),
subspace
);
});

it('should use the smallest-available SemVer range (preferredVersions)', async () => {
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
expect(shimJson.allPreferredVersions).toHaveProperty('core-js', '3.6.5');
});

it('should use the smallest-available SemVer range (per-project)', async () => {
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
expect(shimJson.allPreferredVersions).toHaveProperty('delay', '5.0.0');
});

it('should override preferredVersions when per-project versions conflict', async () => {
const shimJson: JsonObject = await JsonFile.loadAsync(shimPath);
expect(shimJson.allPreferredVersions).toHaveProperty('find-up', '5.0.0');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "baz",
"version": "1.0.0",
"dependencies": {
"core-js": "^3.0.0",
"delay": "5.0.0",
"find-up": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/common-versions.schema.json",
"preferredVersions": {
"core-js": "3.6.5",
"delay": "4.0.0",
"find-up": "5.0.0"
}
}
14 changes: 14 additions & 0 deletions libraries/rush-lib/src/logic/pnpm/test/repo/rush3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/rush/v5/rush.schema.json",
"pnpmVersion": "7.0.0",
"rushVersion": "5.46.1",
"projects": [
{
"packageName": "baz",
"projectFolder": "apps/baz"
}
],
"pnpmOptions": {
"useWorkspaces": true
}
}
Loading