Skip to content

Fix bad pssa settings path crashes PSES #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,42 @@ public void Update(

string settingsPath = settings.SettingsPath;

if (string.IsNullOrWhiteSpace(settingsPath))
try
{
settingsPath = null;
}
else if (!Path.IsPathRooted(settingsPath))
{
if (string.IsNullOrEmpty(workspaceRootPath))
if (string.IsNullOrWhiteSpace(settingsPath))
{
// The workspace root path could be an empty string
// when the user has opened a PowerShell script file
// without opening an entire folder (workspace) first.
// In this case we should just log an error and let
// the specified settings path go through even though
// it will fail to load.
logger.Write(
LogLevel.Error,
"Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
settingsPath = null;
}
else
else if (!Path.IsPathRooted(settingsPath))
{
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
if (string.IsNullOrEmpty(workspaceRootPath))
{
// The workspace root path could be an empty string
// when the user has opened a PowerShell script file
// without opening an entire folder (workspace) first.
// In this case we should just log an error and let
// the specified settings path go through even though
// it will fail to load.
logger.Write(
LogLevel.Error,
"Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath.");
}
else
{
settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
}
}
}
catch (Exception ex) when (ex is NotSupportedException)
{
// Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here
logger.WriteException(
$"Invalid Script Analyzer settings path - '{settingsPath}'.",
ex);
}

this.SettingsPath = settingsPath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want to set this.SettingsPath to settingsPath even if settingsPath is an Invalid Script Analyzer settings path?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doh! No. Thanks for catching this. I'll fix it later tonight.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'.");
}
}
}
Expand Down