Skip to content

Commit

Permalink
Fix NullRefEx bug when accessing scriptFile.ReferencedFiles
Browse files Browse the repository at this point in the history
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 PowerShell/vscode-powershell#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.  :-(
  • Loading branch information
rkeithhill committed Dec 28, 2018
1 parent 49cb3a3 commit d4076b8
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/PowerShellEditorServices/Workspace/ScriptFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class ScriptFile
"\n"
};

private Token[] scriptTokens;
private Version powerShellVersion;

#endregion
Expand Down Expand Up @@ -116,7 +115,8 @@ public ScriptBlockAst ScriptAst
/// </summary>
public Token[] ScriptTokens
{
get { return this.scriptTokens; }
get;
private set;
}

/// <summary>
Expand Down Expand Up @@ -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<string>();
this.ScriptTokens = new Token[0];

this.SetFileContents(textReader.ReadToEnd());
}
Expand Down Expand Up @@ -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)
Expand All @@ -610,17 +617,19 @@ private void ParseFileContents()
Parser.ParseInput(
this.Contents,
this.FilePath,
out this.scriptTokens,
out scriptTokens,
out parseErrors);
}
else
{
this.ScriptAst =
Parser.ParseInput(
this.Contents,
out this.scriptTokens,
out scriptTokens,
out parseErrors);
}

this.ScriptTokens = scriptTokens;
#else
this.ScriptAst =
Parser.ParseInput(
Expand All @@ -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;
}

Expand Down

0 comments on commit d4076b8

Please sign in to comment.