Skip to content

Commit 3308878

Browse files
committed
Fix #287: Handle invalid path chars when resolving script references
This change resolves an issue that appears when the user edits a script that has dot-sourced script references which contain invalid characters. The fix handles the exceptions that might be thrown in this case and writes out the path details to the log file. Related to PowerShell/vscode-powershell#274.
1 parent f08ad85 commit 3308878

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/PowerShellEditorServices/Language/FindDotSourcedVisitor.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
3434
{
3535
if (commandAst.InvocationOperator.Equals(TokenKind.Dot))
3636
{
37-
string fileName = commandAst.CommandElements[0].Extent.Text;
37+
// Strip any quote characters off of the string
38+
string fileName = commandAst.CommandElements[0].Extent.Text.Trim('\'', '"');
3839
DotSourcedFiles.Add(fileName);
3940
}
41+
4042
return base.VisitCommand(commandAst);
4143
}
4244
}

src/PowerShellEditorServices/Workspace/Workspace.cs

+46-9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ private void RecursivelyFindReferences(
203203
baseFilePath,
204204
referencedFileName);
205205

206+
// If there was an error resolving the string, skip this reference
207+
if (resolvedScriptPath == null)
208+
{
209+
continue;
210+
}
211+
206212
Logger.Write(
207213
LogLevel.Verbose,
208214
string.Format(
@@ -287,18 +293,49 @@ private string GetBaseFilePath(string filePath)
287293

288294
private string ResolveRelativeScriptPath(string baseFilePath, string relativePath)
289295
{
290-
if (Path.IsPathRooted(relativePath))
296+
string combinedPath = null;
297+
Exception resolveException = null;
298+
299+
try
291300
{
292-
return relativePath;
301+
// If the path is already absolute there's no need to resolve it relatively
302+
// to the baseFilePath.
303+
if (Path.IsPathRooted(relativePath))
304+
{
305+
return relativePath;
306+
}
307+
308+
// Get the directory of the original script file, combine it
309+
// with the given path and then resolve the absolute file path.
310+
combinedPath =
311+
Path.GetFullPath(
312+
Path.Combine(
313+
baseFilePath,
314+
relativePath));
315+
}
316+
catch (NotSupportedException e)
317+
{
318+
// Occurs if the path is incorrectly formatted for any reason. One
319+
// instance where this occurred is when a user had curly double-quote
320+
// characters in their source instead of normal double-quotes.
321+
resolveException = e;
322+
}
323+
catch (ArgumentException e)
324+
{
325+
// Occurs if the path contains invalid characters, specifically those
326+
// listed in System.IO.Path.InvalidPathChars.
327+
resolveException = e;
293328
}
294329

295-
// Get the directory of the original script file, combine it
296-
// with the given path and then resolve the absolute file path.
297-
string combinedPath =
298-
Path.GetFullPath(
299-
Path.Combine(
300-
baseFilePath,
301-
relativePath));
330+
if (resolveException != null)
331+
{
332+
Logger.Write(
333+
LogLevel.Error,
334+
$"Could not resolve relative script path\r\n" +
335+
$" baseFilePath = {baseFilePath}\r\n " +
336+
$" relativePath = {relativePath}\r\n\r\n" +
337+
$"{resolveException.ToString()}");
338+
}
302339

303340
return combinedPath;
304341
}

0 commit comments

Comments
 (0)