From 56b56cca64e29de8c0300a3b72f5dac6b2869fe4 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Tue, 16 Jun 2026 00:27:32 +0000 Subject: [PATCH] [Performance] Memoize isWsl result The `isWsl` function in `packages/cli-kit/src/public/node/system.ts` performs a dynamic import of the `is-wsl` package every time it is called. Since the environment (WSL or not) does not change during the execution of the process, this result can be safely memoized to avoid redundant imports and computations. This PR memoizes the promise returned by the dynamic `is-wsl` import in the `isWsl` function. - Introduced a module-level `memoizedIsWsl` variable. - Updated `isWsl` to use the nullish coalescing assignment operator (`??=`) to cache and return the promise. - Exported an internal `_resetIsWsl` function to allow clearing the cache during unit tests, ensuring test isolation. - Added unit tests in `packages/cli-kit/src/public/node/system.test.ts` to verify memoization and reset behavior. --- .../cli-kit/src/public/node/system.test.ts | 26 +++++++++++++++++++ packages/cli-kit/src/public/node/system.ts | 18 ++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba40..9796e0a3b74 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -353,6 +353,32 @@ describe('isStdinPiped', () => { }) }) +describe('isWsl', () => { + test('memoizes the result', async () => { + // When + const result1 = system.isWsl() + const result2 = system.isWsl() + + // Then + expect(result1).toBe(result2) + await expect(result1).resolves.toBeTypeOf('boolean') + }) + + test('clears the cache when reset', async () => { + // Given + const result1 = system.isWsl() + system._resetIsWsl() + + // When + const result2 = system.isWsl() + + // Then + expect(result1).not.toBe(result2) + await expect(result1).resolves.toBeTypeOf('boolean') + await expect(result2).resolves.toBeTypeOf('boolean') + }) +}) + describe('readStdinString', () => { test('returns undefined when stdin is not piped', async () => { // Given diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index 3f449f8ff34..c8f1824d3ed 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -354,14 +354,26 @@ export function isCI(): boolean { return isTruthy(process.env.CI) } +/** + * Memoized value for the WSL check. + */ +let memoizedIsWsl: Promise | undefined + /** * Check if the current environment is a WSL environment. * * @returns True if the current environment is a WSL environment. */ -export async function isWsl(): Promise { - const wsl = await import('is-wsl') - return wsl.default +export function isWsl(): Promise { + return (memoizedIsWsl ??= import('is-wsl').then((module) => module.default)) +} + +/** + * Resets the memoized value for the WSL check. + * This is used for testing purposes. + */ +export function _resetIsWsl() { + memoizedIsWsl = undefined } /**