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

Conversation

rkeithhill
Copy link
Contributor

Crash happens if user tries to set path to something like ${env:HOME}\pssettings.psd1 which looks like it might be valid but isn't because the variable doesn't get expanded.

It seems the GH diff view has gotten dumber or something. This is the diff I see in VS:

image

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.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

Looks good. The only optional suggestion I would make would be to try to make the try/catch block smaller.

The comment in the catch block says that GetFullPath() throws so I wonder if we could just wrap that line in a try/catch:

try {
    settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath));
} catch (Exception ex) when (ex is NotSupportedException) {
    logger.WriteException( $"Invalid Script Analyzer settings path - '{settingsPath}'.", ex);
    settingsPath = null;
}

A smaller try/catch helps to understand what is actually throwing the errors we are catching IMO.

@rkeithhill
Copy link
Contributor Author

A smaller try/catch helps to understand what is actually throwing the errors we are catching IMO

While that is true, my concern is that any future changes to this method skip providing such error recovery. By having an encompassing try/catch we "handle" PSSA settings path errors by logging an error instead of crashing the lang server. The line we have to walk is to not hide "developer" errors which is why I don't like a naked catch (Exception ex) in these cases and prefer to use a filter. In fact, I just noticed I'm missing PathTooLongException. I'll add that and update the PR.

FWIW I fixed a number of these bugs lately (particularly PathTooLongExc) where either a user supplied setting value OR an environmental factor like a path that is too long where the fix is to do just this - catch these exceptions (where we can't do anything useful but shouldn't crash - if possible).

@TylerLeonhardt
Copy link
Member

Fair point! I agree with you, then. I sign off. 👍

@rkeithhill rkeithhill merged commit 2464e93 into PowerShell:master Jan 1, 2018
@rkeithhill rkeithhill deleted the rkeithhill/fix-bad-pssa-settings-path branch January 1, 2018 01:17
SeeminglyScience pushed a commit to SeeminglyScience/PowerShellEditorServices that referenced this pull request Feb 1, 2018
* Fix PSES crash when pssa settings path has invalid chars

* Alway log (verbose) the pssa settings path

* Set SettingsPath to null in catch handler

* GetFullPath can also throw PathTooLongExc and SecurityExc
TylerLeonhardt pushed a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Feb 26, 2019
This change adds a new setting that controls whether focus changes to the
integrated console when scripts are executed or debugged:

- `powershell.integratedConsole.focusConsoleOnExecute`

When true, it causes the focus to get set to the console on any script
execution.  It is set to true by default to ensure that screen readers
pick up the execution output once it starts.

Resolves PowerShell#588.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants