-
Notifications
You must be signed in to change notification settings - Fork 697
[rush] Support percentage-based weights for operationSettings in rush-project.json #5679
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
02dea2d
feature: Support percentage-based weights for operationSettings in ru…
LPegasus 50b9ee3
Resolve CodeReview comments for:
LPegasus 2fdb23d
Update libraries/rush-lib/src/schemas/rush-project.schema.json
LPegasus ab905d9
Update libraries/rush-lib/src/schemas/rush-project.schema.json
LPegasus afb6812
PR feedback
octogonz 79279fc
PR feedback
octogonz d05a579
Prepare to publish a MINOR release of Rush
octogonz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
common/changes/@microsoft/rush/rush-pr-issue-5607_2026-03-03-11-24.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Support percentage weight in operationSettings in rush-project.json file.", | ||
| "type": "none" | ||
| } | ||
| ], | ||
| "packageName": "@microsoft/rush" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
libraries/rush-lib/src/cli/parsing/test/__snapshots__/ParseParallelism.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`parseParallelism throwsErrorOnInvalidParallelism 1`] = `"Invalid parallelism value of 'tequila', expected a number, a percentage, or 'max'"`; | ||
| exports[`parseParallelism throwsErrorOnInvalidParallelism 1`] = `"Invalid parallelism value of \\"tequila\\": expected a number, a percentage string, or \\"max\\""`; | ||
|
|
||
| exports[`parseParallelism throwsErrorOnInvalidParallelismPercentage 1`] = `"Invalid percentage value of '200%', value cannot be less than '0%' or more than '100%'"`; | ||
| exports[`parseParallelism throwsErrorOnInvalidParallelismPercentage 1`] = `"Invalid percentage value of \\"200\\": value must not exceed 100%"`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
libraries/rush-lib/src/logic/operations/test/WeightedOperationPlugin.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import os from 'node:os'; | ||
|
|
||
| import type { IPhase } from '../../../api/CommandLineConfiguration'; | ||
| import type { RushConfigurationProject } from '../../../api/RushConfigurationProject'; | ||
| import type { IOperationSettings, RushProjectConfiguration } from '../../../api/RushProjectConfiguration'; | ||
| import { | ||
| type IExecuteOperationsContext, | ||
| PhasedCommandHooks | ||
| } from '../../../pluginFramework/PhasedCommandHooks'; | ||
| import type { IOperationExecutionResult } from '../IOperationExecutionResult'; | ||
| import { Operation } from '../Operation'; | ||
| import { WeightedOperationPlugin } from '../WeightedOperationPlugin'; | ||
| import { MockOperationRunner } from './MockOperationRunner'; | ||
|
|
||
| const MOCK_PHASE: IPhase = { | ||
| name: '_phase:test', | ||
| allowWarningsOnSuccess: false, | ||
| associatedParameters: new Set(), | ||
| dependencies: { | ||
| self: new Set(), | ||
| upstream: new Set() | ||
| }, | ||
| isSynthetic: false, | ||
| logFilenameIdentifier: '_phase_test', | ||
| missingScriptBehavior: 'silent' | ||
| }; | ||
|
|
||
| function createProject(packageName: string): RushConfigurationProject { | ||
| return { | ||
| packageName | ||
| } as RushConfigurationProject; | ||
| } | ||
|
|
||
| function createOperation(options: { | ||
| project: RushConfigurationProject; | ||
| settings?: IOperationSettings; | ||
| isNoOp?: boolean; | ||
| }): Operation { | ||
| const { project, settings, isNoOp } = options; | ||
| return new Operation({ | ||
| phase: MOCK_PHASE, | ||
| project, | ||
| settings, | ||
| runner: new MockOperationRunner(`${project.packageName} (${MOCK_PHASE.name})`, undefined, false, isNoOp), | ||
| logFilenameIdentifier: `${project.packageName}_phase_test` | ||
| }); | ||
| } | ||
|
|
||
| function createExecutionRecords(operation: Operation): Map<Operation, IOperationExecutionResult> { | ||
| return new Map([ | ||
| [ | ||
| operation, | ||
| { | ||
| operation, | ||
| runner: operation.runner | ||
| } as unknown as IOperationExecutionResult | ||
| ] | ||
| ]); | ||
| } | ||
|
|
||
| function createContext( | ||
| projectConfigurations: ReadonlyMap<RushConfigurationProject, RushProjectConfiguration>, | ||
| parallelism: number = os.availableParallelism() | ||
| ): IExecuteOperationsContext { | ||
| return { | ||
| projectConfigurations, | ||
| parallelism | ||
| } as IExecuteOperationsContext; | ||
| } | ||
|
|
||
| async function applyWeightPluginAsync( | ||
| operations: Map<Operation, IOperationExecutionResult>, | ||
| context: IExecuteOperationsContext | ||
| ): Promise<void> { | ||
| const hooks: PhasedCommandHooks = new PhasedCommandHooks(); | ||
| new WeightedOperationPlugin().apply(hooks); | ||
| await hooks.beforeExecuteOperations.promise(operations, context); | ||
| } | ||
|
|
||
| function mockAvailableParallelism(value: number): jest.SpyInstance<number, []> { | ||
| return jest.spyOn(os, 'availableParallelism').mockReturnValue(value); | ||
| } | ||
|
|
||
| describe(WeightedOperationPlugin.name, () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| it('applies numeric weight from operation settings', async () => { | ||
| const project: RushConfigurationProject = createProject('project-number'); | ||
| const operation: Operation = createOperation({ | ||
| project, | ||
| settings: { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: 7 | ||
| } | ||
| }); | ||
|
|
||
| await applyWeightPluginAsync( | ||
| createExecutionRecords(operation), | ||
| createContext(new Map(), /* Set parallelism to ensure -p does not affect weight calculation */ 1) | ||
| ); | ||
|
|
||
| expect(operation.weight).toBe(7); | ||
| }); | ||
|
|
||
| it('converts percentage weight using available parallelism', async () => { | ||
| mockAvailableParallelism(10); | ||
|
|
||
| const project: RushConfigurationProject = createProject('project-percent'); | ||
| const operation: Operation = createOperation({ | ||
| project, | ||
| settings: { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: '25%' | ||
| } as IOperationSettings | ||
| }); | ||
|
|
||
| await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); | ||
|
|
||
| expect(operation.weight).toBe(2); | ||
| }); | ||
|
|
||
| it('reads weight from rush-project configuration when operation settings are undefined', async () => { | ||
| mockAvailableParallelism(8); | ||
|
|
||
| const project: RushConfigurationProject = createProject('project-config'); | ||
| const operation: Operation = createOperation({ project }); | ||
| const projectConfiguration: RushProjectConfiguration = { | ||
| operationSettingsByOperationName: new Map([ | ||
| [ | ||
| MOCK_PHASE.name, | ||
| { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: '50%' | ||
| } as IOperationSettings | ||
| ] | ||
| ]) | ||
| } as unknown as RushProjectConfiguration; | ||
|
|
||
| await applyWeightPluginAsync( | ||
| createExecutionRecords(operation), | ||
| createContext(new Map([[project, projectConfiguration]])) | ||
| ); | ||
|
|
||
| expect(operation.weight).toBe(4); | ||
| }); | ||
|
|
||
| it('use floor when converting percentage weight to avoid zero weight', async () => { | ||
| mockAvailableParallelism(16); | ||
|
|
||
| const project: RushConfigurationProject = createProject('project-floor'); | ||
| const operation: Operation = createOperation({ | ||
| project, | ||
| settings: { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: '33.3333%' | ||
| } as IOperationSettings | ||
| }); | ||
|
|
||
| await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); | ||
|
|
||
| expect(operation.weight).toBe(5); | ||
| }); | ||
|
|
||
| it('forces NO-OP operation weight to zero ignore weight settings', async () => { | ||
| const project: RushConfigurationProject = createProject('project-no-op'); | ||
| const operation: Operation = createOperation({ | ||
| project, | ||
| isNoOp: true, | ||
| settings: { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: 100 | ||
| } | ||
| }); | ||
|
|
||
| await applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())); | ||
|
|
||
| expect(operation.weight).toBe(0); | ||
| }); | ||
|
|
||
| it('throws for invalid percentage weight format', async () => { | ||
| mockAvailableParallelism(16); | ||
|
|
||
| const project: RushConfigurationProject = createProject('project-invalid'); | ||
| const operation: Operation = createOperation({ | ||
| project, | ||
| // @ts-expect-error Testing invalid input | ||
| settings: { | ||
| operationName: MOCK_PHASE.name, | ||
| weight: '12.5a%' | ||
| } as IOperationSettings | ||
| }); | ||
|
|
||
| await expect( | ||
| applyWeightPluginAsync(createExecutionRecords(operation), createContext(new Map())) | ||
| ).rejects.toThrow(/invalid weight: "12.5a%"/i); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find myself wondering why this layer is here;
operation.settingsshould always already be defined ifprojectConfigurations.get(project)?.operationSettingsByOperationName.get(phase.name)returns a truthy value.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems out of scope