Skip to content
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

Fix the regex bug for env variables #6514

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion packages/yarnpkg-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yarnpkg/core",
"version": "4.1.3",
"version": "4.1.4",
"license": "BSD-2-Clause",
"main": "./sources/index.ts",
"exports": {
Expand Down
21 changes: 13 additions & 8 deletions packages/yarnpkg-core/sources/miscUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,26 +467,31 @@
}).join(`|`);
}

export function replaceEnvVariables(value: string, {env}: {env: {[key: string]: string | undefined}}) {
const regex = /\${(?<variableName>[\d\w_]+)(?<colon>:)?(?:-(?<fallback>[^}]*))?}/g;
export function replaceEnvVariables(value: string, { env }: { env: { [key: string]: string | undefined } }) {

Check failure on line 470 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space after '{'

Check failure on line 470 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space before '}'
const regex = /\${(?<variableName>[\w_]+)(?::(?<fallback>[^}]*))?}/g;

return value.replace(regex, (...args) => {
const {variableName, colon, fallback} = args[args.length - 1];
const { variableName, fallback } = args[args.length - 1];

Check failure on line 474 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space after '{'

Check failure on line 474 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

There should be no space before '}'

const variableExist = Object.hasOwn(env, variableName);
const variableValue = env[variableName];

if (variableValue)
// If the variable exists, return its value
if (variableValue !== undefined) {

Check failure on line 479 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Unnecessary { after 'if' condition
return variableValue;
if (variableExist && !colon)
return variableValue;
if (fallback != null)
}

Check failure on line 482 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Trailing spaces not allowed
// If the variable does not exist but a fallback is provided, return the fallback
if (fallback != null) {

Check failure on line 484 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

Unnecessary { after 'if' condition
return fallback;
}

// Throw an error if the variable does not exist and no fallback is provided
throw new UsageError(`Environment variable not found (${variableName})`);
});
}



Check failure on line 494 in packages/yarnpkg-core/sources/miscUtils.ts

View workflow job for this annotation

GitHub Actions / Testing chores

More than 2 blank lines not allowed
export function parseBoolean(value: unknown): boolean {
switch (value) {
case `true`:
Expand Down
Loading