-
Notifications
You must be signed in to change notification settings - Fork 709
[rush] Add experiment to trim RUSH_ env vars forwarded to operation processes #5994
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
Open
Copilot
wants to merge
3
commits into
main
Choose a base branch
from
copilot/trim-rush-env-vars
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
9 changes: 9 additions & 0 deletions
9
common/changes/@microsoft/rush/trim-rush-env-vars-experiment_2026-08-28-05-20.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,9 @@ | ||
| { | ||
| "changes": [ | ||
| { | ||
| "packageName": "@microsoft/rush", | ||
| "comment": "Add a new `trimRushEnvironmentVariablesForOperations` experiment that, when enabled, omits environment variables whose names begin with `RUSH_` from the environment forwarded to operation processes (e.g. \"build\", \"test\").", | ||
| "type": "minor" | ||
| } | ||
| ] | ||
| } |
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
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
30 changes: 30 additions & 0 deletions
30
libraries/rush-lib/src/logic/operations/TrimRushEnvironmentVariablesPlugin.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,30 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import { RUSH_ENVIRONMENT_VARIABLE_NAME_REGEXP } from '../../api/EnvironmentConfiguration'; | ||
| import type { IPhasedCommandPlugin, PhasedCommandHooks } from '../../pluginFramework/PhasedCommandHooks'; | ||
| import type { IEnvironment } from '../../utilities/Utilities'; | ||
|
|
||
| const PLUGIN_NAME: 'TrimRushEnvironmentVariablesPlugin' = 'TrimRushEnvironmentVariablesPlugin'; | ||
|
|
||
| /** | ||
| * Phased command plugin that removes environment variables whose names begin with `RUSH_` before | ||
| * they are forwarded to operation processes. Enabled via the `trimRushEnvironmentVariablesForOperations` | ||
| * experiment in experiments.json. | ||
| */ | ||
| export class TrimRushEnvironmentVariablesPlugin implements IPhasedCommandPlugin { | ||
| public apply(hooks: PhasedCommandHooks): void { | ||
| hooks.onGraphCreatedAsync.tap(PLUGIN_NAME, (graph) => { | ||
| graph.hooks.createEnvironmentForOperation.tap(PLUGIN_NAME, (env: IEnvironment) => { | ||
| const trimmedEnv: IEnvironment = {}; | ||
| for (const key of Object.getOwnPropertyNames(env)) { | ||
| if (!RUSH_ENVIRONMENT_VARIABLE_NAME_REGEXP.test(key)) { | ||
| trimmedEnv[key] = env[key]; | ||
| } | ||
| } | ||
|
|
||
| return trimmedEnv; | ||
| }); | ||
| }); | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
libraries/rush-lib/src/logic/operations/test/TrimRushEnvironmentVariablesPlugin.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,99 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import path from 'node:path'; | ||
| import { JsonFile } from '@rushstack/node-core-library'; | ||
|
|
||
| import { RushConfiguration } from '../../../api/RushConfiguration'; | ||
| import { CommandLineConfiguration, type IPhasedCommandConfig } from '../../../api/CommandLineConfiguration'; | ||
| import type { Operation } from '../Operation'; | ||
| import type { ICommandLineJson } from '../../../api/CommandLineJson'; | ||
| import { PhasedOperationPlugin } from '../PhasedOperationPlugin'; | ||
| import { ShellOperationRunnerPlugin } from '../ShellOperationRunnerPlugin'; | ||
| import { TrimRushEnvironmentVariablesPlugin } from '../TrimRushEnvironmentVariablesPlugin'; | ||
| import { | ||
| type ICreateOperationsContext, | ||
| type IOperationGraphContext, | ||
| PhasedCommandHooks | ||
| } from '../../../pluginFramework/PhasedCommandHooks'; | ||
| import type { IOperationGraph } from '../IOperationGraph'; | ||
| import { OperationGraphHooks } from '../../../pluginFramework/OperationGraphHooks'; | ||
| import type { IEnvironment } from '../../../utilities/Utilities'; | ||
| import type { IOperationRunnerContext } from '../IOperationRunner'; | ||
| import type { IOperationExecutionResult } from '../IOperationExecutionResult'; | ||
|
|
||
| /** | ||
| * Helper function to create a minimal mock record for testing the createEnvironmentForOperation hook | ||
| */ | ||
| function createMockRecord(operation: Operation): IOperationRunnerContext & IOperationExecutionResult { | ||
| return { | ||
| operation, | ||
| environment: undefined | ||
| } as IOperationRunnerContext & IOperationExecutionResult; | ||
| } | ||
|
|
||
| describe(TrimRushEnvironmentVariablesPlugin.name, () => { | ||
| it('should remove RUSH_-prefixed environment variables while preserving others', async () => { | ||
| const rushJsonFile: string = path.resolve(__dirname, `../../test/parameterIgnoringRepo/rush.json`); | ||
| const commandLineJsonFile: string = path.resolve( | ||
| __dirname, | ||
| `../../test/parameterIgnoringRepo/common/config/rush/command-line.json` | ||
| ); | ||
|
|
||
| const rushConfiguration = RushConfiguration.loadFromConfigurationFile(rushJsonFile); | ||
| const commandLineJson: ICommandLineJson = await JsonFile.loadAsync(commandLineJsonFile); | ||
|
|
||
| const commandLineConfiguration = new CommandLineConfiguration(commandLineJson); | ||
| const buildCommand: IPhasedCommandConfig = commandLineConfiguration.commands.get( | ||
| 'build' | ||
| )! as IPhasedCommandConfig; | ||
|
|
||
| const fakeCreateOperationsContext: Pick< | ||
| ICreateOperationsContext, | ||
| 'phaseSelection' | 'projectSelection' | 'projectConfigurations' | 'rushConfiguration' | ||
| > = { | ||
| phaseSelection: buildCommand.phases, | ||
| projectSelection: new Set(rushConfiguration.projects), | ||
| projectConfigurations: new Map(), | ||
| rushConfiguration | ||
| }; | ||
|
|
||
| const hooks: PhasedCommandHooks = new PhasedCommandHooks(); | ||
|
|
||
| // Apply plugins | ||
| new PhasedOperationPlugin().apply(hooks); | ||
| new ShellOperationRunnerPlugin().apply(hooks); | ||
| new TrimRushEnvironmentVariablesPlugin().apply(hooks); | ||
|
|
||
| const operations: Set<Operation> = await hooks.createOperationsAsync.promise( | ||
| new Set(), | ||
| fakeCreateOperationsContext as ICreateOperationsContext | ||
| ); | ||
|
|
||
| // Set up a mock graph and invoke onGraphCreatedAsync so the plugin registers its graph hooks | ||
| const graphHooks: OperationGraphHooks = new OperationGraphHooks(); | ||
| const fakeGraph: IOperationGraph = { hooks: graphHooks } as IOperationGraph; | ||
| await hooks.onGraphCreatedAsync.promise(fakeGraph, fakeCreateOperationsContext as IOperationGraphContext); | ||
|
|
||
| const operation = Array.from(operations)[0]; | ||
| expect(operation).toBeDefined(); | ||
|
|
||
| const mockRecord = createMockRecord(operation); | ||
|
|
||
| const initialEnvironment: IEnvironment = { | ||
| ...process.env, | ||
| RUSH_TEMP_FOLDER: 'some-temp-folder', | ||
| rush_someMixedCaseVar: 'should also be trimmed', | ||
| RUSHSTACK_FILE_ERROR_BASE_FOLDER: 'should be preserved', | ||
| PATH: process.env.PATH, | ||
| SOME_OTHER_VAR: 'should be preserved' | ||
| }; | ||
|
|
||
| const env: IEnvironment = graphHooks.createEnvironmentForOperation.call(initialEnvironment, mockRecord); | ||
|
|
||
| expect(env.RUSH_TEMP_FOLDER).toBeUndefined(); | ||
| expect(env.rush_someMixedCaseVar).toBeUndefined(); | ||
| expect(env.RUSHSTACK_FILE_ERROR_BASE_FOLDER).toBe('should be preserved'); | ||
| expect(env.SOME_OTHER_VAR).toBe('should be preserved'); | ||
| }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.