From 0bbd987fdebfe4233e615b2471aa34435b247987 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Fri, 5 Jun 2026 00:29:13 +0000 Subject: [PATCH] [Performance] Memoize isWsl Memoize the isWsl function in packages/cli-kit/src/public/node/system.ts by caching the Promise of the dynamic is-wsl import. This avoids redundant imports and environment checks during process execution. --- .../cli-kit/src/public/node/system.test.ts | 23 +++++++++++++++++++ packages/cli-kit/src/public/node/system.ts | 8 +++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba4..34317bf488 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -393,3 +393,26 @@ describe('readStdinString', () => { await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.') }) }) + +describe('isWsl', () => { + test('memoizes the result', async () => { + // Given + vi.doMock('is-wsl', () => ({default: true})) + + // When + const firstCall = await system.isWsl() + + // Then + expect(firstCall).toBe(true) + + // Given + // We change the mock value. If it's memoized, it should still return the first value. + vi.doMock('is-wsl', () => ({default: false})) + + // When + const secondCall = await system.isWsl() + + // Then + expect(secondCall).toBe(true) + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index b4f1e8cc69..ccd5ec5e46 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -351,14 +351,18 @@ 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 + return (memoizedIsWsl ??= import('is-wsl').then((wsl) => wsl.default)) } /**