@@ -21,11 +21,22 @@ public abstract class OutOfProcessNodeInstance : INodeInstance
2121 {
2222 protected readonly ILogger OutputLogger ;
2323 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+ -----" ;
2434 private readonly TaskCompletionSource < object > _connectionIsReadySource = new TaskCompletionSource < object > ( ) ;
2535 private bool _disposed ;
2636 private readonly StringAsTempFile _entryPointScript ;
2737 private FileSystemWatcher _fileSystemWatcher ;
2838 private readonly Process _nodeProcess ;
39+ private int ? _nodeDebuggingPort ;
2940 private bool _nodeProcessNeedsRestart ;
3041 private readonly string [ ] _watchFileExtensions ;
3142
@@ -34,7 +45,9 @@ public OutOfProcessNodeInstance(
3445 string projectPath ,
3546 string [ ] watchFileExtensions ,
3647 string commandLineArguments ,
37- ILogger nodeOutputLogger )
48+ ILogger nodeOutputLogger ,
49+ bool launchWithDebugging ,
50+ int ? debuggingPort )
3851 {
3952 if ( nodeOutputLogger == null )
4053 {
@@ -43,8 +56,9 @@ public OutOfProcessNodeInstance(
4356
4457 OutputLogger = nodeOutputLogger ;
4558 _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 ) ;
4862 _nodeProcess = LaunchNodeProcess ( startInfo ) ;
4963 _watchFileExtensions = watchFileExtensions ;
5064 _fileSystemWatcher = BeginFileWatcher ( projectPath ) ;
@@ -84,11 +98,23 @@ public void Dispose()
8498
8599 // This method is virtual, as it provides a way to override the NODE_PATH or the path to node.exe
86100 protected virtual ProcessStartInfo PrepareNodeProcessStartInfo (
87- string entryPointFilename , string projectPath , string commandLineArguments )
101+ string entryPointFilename , string projectPath , string commandLineArguments ,
102+ bool launchWithDebugging , int ? debuggingPort )
88103 {
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+
89115 var startInfo = new ProcessStartInfo ( "node" )
90116 {
91- Arguments = "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
117+ Arguments = debuggingArgs + "\" " + entryPointFilename + "\" " + ( commandLineArguments ?? string . Empty ) ,
92118 UseShellExecute = false ,
93119 RedirectStandardInput = true ,
94120 RedirectStandardOutput = true ,
@@ -201,7 +227,12 @@ private void ConnectToInputOutputStreams()
201227 {
202228 if ( evt . Data != null )
203229 {
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 )
205236 {
206237 _connectionIsReadySource . SetException (
207238 new InvalidOperationException ( "The Node.js process failed to initialize: " + evt . Data ) ) ;
@@ -218,6 +249,11 @@ private void ConnectToInputOutputStreams()
218249 _nodeProcess . BeginErrorReadLine ( ) ;
219250 }
220251
252+ private static bool IsDebuggerListeningMessage ( string message )
253+ {
254+ return message . StartsWith ( "Debugger listening on port " , StringComparison . OrdinalIgnoreCase ) ;
255+ }
256+
221257 private FileSystemWatcher BeginFileWatcher ( string rootDir )
222258 {
223259 if ( _watchFileExtensions == null || _watchFileExtensions . Length == 0 )
0 commit comments