From e625dea08044fdefef5ae3d8087ccc242579fcbe Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Wed, 24 Jun 2026 00:56:40 +0000 Subject: [PATCH] [Performance] Memoize isWsl result This change memoizes the result of the `isWsl` function in `@shopify/cli-kit`. Since `isWsl` involves a dynamic import and potential filesystem checks, memoizing the promise ensures that these operations are only performed once per process. This is particularly beneficial in high-frequency paths like analytics collection. A `_resetIsWsl` function is also provided for test isolation. Added regression tests in `system.test.ts`. --- .../cli-kit/src/public/node/system.test.ts | 29 +++++++++++++++++++ packages/cli-kit/src/public/node/system.ts | 21 ++++++++++++-- 2 files changed, 47 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..04ddf458761 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -393,3 +393,32 @@ describe('readStdinString', () => { await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.') }) }) + +describe('isWsl', () => { + test('memoizes the result', async () => { + // Given + // We need to use the real isWsl but since it's already exported from system.js + // we can check if it returns the same promise. + + // When + const promise1 = system.isWsl() + const promise2 = system.isWsl() + + // Then + expect(promise1).toBe(promise2) + await promise1 + }) + + test('can be reset', async () => { + // Given + const promise1 = system.isWsl() + + // When + ;(system as any)._resetIsWsl() + const promise2 = system.isWsl() + + // Then + expect(promise1).not.toBe(promise2) + await Promise.all([promise1, promise2]) + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index 3f449f8ff34..ccd761357da 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -354,14 +354,29 @@ 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 ??= (async () => { + const wsl = await import('is-wsl') + return wsl.default + })()) +} + +/** + * Resets the memoized value of isWsl. + * This is only used for testing. + */ +export function _resetIsWsl() { + memoizedIsWsl = undefined } /**