Skip to content

Commit 3fdb5e1

Browse files
committed
Fix NullRefEx bug when accessing scriptFile.ReferencedFilesThis happens because the ScriptFile ctor does not initialize all itspublic props. I added initialization for the other public propsexcept for the ScriptAst prop. I don't see an empty Ast. Perhaps nullis OK for this prop?This address vscode-powershell bug https://github.com/PowerShell/vscode-powershell/issues/1675Also, for the 2.0.0 branch, we should see if we can use Array.Empty<>()for initialization. It isn't availble to net45x. :-(
1 parent 49cb3a3 commit 3fdb5e1

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/PowerShellEditorServices/Workspace/ScriptFile.cs

+16-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class ScriptFile
2626
"\n"
2727
};
2828

29-
private Token[] scriptTokens;
3029
private Version powerShellVersion;
3130

3231
#endregion
@@ -116,7 +115,8 @@ public ScriptBlockAst ScriptAst
116115
/// </summary>
117116
public Token[] ScriptTokens
118117
{
119-
get { return this.scriptTokens; }
118+
get;
119+
private set;
120120
}
121121

122122
/// <summary>
@@ -146,11 +146,16 @@ public ScriptFile(
146146
TextReader textReader,
147147
Version powerShellVersion)
148148
{
149+
this.powerShellVersion = powerShellVersion;
150+
149151
this.FilePath = filePath;
150152
this.ClientFilePath = clientFilePath;
151153
this.IsAnalysisEnabled = true;
152154
this.IsInMemory = Workspace.IsPathInMemory(filePath);
153-
this.powerShellVersion = powerShellVersion;
155+
this.ReferencedFiles = new string[0];
156+
this.SyntaxMarkers = new ScriptFileMarker[0];
157+
this.FileLines = new List<string>();
158+
this.ScriptTokens = new Token[0];
154159

155160
this.SetFileContents(textReader.ReadToEnd());
156161
}
@@ -600,6 +605,8 @@ private void ParseFileContents()
600605
try
601606
{
602607
#if PowerShellv5r2
608+
Token[] scriptTokens;
609+
603610
// This overload appeared with Windows 10 Update 1
604611
if (this.powerShellVersion.Major >= 5 &&
605612
this.powerShellVersion.Build >= 10586)
@@ -610,22 +617,24 @@ private void ParseFileContents()
610617
Parser.ParseInput(
611618
this.Contents,
612619
this.FilePath,
613-
out this.scriptTokens,
620+
out scriptTokens,
614621
out parseErrors);
615622
}
616623
else
617624
{
618625
this.ScriptAst =
619626
Parser.ParseInput(
620627
this.Contents,
621-
out this.scriptTokens,
628+
out scriptTokens,
622629
out parseErrors);
623630
}
631+
632+
this.ScriptTokens = scriptTokens;
624633
#else
625634
this.ScriptAst =
626635
Parser.ParseInput(
627636
this.Contents,
628-
out this.scriptTokens,
637+
out scriptTokens,
629638
out parseErrors);
630639
#endif
631640
}
@@ -638,7 +647,7 @@ private void ParseFileContents()
638647
ex.Message);
639648

640649
parseErrors = new[] { parseError };
641-
this.scriptTokens = new Token[0];
650+
this.ScriptTokens = new Token[0];
642651
this.ScriptAst = null;
643652
}
644653

0 commit comments

Comments
 (0)