From 513d5e065821301c7dc00a44faf28a73174ab687 Mon Sep 17 00:00:00 2001 From: doomfrawen <2280792+doomfrawen@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:00:39 +0000 Subject: [PATCH 1/2] Honor script-shell when running rushx commands --- .../rush/rushx-script-shell_2026-11-23.json | 11 ++++ .../rush/browser-approved-packages.json | 4 ++ .../config/subspaces/default/pnpm-lock.yaml | 17 +++++++ .../config/subspaces/default/repo-state.json | 2 +- libraries/rush-lib/package.json | 2 + .../rush-lib/src/cli/RushXCommandLine.ts | 49 +++++++++++++++++- .../src/cli/test/RushXCommandLine.test.ts | 50 +++++++++++++++++-- libraries/rush-lib/src/utilities/Utilities.ts | 17 ++++++- .../rush-lib/src/utilities/npmrcUtilities.ts | 14 ++++++ .../src/utilities/test/Utilities.test.ts | 14 ++++++ 10 files changed, 171 insertions(+), 9 deletions(-) create mode 100644 common/changes/@microsoft/rush/rushx-script-shell_2026-11-23.json diff --git a/common/changes/@microsoft/rush/rushx-script-shell_2026-11-23.json b/common/changes/@microsoft/rush/rushx-script-shell_2026-11-23.json new file mode 100644 index 00000000000..992ef99dc6a --- /dev/null +++ b/common/changes/@microsoft/rush/rushx-script-shell_2026-11-23.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "packageName": "@microsoft/rush", + "comment": "Honor the script-shell setting in common/config/rush/.npmrc when rushx runs a package script.", + "type": "minor" + } + ], + "packageName": "@microsoft/rush", + "email": "2280792+doomfrawen@users.noreply.github.com" +} diff --git a/common/config/rush/browser-approved-packages.json b/common/config/rush/browser-approved-packages.json index 9420e1c3669..54580ecc083 100644 --- a/common/config/rush/browser-approved-packages.json +++ b/common/config/rush/browser-approved-packages.json @@ -90,6 +90,10 @@ "name": "dependency-path", "allowedCategories": [ "libraries" ] }, + { + "name": "ini", + "allowedCategories": [ "libraries" ] + }, { "name": "office-ui-fabric-core", "allowedCategories": [ "libraries" ] diff --git a/common/config/subspaces/default/pnpm-lock.yaml b/common/config/subspaces/default/pnpm-lock.yaml index 13b7ce39782..2e2e953fa65 100644 --- a/common/config/subspaces/default/pnpm-lock.yaml +++ b/common/config/subspaces/default/pnpm-lock.yaml @@ -4339,6 +4339,9 @@ importers: ignore: specifier: ~5.1.6 version: 5.1.9 + ini: + specifier: ~4.1.3 + version: 4.1.3 js-yaml: specifier: ~4.3.2 version: 4.3.2 @@ -4394,6 +4397,9 @@ importers: '@rushstack/webpack-preserve-dynamic-require-plugin': specifier: workspace:* version: link:../../webpack/preserve-dynamic-require-plugin + '@types/ini': + specifier: 4.1.1 + version: 4.1.1 '@types/js-yaml': specifier: 4.0.9 version: 4.0.9 @@ -10487,6 +10493,9 @@ packages: '@types/http-proxy@1.17.17': resolution: {integrity: sha512-ED6LB+Z1AVylNTu7hdzuBqOgMnvG/ld6wGCG8wFnAzKX5uyW2K3WD52v0gnLCTK/VLpXtKckgWuyScYK6cSPaw==} + '@types/ini@4.1.1': + resolution: {integrity: sha1-aYRmSozHTDNI9ASdC/Kxqy0GHKM=, tarball: https://ms-feed-25.pkgs.visualstudio.com/1es-public/_packaging/npm-public/npm/registry/@types/ini/-/ini-4.1.1.tgz} + '@types/is-function@1.0.3': resolution: {integrity: sha512-/CLhCW79JUeLKznI6mbVieGbl4QU5Hfn+6udw1YHZoofASjbQ5zaP5LzAUZYDpRYEjS4/P+DhEgyJ/PQmGGTWw==} @@ -14523,6 +14532,10 @@ packages: resolution: {integrity: sha512-7PnF4oN3CvZF23ADhA5wRaYEQpJ8qygSkbtTXWBeXWXmEVRXK+1ITciHWwHhsjv1TmW0MgacIv6hEi5pX5NQdA==} engines: {node: '>=10'} + ini@4.1.3: + resolution: {integrity: sha1-TDWWdaYHGkaYXrObFOSiwOyYp5U=, tarball: https://ms-feed-25.pkgs.visualstudio.com/1es-public/_packaging/npm-public/npm/registry/ini/-/ini-4.1.3.tgz} + engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0} + inline-style-parser@0.1.1: resolution: {integrity: sha512-7NXolsK4CAS5+xvdj5OMMbI962hU/wvwoxk+LWR9Ek9bVtyuuYScDN6eS0rUm6TxApFpw7CX1o4uJzcd4AyD3Q==} @@ -27048,6 +27061,8 @@ snapshots: dependencies: '@types/node': 22.9.3 + '@types/ini@4.1.1': {} + '@types/is-function@1.0.3': {} '@types/istanbul-lib-coverage@2.0.4': {} @@ -32713,6 +32728,8 @@ snapshots: ini@2.0.0: {} + ini@4.1.3: {} + inline-style-parser@0.1.1: {} internal-slot@1.1.0: diff --git a/common/config/subspaces/default/repo-state.json b/common/config/subspaces/default/repo-state.json index 3d09725be2d..de7714ecf1e 100644 --- a/common/config/subspaces/default/repo-state.json +++ b/common/config/subspaces/default/repo-state.json @@ -1,5 +1,5 @@ // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. { - "pnpmShrinkwrapHash": "a21e9138c38033203ce379c3ec1a9aa12562d3c6", + "pnpmShrinkwrapHash": "516309811e91bb4449f5b5021b5abdf2a5cf275c", "preferredVersionsHash": "029c99bd6e65c5e1f25e2848340509811ff9753c" } diff --git a/libraries/rush-lib/package.json b/libraries/rush-lib/package.json index a4920c16f95..c14e8b73057 100644 --- a/libraries/rush-lib/package.json +++ b/libraries/rush-lib/package.json @@ -65,6 +65,7 @@ "git-repo-info": "~2.1.0", "https-proxy-agent": "~5.0.0", "ignore": "~5.1.6", + "ini": "~4.1.3", "@inquirer/checkbox": "~5.1.3", "@inquirer/confirm": "~6.0.11", "@inquirer/input": "~5.0.11", @@ -90,6 +91,7 @@ "@rushstack/operation-graph": "workspace:*", "@rushstack/webpack-deep-imports-plugin": "workspace:*", "@rushstack/webpack-preserve-dynamic-require-plugin": "workspace:*", + "@types/ini": "4.1.1", "@types/js-yaml": "4.0.9", "@types/npm-package-arg": "6.1.0", "@types/object-hash": "~3.0.6", diff --git a/libraries/rush-lib/src/cli/RushXCommandLine.ts b/libraries/rush-lib/src/cli/RushXCommandLine.ts index 0b625d3e9c4..2e1886f10fa 100644 --- a/libraries/rush-lib/src/cli/RushXCommandLine.ts +++ b/libraries/rush-lib/src/cli/RushXCommandLine.ts @@ -4,6 +4,7 @@ import * as path from 'node:path'; import type * as childProcess from 'node:child_process'; +import { parse as parseIni } from 'ini'; import { type ILogMessageCallbackOptions, pnpmSyncCopyAsync } from 'pnpm-sync-lib'; import { @@ -25,6 +26,7 @@ import { } from '@rushstack/terminal'; import { Utilities, type ILifecycleCommandOptions } from '../utilities/Utilities'; +import { expandNpmrcValue, trimNpmrcFileLines } from '../utilities/npmrcUtilities'; import { ProjectCommandSet } from '../logic/ProjectCommandSet'; import { type ILaunchOptions, Rush } from '../api/Rush'; import { RushConfiguration } from '../api/RushConfiguration'; @@ -36,7 +38,11 @@ import { EnvironmentConfiguration, EnvironmentVariableNames } from '../api/Envir import { RushConstants } from '../logic/RushConstants'; import { PnpmSyncUtilities } from '../utilities/PnpmSyncUtilities'; import { initializeDotEnv, loadDotEnvForEnvironment } from '../logic/dotenv'; -import { escapeArgumentIfNeeded } from '../utilities/executionUtilities'; +import { + escapeArgumentIfNeeded, + IS_WINDOWS, + quoteShellArgumentIfNeeded +} from '../utilities/executionUtilities'; /** Native Rushx arguments. Options after the command belong to the script. @beta */ export interface IRushXCommandLineArguments { @@ -332,10 +338,14 @@ async function _launchRushXInternalAsync( throw Error(errorMessage); } + const scriptShell: string | undefined = + rushConfiguration && _getScriptShell(rushConfiguration, execution?.environment ?? process.env); let commandWithArgs: string = scriptBody; let commandWithArgsForDisplay: string = scriptBody; if (commandArgs.length > 0) { - const escapedRemainingArgs: string[] = commandArgs.map((x) => escapeArgumentIfNeeded(x)); + const escapedRemainingArgs: string[] = commandArgs.map((arg) => + scriptShell ? _quoteScriptShellArgument(arg, scriptShell) : escapeArgumentIfNeeded(arg) + ); commandWithArgs += ' ' + escapedRemainingArgs.join(' '); // Display it nicely without the extra quotes @@ -349,6 +359,7 @@ async function _launchRushXInternalAsync( const packageFolder: string = path.dirname(packageJsonFilePath); const lifecycleOptions: ILifecycleCommandOptions = { + scriptShell, rushConfiguration: lifecycleConfiguration, workingDirectory: packageFolder, // If there is a rush.json then use its .npmrc from the temp folder. @@ -397,6 +408,40 @@ async function _launchRushXInternalAsync( } } +function _quoteScriptShellArgument(argument: string, shell: string): string { + const shellName: string = path.win32 + .basename(shell) + .toLowerCase() + .replace(/\.exe$/, ''); + if (shellName === 'pwsh' || shellName === 'powershell') { + // Quoting a simple parameter name such as -Force would turn it into a positional argument. + if (/^[A-Za-z0-9_./-]+$/.test(argument)) return argument; + // PowerShell accepts both ASCII and typographic single quotes as string delimiters. + return `'${argument.replace(/['\u2018\u2019\u201a\u201b]/g, '$&$&')}'`; + } + return quoteShellArgumentIfNeeded(argument, IS_WINDOWS && shellName === 'cmd'); +} + +function _getScriptShell( + rushConfiguration: RushConfiguration, + environment: NodeJS.ProcessEnv +): string | undefined { + let npmrc: string; + try { + npmrc = FileSystem.readFile(path.join(rushConfiguration.commonRushConfigFolder, '.npmrc')); + } catch (error) { + if (FileSystem.isNotExistError(error as Error)) return undefined; + throw error; + } + + // Match the .npmrc that Rush provides to the package manager: a missing variable disables that line. + const lines: string[] = trimNpmrcFileLines(npmrc.split('\n'), environment, rushConfiguration.isPnpm); + const shell: unknown = parseIni(lines.join('\n'))['script-shell']; + return typeof shell === 'string' && shell + ? expandNpmrcValue(shell, environment, rushConfiguration.isPnpm) + : undefined; +} + function _getLifecycleConfiguration( configuration: RushConfiguration | undefined, execution: IRushXCommandOptions | undefined diff --git a/libraries/rush-lib/src/cli/test/RushXCommandLine.test.ts b/libraries/rush-lib/src/cli/test/RushXCommandLine.test.ts index 0bb398c4698..322c1643a9b 100644 --- a/libraries/rush-lib/src/cli/test/RushXCommandLine.test.ts +++ b/libraries/rush-lib/src/cli/test/RushXCommandLine.test.ts @@ -5,7 +5,9 @@ jest.mock('../../logic/dotenv', () => ({ initializeDotEnv: () => {} })); -import { PackageJsonLookup } from '@rushstack/node-core-library'; +import * as path from 'node:path'; + +import { FileSystem, PackageJsonLookup } from '@rushstack/node-core-library'; import { Utilities } from '../../utilities/Utilities'; import { Rush, type ILaunchOptions } from '../../api/Rush'; @@ -57,12 +59,12 @@ describe(RushXCommandLine.name, () => { } as RushConfigurationProject ]; rushConfiguration = { - commonRushConfigFolder: '', + commonRushConfigFolder: 'common/config/rush', rushJsonFolder: '', commonTempFolder: 'common/temp', projects, - tryGetProjectForPath(path: string): RushConfigurationProject | undefined { - return projects.find((project) => project.projectFolder === path); + tryGetProjectForPath(projectPath: string): RushConfigurationProject | undefined { + return projects.find((project) => project.projectFolder === projectPath); } } as RushConfiguration; jest.spyOn(RushConfiguration, 'tryFindRushJsonLocation').mockReturnValue('/Users/jdoe/bigrepo'); @@ -135,6 +137,46 @@ describe(RushXCommandLine.name, () => { expect(logMock!.mock.calls).toMatchSnapshot(); }); + it('uses the repository .npmrc shell and quotes arguments for it', async () => { + jest.replaceProperty(process, 'env', { RUSHX_TEST_SHELL: '/path with spaces/bash' }); + const readFile = jest + .spyOn(FileSystem, 'readFile') + .mockReturnValue( + [ + '; script-shell=ignored', + 'script-shell=other', + 'script-shell = "${RUSHX_TEST_SHELL}"', + 'script-shell=${RUSHX_UNSET_SHELL}' + ].join('\n') + ); + executeLifecycleCommandMock!.mockReturnValue(0); + process.argv = ['node', 'startx.js', 'build', 'argument with spaces', '$literal', "it's", '']; + + await RushXCommandLine.launchRushXAsync('0.0.0', { isManaged: true }); + + expect(readFile).toHaveBeenCalledWith(path.join('common/config/rush', '.npmrc')); + expect(executeLifecycleCommandMock).toHaveBeenCalledWith( + "an acme project build command 'argument with spaces' '$literal' 'it'\\''s' ''", + expect.objectContaining({ scriptShell: '/path with spaces/bash' }) + ); + }); + + it.each(['pwsh', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\PowerShell.exe'])( + 'quotes literal arguments for %s', + async (shell: string) => { + jest.spyOn(FileSystem, 'readFile').mockReturnValue(`script-shell=${shell}`); + executeLifecycleCommandMock!.mockReturnValue(0); + process.argv = ['node', 'startx.js', 'build', '-Switch', 'plain', "O'Brien", 'it’s', '$literal', '']; + + await RushXCommandLine.launchRushXAsync('0.0.0', { isManaged: true }); + + expect(executeLifecycleCommandMock).toHaveBeenCalledWith( + "an acme project build command -Switch plain 'O''Brien' 'it’’s' '$literal' ''", + expect.objectContaining({ scriptShell: shell }) + ); + } + ); + it('fails if the package does not contain a matching script', () => { process.argv = ['node', 'startx.js', 'asdf']; executeLifecycleCommandMock!.mockReturnValue(0); diff --git a/libraries/rush-lib/src/utilities/Utilities.ts b/libraries/rush-lib/src/utilities/Utilities.ts index 35059c6e212..32f9d89264c 100644 --- a/libraries/rush-lib/src/utilities/Utilities.ts +++ b/libraries/rush-lib/src/utilities/Utilities.ts @@ -80,6 +80,11 @@ export interface IInstallPackageInDirectoryOptions { } export interface ILifecycleCommandOptions { + /** + * An explicit shell for executing the lifecycle script, as configured by rushx's script-shell setting. + */ + scriptShell?: string; + /** * Rush configuration paths used to prepare the lifecycle environment, if running in a Rush repo. */ @@ -491,7 +496,7 @@ export class Utilities { } /** - * Executes the command using cmd if running on windows, or using sh if running on a non-windows OS. + * Executes the command using the configured script shell, defaulting to cmd.exe on Windows and sh on POSIX. * @param command - the command to run on shell * @param options - options for how the command should be run */ @@ -525,7 +530,7 @@ export class Utilities { } /** - * Executes the command using cmd if running on windows, or using sh if running on a non-windows OS. + * Executes the command using the configured script shell, defaulting to cmd.exe on Windows and sh on POSIX. * @param command - the command to run on shell * @param options - options for how the command should be run */ @@ -769,6 +774,14 @@ function _executeLifecycleCommandInternal( Object.assign(spawnOptions, SubprocessTerminator.RECOMMENDED_OPTIONS); } + if (options.scriptShell) { + // Let Node pass the complete script to this shell using its native switches (including cmd.exe's /d /s /c). + return spawnFunction(commandAndArgs, [], { + ...spawnOptions, + shell: IS_WINDOWS ? path.win32.normalize(options.scriptShell) : options.scriptShell + }); + } + const { command, args } = Utilities._convertCommandAndArgsToShell( commandAndArgs, IS_WINDOWS, diff --git a/libraries/rush-lib/src/utilities/npmrcUtilities.ts b/libraries/rush-lib/src/utilities/npmrcUtilities.ts index e2d2dd9ba9e..1a0f97e4a74 100644 --- a/libraries/rush-lib/src/utilities/npmrcUtilities.ts +++ b/libraries/rush-lib/src/utilities/npmrcUtilities.ts @@ -277,6 +277,20 @@ interface IEnvironmentVariableExpansionResult { hasUndefinedVariable: boolean; } +/** Resolves a setting with the same environment variable syntax as Rush's generated .npmrc. */ +export function expandNpmrcValue( + value: string, + env: NodeJS.ProcessEnv, + supportEnvVarFallbackSyntax: boolean +): string | undefined { + const { expandedText, hasUndefinedVariable } = _expandEnvironmentVariables( + value, + env, + supportEnvVarFallbackSyntax + ); + return hasUndefinedVariable ? undefined : expandedText; +} + // This finds environment variable tokens that look like "${VAR_NAME}" const ENVIRONMENT_VARIABLE_REGEX: RegExp = /\$\{([^\}]+)\}/g; diff --git a/libraries/rush-lib/src/utilities/test/Utilities.test.ts b/libraries/rush-lib/src/utilities/test/Utilities.test.ts index 7ab68d6f0af..18e6e0d716f 100644 --- a/libraries/rush-lib/src/utilities/test/Utilities.test.ts +++ b/libraries/rush-lib/src/utilities/test/Utilities.test.ts @@ -350,6 +350,20 @@ describe(Utilities.name, () => { } }); + it('runs the script directly in the configured shell instead of wrapping it in the platform default', () => { + const spawn = jest.fn().mockReturnValue(new ChildProcess()); + Utilities.executeLifecycleCommandAsync('echo $0', { + rushConfiguration: undefined, + workingDirectory: process.cwd(), + initCwd: process.cwd(), + handleOutput: false, + environmentPathOptions: {}, + scriptShell: 'bash', + spawn + }); + expect(spawn).toHaveBeenCalledWith('echo $0', [], expect.objectContaining({ shell: 'bash' })); + }); + it('does not pass the daemon routing and host settings to lifecycle commands', () => { const spawn = jest .fn, SpawnOptions]>() From a11345dac84f662c590dd081dafa8b0e22575688 Mon Sep 17 00:00:00 2001 From: doomfrawen <2280792+doomfrawen@users.noreply.github.com> Date: Mon, 23 Nov 2026 04:21:01 +0000 Subject: [PATCH 2/2] Update the Rush build tests lockfile for ini --- .../config/subspaces/build-tests-subspace/pnpm-lock.yaml | 7 +++++++ .../config/subspaces/build-tests-subspace/repo-state.json | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml b/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml index 08f760e7c08..79ae782a247 100644 --- a/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml +++ b/common/config/subspaces/build-tests-subspace/pnpm-lock.yaml @@ -2344,6 +2344,10 @@ packages: inherits@2.0.4: resolution: {integrity: sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==} + ini@4.1.3: + resolution: {integrity: sha1-TDWWdaYHGkaYXrObFOSiwOyYp5U=, tarball: https://ms-feed-25.pkgs.visualstudio.com/1es-public/_packaging/npm-public/npm/registry/ini/-/ini-4.1.3.tgz} + engines: {node: ^14.17.0 || ^16.13.0 || >=18.0.0} + internal-slot@1.1.0: resolution: {integrity: sha512-4gd7VpWNQNB4UKKCFFVcp1AVv+FMOgs9NKzjHKusc8jTMhd5eL1NqQqOpE0KzMds804/yHlglp3uxgluOqAPLw==} engines: {node: '>= 0.4'} @@ -4385,6 +4389,7 @@ snapshots: git-repo-info: 2.1.1 https-proxy-agent: 5.0.1 ignore: 5.1.9 + ini: 4.1.3 js-yaml: 4.3.2 npm-package-arg: 6.1.1 object-hash: 3.0.0 @@ -6664,6 +6669,8 @@ snapshots: inherits@2.0.4: {} + ini@4.1.3: {} + internal-slot@1.1.0: dependencies: es-errors: 1.3.0 diff --git a/common/config/subspaces/build-tests-subspace/repo-state.json b/common/config/subspaces/build-tests-subspace/repo-state.json index 1449961bcde..946c4a13678 100644 --- a/common/config/subspaces/build-tests-subspace/repo-state.json +++ b/common/config/subspaces/build-tests-subspace/repo-state.json @@ -1,6 +1,6 @@ // DO NOT MODIFY THIS FILE MANUALLY BUT DO COMMIT IT. It is generated and used by Rush. { - "pnpmShrinkwrapHash": "18a657d83020df41f6602b7c9087b7253cfee8e0", + "pnpmShrinkwrapHash": "772a353c00c88e22c6013a12be374a5f05f8d08b", "preferredVersionsHash": "550b4cee0bef4e97db6c6aad726df5149d20e7d9", - "packageJsonInjectedDependenciesHash": "293debc6ed16567f5567a7748229a73be6647cc6" + "packageJsonInjectedDependenciesHash": "400579fe4f2de7eb21192076cccd9dbe96dd64da" }