From e123e49f042b1aed3c7796559a867da0b9bbdfc0 Mon Sep 17 00:00:00 2001 From: LeSingh1 Date: Thu, 11 Jun 2026 23:27:49 -0700 Subject: [PATCH] [ts-command-line] Fix EnvironmentVariableParser to report env var name in error messages When a list parameter's environment variable contained an invalid JSON array, the two error messages in EnvironmentVariableParser.parseAsList accidentally interpolated the variable's value (environmentValue) where the variable's name (envVarName) was intended. The result was confusing messages like "The [u environment variable value looks like a JSON array but failed to parse" instead of "The MY_VAR environment variable value looks like a JSON array but failed to parse". Update the two affected template strings to use envVarName, and correct the corresponding test assertion and snapshot that were written to match the old (wrong) output. --- .../fix-env-var-name-in-error-message_2026-06-11.json | 11 +++++++++++ .../src/parameters/EnvironmentVariableParser.ts | 4 ++-- .../src/test/CommandLineParameter.test.ts | 2 +- .../__snapshots__/CommandLineParameter.test.ts.snap | 2 +- 4 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 common/changes/@rushstack/ts-command-line/fix-env-var-name-in-error-message_2026-06-11.json diff --git a/common/changes/@rushstack/ts-command-line/fix-env-var-name-in-error-message_2026-06-11.json b/common/changes/@rushstack/ts-command-line/fix-env-var-name-in-error-message_2026-06-11.json new file mode 100644 index 00000000000..b583578aa8c --- /dev/null +++ b/common/changes/@rushstack/ts-command-line/fix-env-var-name-in-error-message_2026-06-11.json @@ -0,0 +1,11 @@ +{ + "changes": [ + { + "comment": "Fix EnvironmentVariableParser error messages to report the environment variable name instead of its value", + "type": "patch", + "packageName": "@rushstack/ts-command-line" + } + ], + "packageName": "@rushstack/ts-command-line", + "email": "sshaurya914@gmail.com" +} diff --git a/libraries/ts-command-line/src/parameters/EnvironmentVariableParser.ts b/libraries/ts-command-line/src/parameters/EnvironmentVariableParser.ts index f25dd17b015..84dcffef739 100644 --- a/libraries/ts-command-line/src/parameters/EnvironmentVariableParser.ts +++ b/libraries/ts-command-line/src/parameters/EnvironmentVariableParser.ts @@ -30,14 +30,14 @@ export class EnvironmentVariableParser { !parsedJson.every((x) => typeof x === 'string' || typeof x === 'boolean' || typeof x === 'number') ) { throw new Error( - `The ${environmentValue} environment variable value must be a JSON ` + + `The ${envVarName} environment variable value must be a JSON ` + ` array containing only strings, numbers, and booleans.` ); } return parsedJson.map((x) => x.toString()); } catch (ex) { throw new Error( - `The ${environmentValue} environment variable value looks like a JSON array` + + `The ${envVarName} environment variable value looks like a JSON array` + ` but failed to parse: ` + (ex as Error).message ); diff --git a/libraries/ts-command-line/src/test/CommandLineParameter.test.ts b/libraries/ts-command-line/src/test/CommandLineParameter.test.ts index d8668b9c37a..c2416f73c47 100644 --- a/libraries/ts-command-line/src/test/CommandLineParameter.test.ts +++ b/libraries/ts-command-line/src/test/CommandLineParameter.test.ts @@ -439,7 +439,7 @@ describe(CommandLineParameterBase.name, () => { } expect(error).toMatch( - /^The \[u environment variable value looks like a JSON array but failed to parse: Unexpected token / + /^The ENV_COLOR environment variable value looks like a JSON array but failed to parse: Unexpected token / ); }); diff --git a/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap b/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap index b2fec498707..9d03ce2fb0b 100644 --- a/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap +++ b/libraries/ts-command-line/src/test/__snapshots__/CommandLineParameter.test.ts.snap @@ -30,7 +30,7 @@ Array [ ] `; -exports[`CommandLineParameterBase choice list raises an error if env var value is json containing non-scalars 1`] = `"The [{}] environment variable value looks like a JSON array but failed to parse: The [{}] environment variable value must be a JSON array containing only strings, numbers, and booleans."`; +exports[`CommandLineParameterBase choice list raises an error if env var value is json containing non-scalars 1`] = `"The ENV_COLOR environment variable value looks like a JSON array but failed to parse: The ENV_COLOR environment variable value must be a JSON array containing only strings, numbers, and booleans."`; exports[`CommandLineParameterBase choice list raises an error if env var value is not a valid choice 1`] = `"Invalid value \\"oblong\\" for the environment variable ENV_COLOR. Valid choices are: \\"purple\\", \\"yellow\\", \\"pizza\\""`;