@@ -174,6 +174,7 @@ public PowerShellContextService(
174
174
OmniSharp . Extensions . LanguageServer . Protocol . Server . ILanguageServerFacade languageServer ,
175
175
bool isPSReadLineEnabled )
176
176
{
177
+ logger . LogTrace ( "Instantiating PowerShellContextService and adding event handlers" ) ;
177
178
_languageServer = languageServer ;
178
179
this . logger = logger ;
179
180
this . isPSReadLineEnabled = isPSReadLineEnabled ;
@@ -212,9 +213,9 @@ public static PowerShellContextService Create(
212
213
hostUserInterface ,
213
214
logger ) ;
214
215
216
+ logger . LogTrace ( "Creating initial PowerShell runspace" ) ;
215
217
Runspace initialRunspace = PowerShellContextService . CreateRunspace ( psHost , hostStartupInfo . LanguageMode ) ;
216
218
powerShellContext . Initialize ( hostStartupInfo . ProfilePaths , initialRunspace , true , hostUserInterface ) ;
217
-
218
219
powerShellContext . ImportCommandsModuleAsync ( ) ;
219
220
220
221
// TODO: This can be moved to the point after the $psEditor object
@@ -325,6 +326,7 @@ public void Initialize(
325
326
IHostOutput consoleHost )
326
327
{
327
328
Validate . IsNotNull ( "initialRunspace" , initialRunspace ) ;
329
+ this . logger . LogTrace ( $ "Initializing PowerShell context with runspace { initialRunspace . Name } ") ;
328
330
329
331
this . ownsInitialRunspace = ownsInitialRunspace ;
330
332
this . SessionState = PowerShellContextState . NotStarted ;
@@ -351,11 +353,7 @@ public void Initialize(
351
353
this . CurrentRunspace = this . initialRunspace ;
352
354
353
355
// Write out the PowerShell version for tracking purposes
354
- this . logger . LogInformation (
355
- string . Format (
356
- "PowerShell runtime version: {0}, edition: {1}" ,
357
- this . LocalPowerShellVersion . Version ,
358
- this . LocalPowerShellVersion . Edition ) ) ;
356
+ this . logger . LogInformation ( $ "PowerShell Version: { this . LocalPowerShellVersion . Version } , Edition: { this . LocalPowerShellVersion . Edition } ") ;
359
357
360
358
Version powerShellVersion = this . LocalPowerShellVersion . Version ;
361
359
if ( powerShellVersion >= new Version ( 5 , 0 ) )
@@ -441,6 +439,8 @@ public void Initialize(
441
439
442
440
public Task ImportCommandsModuleAsync ( string path )
443
441
{
442
+ this . logger . LogTrace ( $ "Importing PowershellEditorServices commands from { path } ") ;
443
+
444
444
PSCommand importCommand = new PSCommand ( )
445
445
. AddCommand ( "Import-Module" )
446
446
. AddArgument ( path ) ;
@@ -463,6 +463,7 @@ private static bool CheckIfRunspaceNeedsEventHandlers(RunspaceDetails runspaceDe
463
463
464
464
private void ConfigureRunspace ( RunspaceDetails runspaceDetails )
465
465
{
466
+ this . logger . LogTrace ( "Configuring Runspace" ) ;
466
467
runspaceDetails . Runspace . StateChanged += this . HandleRunspaceStateChanged ;
467
468
if ( runspaceDetails . Runspace . Debugger != null )
468
469
{
@@ -475,6 +476,7 @@ private void ConfigureRunspace(RunspaceDetails runspaceDetails)
475
476
476
477
private void CleanupRunspace ( RunspaceDetails runspaceDetails )
477
478
{
479
+ this . logger . LogTrace ( "Cleaning Up Runspace" ) ;
478
480
runspaceDetails . Runspace . StateChanged -= this . HandleRunspaceStateChanged ;
479
481
if ( runspaceDetails . Runspace . Debugger != null )
480
482
{
@@ -596,6 +598,8 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
596
598
Validate . IsNotNull ( nameof ( psCommand ) , psCommand ) ;
597
599
Validate . IsNotNull ( nameof ( executionOptions ) , executionOptions ) ;
598
600
601
+ this . logger . LogTrace ( $ "Attempting to execute command(s): { GetStringForPSCommand ( psCommand ) } ") ;
602
+
599
603
// Add history to PSReadLine before cancelling, otherwise it will be restored as the
600
604
// cancelled prompt when it's called again.
601
605
if ( executionOptions . AddToHistory )
@@ -629,7 +633,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
629
633
this . ShouldExecuteWithEventing ( executionOptions ) ||
630
634
( PromptNest . IsRemote && executionOptions . IsReadLine ) ) )
631
635
{
632
- this . logger . LogTrace ( "Passing command execution to pipeline thread. " ) ;
636
+ this . logger . LogTrace ( "Passing command execution to pipeline thread" ) ;
633
637
634
638
if ( shouldCancelReadLine && PromptNest . IsReadLineBusy ( ) )
635
639
{
@@ -714,8 +718,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
714
718
}
715
719
catch ( Exception e )
716
720
{
717
- logger . LogError (
718
- "Exception occurred while executing debugger command:\r \n \r \n " + e . ToString ( ) ) ;
721
+ this . logger . LogException ( "Exception occurred while executing debugger command" , e ) ;
719
722
}
720
723
finally
721
724
{
@@ -736,11 +739,7 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
736
739
AddToHistory = executionOptions . AddToHistory
737
740
} ;
738
741
739
- this . logger . LogTrace (
740
- string . Format (
741
- "Attempting to execute command(s):\r \n \r \n {0}" ,
742
- GetStringForPSCommand ( psCommand ) ) ) ;
743
-
742
+ this . logger . LogTrace ( "Passing to PowerShell" ) ;
744
743
745
744
PowerShell shell = this . PromptNest . GetPowerShell ( executionOptions . IsReadLine ) ;
746
745
@@ -818,29 +817,23 @@ public async Task<IEnumerable<TResult>> ExecuteCommandAsync<TResult>(
818
817
}
819
818
else
820
819
{
821
- this . logger . LogTrace (
822
- "Execution completed successfully." ) ;
820
+ this . logger . LogTrace ( "Execution completed successfully" ) ;
823
821
}
824
822
}
825
823
}
826
824
catch ( PSRemotingDataStructureException e )
827
825
{
828
- this . logger . LogError (
829
- "Pipeline stopped while executing command:\r \n \r \n " + e . ToString ( ) ) ;
830
-
826
+ this . logger . LogHandledException ( "Pipeline stopped while executing command" , e ) ;
831
827
errorMessages ? . Append ( e . Message ) ;
832
828
}
833
829
catch ( PipelineStoppedException e )
834
830
{
835
- this . logger . LogError (
836
- "Pipeline stopped while executing command:\r \n \r \n " + e . ToString ( ) ) ;
837
-
831
+ this . logger . LogHandledException ( "Pipeline stopped while executing command" , e ) ;
838
832
errorMessages ? . Append ( e . Message ) ;
839
833
}
840
834
catch ( RuntimeException e )
841
835
{
842
- this . logger . LogWarning (
843
- "Runtime exception occurred while executing command:\r \n \r \n " + e . ToString ( ) ) ;
836
+ this . logger . LogHandledException ( "Runtime exception occurred while executing command" , e ) ;
844
837
845
838
hadErrors = true ;
846
839
errorMessages ? . Append ( e . Message ) ;
@@ -1078,8 +1071,7 @@ public async Task ExecuteScriptWithArgsAsync(string script, string arguments = n
1078
1071
}
1079
1072
catch ( System . Management . Automation . DriveNotFoundException e )
1080
1073
{
1081
- this . logger . LogError (
1082
- "Could not determine current filesystem location:\r \n \r \n " + e . ToString ( ) ) ;
1074
+ this . logger . LogHandledException ( "Could not determine current filesystem location" , e ) ;
1083
1075
}
1084
1076
1085
1077
var strBld = new StringBuilder ( ) ;
@@ -1250,9 +1242,7 @@ public void AbortExecution(bool shouldAbortDebugSession)
1250
1242
if ( this . SessionState == PowerShellContextState . Aborting
1251
1243
|| this . SessionState == PowerShellContextState . Disposed )
1252
1244
{
1253
- this . logger . LogTrace (
1254
- string . Format (
1255
- $ "Execution abort requested when already aborted (SessionState = { this . SessionState } )") ) ;
1245
+ this . logger . LogTrace ( $ "Execution abort requested when already aborted (SessionState = { this . SessionState } )") ;
1256
1246
return ;
1257
1247
}
1258
1248
@@ -1539,8 +1529,8 @@ private void CloseRunspace(RunspaceDetails runspaceDetails)
1539
1529
1540
1530
if ( exitException != null )
1541
1531
{
1542
- this . logger . LogError (
1543
- $ "Caught { exitException . GetType ( ) . Name } while exiting { runspaceDetails . Location } runspace: \r \n { exitException . ToString ( ) } " ) ;
1532
+ this . logger . LogHandledException (
1533
+ $ "Caught { exitException . GetType ( ) . Name } while exiting { runspaceDetails . Location } runspace" , exitException ) ;
1544
1534
}
1545
1535
}
1546
1536
}
@@ -1582,6 +1572,8 @@ internal bool IsCurrentRunspaceOutOfProcess()
1582
1572
/// </summary>
1583
1573
internal void EnterNestedPrompt ( )
1584
1574
{
1575
+ this . logger . LogTrace ( "Entering nested prompt" ) ;
1576
+
1585
1577
if ( this . IsCurrentRunspaceOutOfProcess ( ) )
1586
1578
{
1587
1579
throw new NotSupportedException ( ) ;
@@ -1884,8 +1876,15 @@ private void OnExecutionStatusChanged(
1884
1876
hadErrors ) ) ;
1885
1877
}
1886
1878
1879
+ /// <remarks>
1880
+ /// TODO: This should somehow check if the server has actually started because we are
1881
+ /// currently sending this notification before it has initialized, which is not allowed.
1882
+ /// This might be the cause of our deadlock!
1883
+ /// </remarks>
1887
1884
private void PowerShellContext_RunspaceChangedAsync ( object sender , RunspaceChangedEventArgs e )
1888
1885
{
1886
+ this . logger . LogTrace ( "Sending runspaceChanged notification" ) ;
1887
+
1889
1888
_languageServer ? . SendNotification (
1890
1889
"powerShell/runspaceChanged" ,
1891
1890
new MinifiedRunspaceDetails ( e . NewRunspace ) ) ;
@@ -1922,10 +1921,16 @@ public MinifiedRunspaceDetails(RunspaceDetails eventArgs)
1922
1921
/// <summary>
1923
1922
/// Event hook on the PowerShell context to listen for changes in script execution status
1924
1923
/// </summary>
1924
+ /// <remarks>
1925
+ /// TODO: This should somehow check if the server has actually started because we are
1926
+ /// currently sending this notification before it has initialized, which is not allowed.
1927
+ /// </remarks>
1925
1928
/// <param name="sender">the PowerShell context sending the execution event</param>
1926
1929
/// <param name="e">details of the execution status change</param>
1927
1930
private void PowerShellContext_ExecutionStatusChangedAsync ( object sender , ExecutionStatusChangedEventArgs e )
1928
1931
{
1932
+ this . logger . LogTrace ( "Sending executionStatusChanged notification" ) ;
1933
+
1929
1934
// The cancelling of the prompt (PSReadLine) causes an ExecutionStatus.Aborted to be sent after every
1930
1935
// actual execution (ExecutionStatus.Running) on the pipeline. We ignore that event since it's counterintuitive to
1931
1936
// the goal of this method which is to send updates when the pipeline is actually running something.
@@ -1945,10 +1950,7 @@ private void PowerShellContext_ExecutionStatusChangedAsync(object sender, Execut
1945
1950
1946
1951
private IEnumerable < TResult > ExecuteCommandInDebugger < TResult > ( PSCommand psCommand , bool sendOutputToHost )
1947
1952
{
1948
- this . logger . LogTrace (
1949
- string . Format (
1950
- "Attempting to execute command(s) in the debugger:\r \n \r \n {0}" ,
1951
- GetStringForPSCommand ( psCommand ) ) ) ;
1953
+ this . logger . LogTrace ( $ "Attempting to execute command(s)a in the debugger: { GetStringForPSCommand ( psCommand ) } ") ;
1952
1954
1953
1955
IEnumerable < TResult > output =
1954
1956
this . versionSpecificOperations . ExecuteCommandInDebugger < TResult > (
@@ -2144,6 +2146,8 @@ private static string GetStringForPSCommand(PSCommand psCommand)
2144
2146
2145
2147
private void SetExecutionPolicy ( )
2146
2148
{
2149
+ this . logger . LogTrace ( "Setting execution policy..." ) ;
2150
+
2147
2151
// We want to get the list hierarchy of execution policies
2148
2152
// Calling the cmdlet is the simplest way to do that
2149
2153
IReadOnlyList < PSObject > policies = this . powerShell
@@ -2188,7 +2192,7 @@ private void SetExecutionPolicy()
2188
2192
}
2189
2193
2190
2194
// Finally set the inherited execution policy
2191
- this . logger . LogTrace ( "Setting execution policy to {Policy}" , policyToSet ) ;
2195
+ this . logger . LogTrace ( $ "Setting execution policy to { policyToSet } " ) ;
2192
2196
try
2193
2197
{
2194
2198
this . powerShell
@@ -2200,7 +2204,8 @@ private void SetExecutionPolicy()
2200
2204
}
2201
2205
catch ( CmdletInvocationException e )
2202
2206
{
2203
- this . logger . LogError ( e , "Error occurred calling 'Set-ExecutionPolicy -Scope Process -ExecutionPolicy {Policy} -Force'" , policyToSet ) ;
2207
+ this . logger . LogHandledException (
2208
+ $ "Error occurred calling 'Set-ExecutionPolicy -Scope Process -ExecutionPolicy { policyToSet } -Force'", e ) ;
2204
2209
}
2205
2210
finally
2206
2211
{
@@ -2221,13 +2226,11 @@ private SessionDetails GetSessionDetails(Func<PSCommand, PSObject> invokeAction)
2221
2226
}
2222
2227
catch ( RuntimeException e )
2223
2228
{
2224
- this . logger . LogTrace (
2225
- "Runtime exception occurred while gathering runspace info:\r \n \r \n " + e . ToString ( ) ) ;
2229
+ this . logger . LogHandledException ( "Runtime exception occurred while gathering runspace info" , e ) ;
2226
2230
}
2227
2231
catch ( ArgumentNullException )
2228
2232
{
2229
- this . logger . LogError (
2230
- "Could not retrieve session details but no exception was thrown." ) ;
2233
+ this . logger . LogError ( "Could not retrieve session details but no exception was thrown." ) ;
2231
2234
}
2232
2235
2233
2236
// TODO: Return a harmless object if necessary
@@ -2320,9 +2323,7 @@ private void SetProfileVariableInCurrentRunspace(ProfilePathInfo profilePaths)
2320
2323
profilePaths . CurrentUserCurrentHost ) ) ;
2321
2324
2322
2325
this . logger . LogTrace (
2323
- string . Format (
2324
- "Setting $profile variable in runspace. Current user host profile path: {0}" ,
2325
- profilePaths . CurrentUserCurrentHost ) ) ;
2326
+ $ "Setting $profile variable in runspace. Current user host profile path: { profilePaths . CurrentUserCurrentHost } ") ;
2326
2327
2327
2328
// Set the variable in the runspace
2328
2329
this . powerShell . Commands . Clear ( ) ;
@@ -2545,6 +2546,7 @@ private void OnDebuggerStop(object sender, DebuggerStopEventArgs e)
2545
2546
else
2546
2547
{
2547
2548
// TODO: How to handle this?
2549
+ this . logger . LogError ( $ "Unhandled TaskIndex: { taskIndex } ") ;
2548
2550
}
2549
2551
}
2550
2552
0 commit comments