From 2d698cf0544ccd408de942ece55ff916f8d442a8 Mon Sep 17 00:00:00 2001 From: Matt Bierner <12821956+mjbvz@users.noreply.github.com> Date: Thu, 19 Feb 2026 00:00:24 -0800 Subject: [PATCH] Disallow dynamic require/import in extensions Follow up on https://github.com/microsoft/vscode/pull/296220 Let's make sure more code doesn't introduce this pattern without some thought --- eslint.config.js | 24 +++++++++++++++++++ .../web/src/serverHost.ts | 2 ++ 2 files changed, 26 insertions(+) diff --git a/eslint.config.js b/eslint.config.js index bc3c698a129..c86b226ee6e 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -2112,6 +2112,30 @@ export default tseslint.config( 'comma-dangle': ['warn', 'only-multiline'] } }, + // Extension main sources (excluding tests) + { + files: [ + 'extensions/**/*.ts', + + ], + ignores: [ + 'extensions/**/*.test.ts', + ], + rules: { + // Ban dynamic require() and import() calls in extensions to ensure tree-shaking works + 'no-restricted-syntax': [ + 'warn', + { + 'selector': `CallExpression[callee.name='require'][arguments.0.type!='Literal']`, + 'message': 'Use static imports instead of dynamic require() calls to enable tree-shaking.' + }, + { + 'selector': `ImportExpression[source.type!='Literal']`, + 'message': 'Use static imports instead of dynamic import() calls to enable tree-shaking.' + }, + ], + } + }, // markdown-language-features { files: [ diff --git a/extensions/typescript-language-features/web/src/serverHost.ts b/extensions/typescript-language-features/web/src/serverHost.ts index fdc617868b5..975672a50be 100644 --- a/extensions/typescript-language-features/web/src/serverHost.ts +++ b/extensions/typescript-language-features/web/src/serverHost.ts @@ -104,6 +104,8 @@ function createServerHost( const scriptPath = combinePaths(packageRoot, browser); try { + // This file isn't bundled so we really do want a dynamic import here + // eslint-disable-next-line no-restricted-syntax const { default: module } = await import(/* webpackIgnore: true */ scriptPath); return { module, error: undefined }; } catch (e) {