From d4076b8a1caf52214d624daab952b2e099f34ec7 Mon Sep 17 00:00:00 2001 From: Keith Hill Date: Fri, 28 Dec 2018 12:39:36 -0700 Subject: [PATCH] Fix NullRefEx bug when accessing scriptFile.ReferencedFiles This happens because the ScriptFile ctor does not initialize all its public props. I added initialization for the other public props except for the ScriptAst prop. I don't see an empty Ast. Perhaps null is OK for this prop? This address vscode-powershell bug https://github.com/PowerShell/vscode-powershell/issues/1675 Also, for the 2.0.0 branch, we should see if we can use Array.Empty<>() for initialization. It isn't availble to net45x. :-( --- .../Workspace/ScriptFile.cs | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/PowerShellEditorServices/Workspace/ScriptFile.cs b/src/PowerShellEditorServices/Workspace/ScriptFile.cs index dab2918ccc..d36fb2669e 100644 --- a/src/PowerShellEditorServices/Workspace/ScriptFile.cs +++ b/src/PowerShellEditorServices/Workspace/ScriptFile.cs @@ -26,7 +26,6 @@ public class ScriptFile "\n" }; - private Token[] scriptTokens; private Version powerShellVersion; #endregion @@ -116,7 +115,8 @@ public ScriptBlockAst ScriptAst /// public Token[] ScriptTokens { - get { return this.scriptTokens; } + get; + private set; } /// @@ -146,11 +146,16 @@ public ScriptFile( TextReader textReader, Version powerShellVersion) { + this.powerShellVersion = powerShellVersion; + this.FilePath = filePath; this.ClientFilePath = clientFilePath; this.IsAnalysisEnabled = true; this.IsInMemory = Workspace.IsPathInMemory(filePath); - this.powerShellVersion = powerShellVersion; + this.ReferencedFiles = new string[0]; + this.SyntaxMarkers = new ScriptFileMarker[0]; + this.FileLines = new List(); + this.ScriptTokens = new Token[0]; this.SetFileContents(textReader.ReadToEnd()); } @@ -600,6 +605,8 @@ private void ParseFileContents() try { #if PowerShellv5r2 + Token[] scriptTokens; + // This overload appeared with Windows 10 Update 1 if (this.powerShellVersion.Major >= 5 && this.powerShellVersion.Build >= 10586) @@ -610,7 +617,7 @@ private void ParseFileContents() Parser.ParseInput( this.Contents, this.FilePath, - out this.scriptTokens, + out scriptTokens, out parseErrors); } else @@ -618,9 +625,11 @@ private void ParseFileContents() this.ScriptAst = Parser.ParseInput( this.Contents, - out this.scriptTokens, + out scriptTokens, out parseErrors); } + + this.ScriptTokens = scriptTokens; #else this.ScriptAst = Parser.ParseInput( @@ -638,7 +647,7 @@ private void ParseFileContents() ex.Message); parseErrors = new[] { parseError }; - this.scriptTokens = new Token[0]; + this.ScriptTokens = new Token[0]; this.ScriptAst = null; }