From 64e3d2be77905e006c32a7b57d9260b819e98e1c Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Tue, 9 Jun 2026 00:31:28 +0000 Subject: [PATCH] [Performance] Memoize isWsl Optimized the `isWsl` function in `packages/cli-kit/src/public/node/system.ts` by caching the Promise returned by the dynamic import of `is-wsl`. Subsequent calls to `isWsl` now return the same Promise instance, avoiding redundant dynamic imports and re-evaluating the environment check. This is particularly beneficial in high-frequency paths like analytics and metadata collection. Changes: - Added `memoizedIsWsl` module-level variable. - Refactored `isWsl` to return the memoized promise. - Added `_resetIsWsl` for test isolation. - Added unit tests in `packages/cli-kit/src/public/node/system.test.ts` to verify functionality and memoization. --- .../cli-kit/src/public/node/system.test.ts | 28 ++++++++++++++++++- packages/cli-kit/src/public/node/system.ts | 17 +++++++++-- 2 files changed, 41 insertions(+), 4 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..a8de9b41726 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -1,11 +1,14 @@ import * as system from './system.js' import {execa} from 'execa' -import {describe, expect, test, vi} from 'vitest' +import {describe, expect, test, vi, beforeEach} from 'vitest' import which from 'which' import {Readable} from 'stream' import * as fs from 'fs' +vi.mock('is-wsl', () => ({ + default: true, +})) vi.mock('which') vi.mock('execa') vi.mock('fs', async (importOriginal) => { @@ -393,3 +396,26 @@ describe('readStdinString', () => { await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.') }) }) + +describe('isWsl', () => { + beforeEach(() => { + system._resetIsWsl() + }) + + test('returns true when is-wsl is true', async () => { + // When + const got = await system.isWsl() + + // Then + expect(got).toBe(true) + }) + + test('memoizes the result', () => { + // When + const firstCall = system.isWsl() + const secondCall = system.isWsl() + + // Then + expect(firstCall).toBe(secondCall) + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index b4f1e8cc693..2950721a59e 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -351,14 +351,25 @@ 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((wsl) => wsl.default)) +} + +/** + * Resets the memoized value for the WSL check. + */ +export function _resetIsWsl(): void { + memoizedIsWsl = undefined } /**