From b06af6600b14ed022bf16725b2b2898922a93e3c Mon Sep 17 00:00:00 2001
From: Sergei Zharinov <zharinov@users.noreply.github.com>
Date: Thu, 1 Feb 2024 15:45:55 -0300
Subject: [PATCH] refactor(regex): Inverse dependency on logger util (#26997)

---
 lib/util/regex.ts           | 27 ++++++++++++++++++---------
 lib/workers/global/index.ts | 13 +++++++++++++
 2 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/lib/util/regex.ts b/lib/util/regex.ts
index 94a5522dbf..138915e008 100644
--- a/lib/util/regex.ts
+++ b/lib/util/regex.ts
@@ -2,24 +2,34 @@ import is from '@sindresorhus/is';
 import { CONFIG_VALIDATION } from '../constants/error-messages';
 import { re2 } from '../expose.cjs';
 
-import { logger } from '../logger';
-
-let RegEx: RegExpConstructor;
-
 const cache = new Map<string, RegExp>();
 
-if (!process.env.RENOVATE_X_IGNORE_RE2) {
+type RegExpEngineStatus =
+  | { type: 'available' }
+  | {
+      type: 'unavailable';
+      err: Error;
+    }
+  | { type: 'ignored' };
+
+let status: RegExpEngineStatus;
+let RegEx: RegExpConstructor = RegExp;
+// istanbul ignore next
+if (process.env.RENOVATE_X_IGNORE_RE2) {
+  status = { type: 'ignored' };
+} else {
   try {
     const RE2 = re2();
     // Test if native is working
     new RE2('.*').exec('test');
-    logger.debug('Using RE2 as regex engine');
     RegEx = RE2;
+    status = { type: 'available' };
   } catch (err) {
-    logger.warn({ err }, 'RE2 not usable, falling back to RegExp');
+    status = { type: 'unavailable', err };
   }
 }
-RegEx ??= RegExp;
+
+export const regexEngineStatus = status;
 
 export function regEx(
   pattern: string | RegExp,
@@ -49,7 +59,6 @@ export function regEx(
     }
     return instance;
   } catch (err) {
-    logger.trace({ err }, 'RegEx constructor error');
     const error = new Error(CONFIG_VALIDATION);
     error.validationMessage = err.message;
     error.validationSource = pattern.toString();
diff --git a/lib/workers/global/index.ts b/lib/workers/global/index.ts
index ddcd3ac97c..5a50a7f9ce 100644
--- a/lib/workers/global/index.ts
+++ b/lib/workers/global/index.ts
@@ -20,6 +20,7 @@ import { getProblems, logger, setMeta } from '../../logger';
 import * as hostRules from '../../util/host-rules';
 import * as queue from '../../util/http/queue';
 import * as throttle from '../../util/http/throttle';
+import { regexEngineStatus } from '../../util/regex';
 import { addSecretForSanitizing } from '../../util/sanitize';
 import * as repositoryWorker from '../repository';
 import { autodiscoverRepositories } from './autodiscover';
@@ -111,6 +112,18 @@ export async function resolveGlobalExtends(
 }
 
 export async function start(): Promise<number> {
+  // istanbul ignore next
+  if (regexEngineStatus.type === 'available') {
+    logger.debug('Using RE2 regex engine');
+  } else if (regexEngineStatus.type === 'unavailable') {
+    logger.warn(
+      { err: regexEngineStatus.err },
+      'RE2 not usable, falling back to RegExp',
+    );
+  } else if (regexEngineStatus.type === 'ignored') {
+    logger.debug('RE2 regex engine is ignored via RENOVATE_X_IGNORE_RE2');
+  }
+
   let config: AllConfig;
   try {
     if (is.nonEmptyStringAndNotWhitespace(process.env.AWS_SECRET_ACCESS_KEY)) {
-- 
GitLab