refactor(compiler-cli): configure VS Code extension host launch for ngp language service

Add VS Code launch and task configurations for launching and debugging the ngp-vscode-ng-language-service extension host in the monorepo, and fix build and runtime resolution issues when running outside Bazel:

- Add 'NGP VSCE: Launch Dev Client' and compound attach configs in .vscode/launch.json and 'NGP VSCE: build' task in .vscode/tasks.json.
- Update ngp-vscode-ng-language-service/build.js to use server/esbuild-tsconfig.json, mark @typescript/native-preview external, and define import.meta.url via banner.
- Update ngp-vscode-ng-language-service/tsconfig.json for TypeScript 7 compatibility (remove baseUrl, use relative paths, and specify node types).
- Resolve platform tsc/tsgo binary via createRequire from @typescript/native-preview/package.json so pnpm workspace layouts work, and await clientReady before sending angular/setTsApiPipe from fallback ts7Client.
- Allow analyzer_loader.ts to locate ng_analyze_wasm.js when walking up from a realpath'd bundle inside bazel-out.
This commit is contained in:
Andrew Scott
2026-11-18 10:11:16 -08:00
committed by Alon Mishne
parent 96832ab41e
commit 6d49c19e22
8 changed files with 117 additions and 84 deletions
+16
View File
@@ -24,6 +24,18 @@
],
"preLaunchTask": "VSCE: package"
},
{
"type": "extensionHost",
"request": "launch",
"name": "NGP VSCE: Launch Dev Client",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}/ngp-vscode-ng-language-service"
],
"outFiles": ["${workspaceFolder}/ngp-vscode-ng-language-service/dist/**/*.js"],
"preLaunchTask": "NGP VSCE: build"
},
{
"name": "VSCE: Attach to Server",
"type": "node",
@@ -68,6 +80,10 @@
"name": "VSCE: Dev Client + Attach to Server",
"configurations": ["VSCE: Launch Dev Client", "VSCE: Attach to Server"]
},
{
"name": "NGP VSCE: Dev Client + Attach to Server",
"configurations": ["NGP VSCE: Launch Dev Client", "VSCE: Attach to Server"]
},
{
"name": "DEBUG: Run bazel test (Custom Target) + Attach",
"configurations": ["DEBUG: Attach to bazel test", "DEBUG: Run bazel test (Custom Target)"]
+5
View File
@@ -27,6 +27,11 @@
"type": "shell",
"label": "VSCE: package",
"command": "pnpm --filter=ng-template run package"
},
{
"type": "shell",
"label": "NGP VSCE: build",
"command": "pnpm bazel build //packages/compiler-cli/preprocessor/ng-analyze:wasm && pnpm --filter=ngp-vscode-ng-language-service run compile"
}
]
}
+9 -38
View File
@@ -7,7 +7,6 @@
*/
const esbuild = require('esbuild');
const fs = require('fs/promises');
const path = require('path');
async function build() {
@@ -20,11 +19,11 @@ async function build() {
format: 'cjs',
outfile: path.join(__dirname, 'dist/client/extension.js'),
external: ['vscode'],
sourcemap: true,
});
// 2. Build Server
console.log('Building server...');
const repoRoot = path.resolve(__dirname, '..');
await esbuild.build({
entryPoints: [path.join(__dirname, 'server/src/server.ts')],
bundle: true,
@@ -32,46 +31,18 @@ async function build() {
format: 'cjs',
nodePaths: [path.join(__dirname, 'node_modules')],
outfile: path.join(__dirname, 'dist/server/server.js'),
external: [
path.join(__dirname, '../packages/compiler-cli/preprocessor/ng-analyze/index.js'),
'typescript',
],
alias: {
'@angular/compiler-cli/private/hybrid_analysis': path.join(
repoRoot,
'packages/compiler-cli/private/hybrid_analysis.ts',
),
'@angular/compiler-cli/private/migrations': path.join(
repoRoot,
'packages/compiler-cli/private/migrations.ts',
),
'@angular/compiler-cli': path.join(repoRoot, 'packages/compiler-cli/index.ts'),
'@angular/compiler/indexing': path.join(repoRoot, 'packages/compiler/indexing/index.ts'),
'@angular/compiler': path.join(repoRoot, 'packages/compiler/index.ts'),
'@angular/language-service/private': path.join(
repoRoot,
'packages/language-service/private.ts',
),
tsconfig: path.join(__dirname, 'server/esbuild-tsconfig.json'),
external: ['typescript', '@typescript/native-preview', '@typescript/native-preview/*'],
banner: {
js: "const __ngp_import_meta_url = require('node:url').pathToFileURL(__filename).href;",
},
logOverride: {
'empty-import-meta': 'silent',
define: {
'import.meta.url': '__ngp_import_meta_url',
},
sourcemap: true,
});
// 3. Patch Server for import.meta.url compatibility in CJS
console.log('Patching server bundle...');
const serverOutPath = path.join(__dirname, 'dist/server/server.js');
let serverContent = await fs.readFile(serverOutPath, 'utf8');
serverContent = serverContent.replace(
/import_meta(\d*)\.url/g,
"require('url').pathToFileURL(__filename).href",
);
serverContent = serverContent.replace(
/var import_meta(\d*) = \{\};/g,
"var import_meta$1 = { url: require('url').pathToFileURL(__filename).href };",
);
await fs.writeFile(serverOutPath, serverContent);
console.log('Successfully patched server bundle.');
console.log('Successfully built client and server bundles.');
}
build().catch((err) => {
@@ -16,6 +16,7 @@ import {
} from 'vscode-languageclient/node';
import {registerCommands} from './commands';
import * as fs from 'node:fs/promises';
import {createRequire} from 'node:module';
interface TsLanguageServerSdk {
initializeAPIConnection(): Promise<string>;
@@ -96,7 +97,11 @@ export async function activate(context: vscode.ExtensionContext) {
outputChannel.appendLine(`Could not get existing TypeScript API connection: ${err}`);
}
const serverModule = context.asAbsolutePath(path.join('dist', 'server', 'server.js'));
const distServer = context.asAbsolutePath(path.join('dist', 'server', 'server.js'));
const bazelServer = context.asAbsolutePath(
path.join('..', 'dist', 'bin', 'ngp-vscode-ng-language-service', 'server', 'server_bundle.js'),
);
const serverModule = (await pathExists(distServer)) ? distServer : bazelServer;
const serverOptions: ServerOptions = {
run: {module: serverModule, transport: TransportKind.ipc},
debug: {
@@ -142,16 +147,20 @@ export async function activate(context: vscode.ExtensionContext) {
const tsGoPath = await resolveTsGoPath();
// If we already connected to an existing TypeScript LSP server via tsApiPipe
// or are subscribed to `typescriptteam.native-preview`'s language server initialization,
// we do not need to launch a separate fallback ts7Client.
// Otherwise, if tsGoPath is available, launch our fallback ts7Client.
if (!tsApiPipe && !usingNativePreviewExtension && tsGoPath) {
launchTs7Client(tsGoPath, context);
// Launch ts7Client when tsGoPath is available so registerTs7RelayHandlers is active
// for TsGoFacade LSP requests. Only initialize an API session on ts7Client if we
// didn't already connect to typescriptteam.native-preview's API pipe.
if (tsGoPath) {
launchTs7Client(tsGoPath, context, clientReady, !tsApiPipe && !usingNativePreviewExtension);
}
}
function launchTs7Client(tsGoPath: string, context: vscode.ExtensionContext) {
function launchTs7Client(
tsGoPath: string,
context: vscode.ExtensionContext,
clientReady: Promise<void>,
initializeApiSession: boolean,
) {
const isNative =
tsGoPath.includes(`typescript-${process.platform}-${process.arch}`) ||
tsGoPath.includes(`native-preview-${process.platform}-${process.arch}`) ||
@@ -211,12 +220,16 @@ function launchTs7Client(tsGoPath: string, context: vscode.ExtensionContext) {
ts7Client
.start()
.then(async () => {
if (!initializeApiSession) {
return;
}
try {
const res = await ts7Client!.sendRequest<{sessionId: string; pipe: string}>(
'custom/initializeAPISession',
{},
);
if (res?.pipe) {
await clientReady;
client.sendNotification('angular/setTsApiPipe', {pipe: res.pipe});
}
} catch {}
@@ -272,19 +285,39 @@ async function resolveTsGoPath(): Promise<string | null> {
return process.env.TSGO_BINARY_PATH;
}
const extensionRoot = path.resolve(__dirname, '../..');
const workspaceRoot = path.resolve(__dirname, '../../..');
const ext = process.platform === 'win32' ? '.exe' : '';
const tsPlatformName = `@typescript/typescript-${process.platform}-${process.arch}`;
const previewPlatformName = `@typescript/native-preview-${process.platform}-${process.arch}`;
const localCandidates = [
path.join(workspaceRoot, 'node_modules', tsPlatformName, 'lib', `tsc${ext}`),
path.join(workspaceRoot, 'node_modules', '@typescript', 'native-preview', 'lib', 'tsc.js'),
path.join(workspaceRoot, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsc'),
path.join(workspaceRoot, 'node_modules', previewPlatformName, 'lib', `tsgo${ext}`),
path.join(workspaceRoot, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo'),
path.join(workspaceRoot, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo.js'),
];
try {
const req = createRequire(path.join(extensionRoot, 'package.json'));
const previewPkg = req.resolve('@typescript/native-preview/package.json');
const previewReq = createRequire(previewPkg);
const platformTargets = [
{pkg: tsPlatformName, bin: `tsc${ext}`},
{pkg: previewPlatformName, bin: `tsgo${ext}`},
];
for (const target of platformTargets) {
try {
const platPkg = previewReq.resolve(`${target.pkg}/package.json`);
const exe = path.join(path.dirname(platPkg), 'lib', target.bin);
if (await pathExists(exe)) {
return exe;
}
} catch {}
}
} catch {}
const localCandidates = [extensionRoot, workspaceRoot].flatMap((root) => [
path.join(root, 'node_modules', tsPlatformName, 'lib', `tsc${ext}`),
path.join(root, 'node_modules', '@typescript', 'native-preview', 'lib', 'tsc.js'),
path.join(root, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsc'),
path.join(root, 'node_modules', previewPlatformName, 'lib', `tsgo${ext}`),
path.join(root, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo'),
path.join(root, 'node_modules', '@typescript', 'native-preview', 'bin', 'tsgo.js'),
]);
const localResults = await Promise.all(
localCandidates.map(async (candidate) => ((await pathExists(candidate)) ? candidate : null)),
@@ -1,8 +1,17 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"target": "ES2022",
"module": "esnext",
"moduleResolution": "bundler",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"rootDir": ".",
"moduleResolution": "bundler"
"paths": {
"@angular/core": ["../../tests/test-workspace/mock_packages/@angular/core/types/core.d.ts"],
"@angular/core/*": ["../../tests/test-workspace/mock_packages/@angular/core/*"]
}
},
"include": ["*.ts"]
}
+2 -2
View File
@@ -146,10 +146,10 @@
],
"main": "./dist/client/extension.js",
"scripts": {
"build:wasm": "bazel build //packages/compiler-cli/preprocessor/ng-analyze:wasm",
"compile": "tsc -p client --noEmit && tsc -p server --noEmit && node build.js",
"typecheck": "tsc -p client --noEmit && tsc -p server --noEmit",
"test": "pnpm run compile && node e2e/build-tests.js && node e2e/runTests.js",
"watch": "pnpm --prefix client run watch & pnpm --prefix server run watch"
"test": "pnpm run compile && node e2e/build-tests.js && node e2e/runTests.js"
},
"dependencies": {
"@typescript/native-preview": "npm:[email protected]",
+5 -4
View File
@@ -8,16 +8,17 @@
"strict": true,
"skipLibCheck": true,
"outDir": "out",
"baseUrl": ".",
"typeRoots": ["./node_modules/@types"],
"types": ["node"],
"paths": {
"@angular/compiler-cli/private/*": ["../packages/compiler-cli/private/*"],
"@angular/compiler-cli": ["../packages/compiler-cli/index.ts"],
"@angular/compiler": ["../packages/compiler/index.ts"],
"@angular/compiler/indexing": ["../packages/compiler/indexing/index.ts"],
"@angular/language-service/private": ["../packages/language-service/private.ts"],
"vscode-languageserver": ["node_modules/vscode-languageserver/lib/common/api.d.ts"],
"vscode-languageserver/*": ["node_modules/vscode-languageserver/*"],
"vscode-uri": ["node_modules/vscode-uri"]
"vscode-languageserver": ["./node_modules/vscode-languageserver/lib/common/api.d.ts"],
"vscode-languageserver/*": ["./node_modules/vscode-languageserver/*"],
"vscode-uri": ["./node_modules/vscode-uri"]
}
}
}
@@ -211,33 +211,31 @@ async function wasmCandidates(options: LoadAnalyzerOptions): Promise<Candidate[]
}
};
const [ngAnalyzeWasmDirIndex, bundledWasm, bazelBinWasm, inRepoWasm] = await Promise.all([
options.ngAnalyzeDir
? existingPath(path.join(options.ngAnalyzeDir, '..', 'ng-analyze-wasm', 'ng_analyze.js'))
: null,
existingPath(path.resolve(SELF_DIR, '../ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js')),
!IS_INSTALLED
? walkUpFor(
path.join(
'dist',
'bin',
'packages',
'compiler-cli',
'preprocessor',
'ng-analyze',
'ng_analyze_wasm',
'ng_analyze_wasm.js',
),
)
: null,
!IS_INSTALLED ? walkUpFor(path.join('ng-analyze-wasm', 'ng_analyze.js')) : null,
]);
const wasmBinRel = path.join(
'packages',
'compiler-cli',
'preprocessor',
'ng-analyze',
'ng_analyze_wasm',
'ng_analyze_wasm.js',
);
const [ngAnalyzeWasmDirIndex, bundledWasm, bazelBinWasm, bazelOutWasm, inRepoWasm] =
await Promise.all([
options.ngAnalyzeDir
? existingPath(path.join(options.ngAnalyzeDir, '..', 'ng-analyze-wasm', 'ng_analyze.js'))
: null,
existingPath(path.resolve(SELF_DIR, '../ng-analyze/ng_analyze_wasm/ng_analyze_wasm.js')),
!IS_INSTALLED ? walkUpFor(path.join('dist', 'bin', wasmBinRel)) : null,
!IS_INSTALLED ? walkUpFor(wasmBinRel) : null,
!IS_INSTALLED ? walkUpFor(path.join('ng-analyze-wasm', 'ng_analyze.js')) : null,
]);
push(options.wasmBinding, 'wasmBinding option');
push(process.env['NG_EXP_COMPILER_WASM_BINDING'], 'NG_EXP_COMPILER_WASM_BINDING');
push(ngAnalyzeWasmDirIndex, 'ngAnalyzeDir option');
push(bundledWasm, 'bundled ng_analyze_wasm');
push(bazelBinWasm, 'bazel-bin ng_analyze_wasm');
push(bazelOutWasm, 'bazel-out ng_analyze_wasm');
push('ng-exp-compiler-arch-wasm', 'ng-exp-compiler-arch-wasm package');
push(inRepoWasm, 'in-repo ng-analyze-wasm/');