@@ -21,11 +21,22 @@ public abstract class OutOfProcessNodeInstance : INodeInstance
21
21
{
22
22
protected readonly ILogger OutputLogger ;
23
23
private const string ConnectionEstablishedMessage = "[Microsoft.AspNetCore.NodeServices:Listening]" ;
24
+ private const string DebuggingStartedMessageFormat = @"-----
25
+ *** Node.js debugging is enabled ***
26
+ {0}
27
+
28
+ To debug, run:
29
+ node-inspector{1}
30
+
31
+ If you haven't yet installed node-inspector, you can do so as follows:
32
+ npm install -g node-inspector
33
+ -----" ;
24
34
private readonly TaskCompletionSource < object > _connectionIsReadySource = new TaskCompletionSource < object > ( ) ;
25
35
private bool _disposed ;
26
36
private readonly StringAsTempFile _entryPointScript ;
27
37
private FileSystemWatcher _fileSystemWatcher ;
28
38
private readonly Process _nodeProcess ;
39
+ private int ? _nodeDebuggingPort ;
29
40
private bool _nodeProcessNeedsRestart ;
30
41
private readonly string [ ] _watchFileExtensions ;
31
42
@@ -34,7 +45,9 @@ public OutOfProcessNodeInstance(
34
45
string projectPath ,
35
46
string [ ] watchFileExtensions ,
36
47
string commandLineArguments ,
37
- ILogger nodeOutputLogger )
48
+ ILogger nodeOutputLogger ,
49
+ bool launchWithDebugging ,
50
+ int ? debuggingPort )
38
51
{
39
52
if ( nodeOutputLogger == null )
40
53
{
@@ -43,8 +56,9 @@ public OutOfProcessNodeInstance(
43
56
44
57
OutputLogger = nodeOutputLogger ;
45
58
_entryPointScript = new StringAsTempFile ( entryPointScript ) ;
46
-
47
- var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ) ;
59
+
60
+ var startInfo = PrepareNodeProcessStartInfo ( _entryPointScript . FileName , projectPath , commandLineArguments ,
61
+ launchWithDebugging , debuggingPort ) ;
48
62
_nodeProcess = LaunchNodeProcess ( startInfo ) ;
49
63
_watchFileExtensions = watchFileExtensions ;
50
64
_fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
@@ -84,11 +98,23 @@ public void Dispose()
84
98
85
99
// This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
86
100
protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
87
- string entryPointFilename , string projectPath , string commandLineArguments )
101
+ string entryPointFilename , string projectPath , string commandLineArguments ,
102
+ bool launchWithDebugging , int ? debuggingPort )
88
103
{
104
+ string debuggingArgs ;
105
+ if ( launchWithDebugging )
106
+ {
107
+ debuggingArgs = debuggingPort . HasValue ? $ "--debug={ debuggingPort . Value } " : "--debug " ;
108
+ _nodeDebuggingPort = debuggingPort ;
109
+ }
110
+ else
111
+ {
112
+ debuggingArgs = string . Empty ;
113
+ }
114
+
89
115
var startInfo = new ProcessStartInfo ( "node" )
90
116
{
91
- Arguments = "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
117
+ Arguments = debuggingArgs + "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
92
118
UseShellExecute = false ,
93
119
RedirectStandardInput = true ,
94
120
RedirectStandardOutput = true ,
@@ -201,7 +227,12 @@ private void ConnectToInputOutputStreams()
201
227
{
202
228
if ( evt . Data != null )
203
229
{
204
- if ( ! initializationIsCompleted )
230
+ if ( IsDebuggerListeningMessage ( evt . Data ) )
231
+ {
232
+ var debugPortArg = _nodeDebuggingPort . HasValue ? $ " --debug-port={ _nodeDebuggingPort . Value } " : string . Empty ;
233
+ OutputLogger . LogWarning ( string . Format ( DebuggingStartedMessageFormat , evt . Data , debugPortArg ) ) ;
234
+ }
235
+ else if ( ! initializationIsCompleted )
205
236
{
206
237
_connectionIsReadySource . SetException (
207
238
new InvalidOperationException ( "The Node.js process failed to initialize: " + evt . Data ) ) ;
@@ -218,6 +249,11 @@ private void ConnectToInputOutputStreams()
218
249
_nodeProcess . BeginErrorReadLine ( ) ;
219
250
}
220
251
252
+ private static bool IsDebuggerListeningMessage ( string message )
253
+ {
254
+ return message . StartsWith ( "Debugger listening on port " , StringComparison . OrdinalIgnoreCase ) ;
255
+ }
256
+
221
257
private FileSystemWatcher BeginFileWatcher ( string rootDir )
222
258
{
223
259
if ( _watchFileExtensions == null || _watchFileExtensions . Length == 0 )
0 commit comments