Skip to content

Commit 6fb4330

Browse files
Keen Yee Liauayazhafiz
authored andcommitted
fix: check dev bundle exists when in debug mode (#456)
VS Code Insider launches extension in debug mode by defaul, but users install prod bundle so we have to check whether dev bundle exists, and fall back to prod bundle if it does not. PR closes #449
1 parent 3a59de1 commit 6fb4330

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

client/src/extension.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9+
import * as fs from 'fs';
910
import * as path from 'path';
1011
import * as vscode from 'vscode';
1112
import * as lsp from 'vscode-languageclient';
@@ -17,35 +18,8 @@ export function activate(context: vscode.ExtensionContext) {
1718
// If the extension is launched in debug mode then the debug server options are used
1819
// Otherwise the run options are used
1920
const serverOptions: lsp.ServerOptions = {
20-
run: {
21-
module: context.asAbsolutePath(path.join('server')),
22-
transport: lsp.TransportKind.ipc,
23-
args: constructArgs(context, false /* debug */),
24-
options: {
25-
env: {
26-
// Force TypeScript to use the non-polling version of the file watchers.
27-
TSC_NONPOLLING_WATCHER: true,
28-
},
29-
},
30-
},
31-
debug: {
32-
module: context.asAbsolutePath(path.join('server', 'out', 'server.js')),
33-
transport: lsp.TransportKind.ipc,
34-
args: constructArgs(context, true /* debug */),
35-
options: {
36-
env: {
37-
// Force TypeScript to use the non-polling version of the file watchers.
38-
TSC_NONPOLLING_WATCHER: true,
39-
NG_DEBUG: true,
40-
},
41-
execArgv: [
42-
// do not lazily evaluate the code so all breakpoints are respected
43-
'--nolazy',
44-
// If debugging port is changed, update .vscode/launch.json as well
45-
'--inspect=6009',
46-
]
47-
},
48-
},
21+
run: getServerOptions(context, false /* debug */),
22+
debug: getServerOptions(context, true /* debug */),
4923
};
5024

5125
// Options to control the language client
@@ -171,3 +145,40 @@ function constructArgs(ctx: vscode.ExtensionContext, debug: boolean): string[] {
171145

172146
return args;
173147
}
148+
149+
function getServerOptions(ctx: vscode.ExtensionContext, debug: boolean): lsp.NodeModule {
150+
// Environment variables for server process
151+
const prodEnv = {
152+
// Force TypeScript to use the non-polling version of the file watchers.
153+
TSC_NONPOLLING_WATCHER: true,
154+
};
155+
const devEnv = {
156+
...prodEnv,
157+
NG_DEBUG: true,
158+
};
159+
160+
// Node module for the language server
161+
const prodBundle = ctx.asAbsolutePath('server');
162+
const devBundle = ctx.asAbsolutePath(path.join('server', 'out', 'server.js'));
163+
164+
// Argv options for Node.js
165+
const prodExecArgv: string[] = [];
166+
const devExecArgv: string[] = [
167+
// do not lazily evaluate the code so all breakpoints are respected
168+
'--nolazy',
169+
// If debugging port is changed, update .vscode/launch.json as well
170+
'--inspect=6009',
171+
];
172+
173+
return {
174+
// VS Code Insider launches extensions in debug mode by default but users
175+
// install prod bundle so we have to check whether dev bundle exists.
176+
module: debug && fs.existsSync(devBundle) ? devBundle : prodBundle,
177+
transport: lsp.TransportKind.ipc,
178+
args: constructArgs(ctx, debug),
179+
options: {
180+
env: debug ? devEnv : prodEnv,
181+
execArgv: debug ? devExecArgv : prodExecArgv,
182+
},
183+
};
184+
}

0 commit comments

Comments
 (0)