Skip to content

fix: use DI for vscode API in nodeLauncher instead of direct import#2369

Merged
connor4312 merged 2 commits into
microsoft:mainfrom
lucygramley:fix/add-vscode-to-external-modules
Jun 3, 2026
Merged

fix: use DI for vscode API in nodeLauncher instead of direct import#2369
connor4312 merged 2 commits into
microsoft:mainfrom
lucygramley:fix/add-vscode-to-external-modules

Conversation

@lucygramley
Copy link
Copy Markdown
Contributor

@lucygramley lucygramley commented Jun 3, 2026

Problem

The vsDebugServerBundle (and other non-extension bundles where isInVsCode=false) fails to build with:

\
X [ERROR] Could not resolve "vscode"

src/targets/node/nodeLauncher.ts:7:24:
  7 │ import * as vscode from 'vscode';

\\

This started after aeb21f0 ("Provide an opt-out for debug entrypoint resolution") added import * as vscode from 'vscode' to nodeLauncher.ts.

Root Cause

The direct import * as vscode from 'vscode' causes two problems for standalone (non-VS Code) bundles:

  1. Build error – esbuild can't resolve the vscode module since it's only in the external list when isInVsCode is true
  2. Runtime error – even if marked external, the emitted require('vscode') would fail at runtime since the module doesn't exist outside VS Code

Fix

Follows the established pattern used in defaultBrowserProvider.ts, nodeBinaryProvider.ts, and other files:

  • import type * as vscodeType from 'vscode' – type-only import, erased at runtime
  • @optional() @inject(VSCodeApi) private readonly vscode?: typeof vscodeType – DI injection, undefined outside VS Code
  • Guard the readConfig call with an if (!this.vscode) check, falling back to the original behavior (no entrypoint resolution)

@connor4312
Copy link
Copy Markdown
Member

THanks for identifying the issue and the PR. We should actually switch the import to import type and DI the vscode API as in

@optional() @inject(VSCodeApi) private readonly vscode?: typeof vscodeType,

Otherwise the code will still error when executed.

@lucygramley lucygramley closed this Jun 3, 2026
@lucygramley lucygramley force-pushed the fix/add-vscode-to-external-modules branch from 908d885 to 031a60a Compare June 3, 2026 17:58
nodeLauncher.ts was directly importing 'vscode', which causes both a
build error (esbuild can't resolve it) and a runtime error (module
doesn't exist outside VS Code) for standalone server bundles like
vsDebugServerBundle.

Switch to the established pattern used elsewhere in the codebase
(e.g. defaultBrowserProvider.ts, nodeBinaryProvider.ts):
- Use 'import type' so no require() is emitted at runtime
- Inject the vscode API via @optional() @Inject(VSCodeApi)
- Guard usage with an undefined check for non-VS Code contexts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lucygramley lucygramley changed the title fix: add 'vscode' to externalModules for standalone server bundles fix: use DI for vscode API in nodeLauncher instead of direct import Jun 3, 2026
@lucygramley lucygramley reopened this Jun 3, 2026
@lucygramley
Copy link
Copy Markdown
Contributor Author

Thanks Connor - I've adjusted the PR. Let me know if it doesn't align with your comments :)

Comment thread src/targets/node/nodeLauncher.ts Outdated
Co-authored-by: Connor Peet <connor@peet.io>
Copy link
Copy Markdown
Member

@connor4312 connor4312 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@connor4312 connor4312 enabled auto-merge (squash) June 3, 2026 22:51
@connor4312 connor4312 merged commit 2df2f9d into microsoft:main Jun 3, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants