Skip to content

Commit

Permalink
Handle different exceptions that can occur and cause VS crashes (#6604)
Browse files Browse the repository at this point in the history
* Handle different exceptions that can occur

* Review feedback, let other package managers have a chance
  • Loading branch information
rchiodo authored Jul 13, 2021
1 parent f08d6e1 commit 7cf41e1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,29 @@ private void ShowInfoBar(string infoBarMessage, List<InfoBarHyperlink> acceptAct

private async Task<bool> InstallPyTestAsync() {
var packageManagers = _infoBarData.InterpreterOptionsService.GetPackageManagers(_infoBarData.InterpreterFactory);
string failureMessage = null;

foreach (var packageManager in packageManagers) {
bool pytestInstalled = await packageManager.InstallAsync(
new PackageSpec("pytest"),
new VsPackageManagerUI(Site),
CancellationToken.None
);

if (pytestInstalled) {
return true;
try {
bool pytestInstalled = await packageManager.InstallAsync(
new PackageSpec("pytest"),
new VsPackageManagerUI(Site),
CancellationToken.None
);

if (pytestInstalled) {
return true;
}
} catch (InvalidOperationException e) {
failureMessage = e.Message;
}
}

if (!String.IsNullOrEmpty(failureMessage)) {
// Something couldn't be installed.
Create(new InfoBarModel(failureMessage));
}

return false;
}

Expand Down
2 changes: 1 addition & 1 deletion Python/Product/PythonTools/PythonToolsService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ internal Dictionary<string, string> GetFullEnvironment(LaunchConfiguration confi
=> LaunchConfigurationUtils.GetFullEnvironment(config, _container, UIThread);

internal IEnumerable<string> GetGlobalPythonSearchPaths(InterpreterConfiguration interpreter) {
if (!GeneralOptions.ClearGlobalPythonPath) {
if (!GeneralOptions.ClearGlobalPythonPath && interpreter != null) {
string pythonPath = Environment.GetEnvironmentVariable(interpreter.PathEnvironmentVariable) ?? string.Empty;
return pythonPath
.Split(Path.PathSeparator)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,16 +205,15 @@ public IEnumerable<string> EnumerateUserFiles(Predicate<string> predicate) {
.Where(x => !string.IsNullOrEmpty(x.InterpreterPath))
.Where(x => PathUtils.IsSubpathOf(_workspace.Location, x.InterpreterPath))
.ToList();
foreach (var file in Directory.EnumerateFiles(_workspace.Location).Where(x => predicate(x))) {
foreach (var file in EnumerateFilesSafe(_workspace.Location).Where(x => predicate(x))) {
yield return file;
}

foreach (var topLevelDirectory in Directory.EnumerateDirectories(_workspace.Location)) {
foreach (var topLevelDirectory in EnumerateDirectoriesSafe(_workspace.Location)) {
if (!workspaceInterpreterConfigs.Any(x => PathUtils.IsSameDirectory(x.GetPrefixPath(), topLevelDirectory)) &&
!PathUtils.IsSameDirectory(topLevelDirectory, workspaceCacheDirPath)
) {
foreach (var file in Directory
.EnumerateFiles(topLevelDirectory, "*", SearchOption.AllDirectories)
foreach (var file in EnumerateFilesSafe(topLevelDirectory, "*", SearchOption.AllDirectories)
.Where(x => predicate(x))
) {
yield return file;
Expand Down Expand Up @@ -263,6 +262,30 @@ private void RefreshCurrentFactory() {
}
}

private IEnumerable<string> EnumerateDirectoriesSafe(string location) {
try {
return EnumerateDirectoriesSafe(location);
} catch (SystemException) {
return Enumerable.Empty<string>();
}
}

private IEnumerable<string> EnumerateFilesSafe(string location) {
try {
return EnumerateFilesSafe(location);
} catch (SystemException) {
return Enumerable.Empty<string>();
}
}

private IEnumerable<string> EnumerateFilesSafe(string location, string pattern, SearchOption option) {
try {
return EnumerateFilesSafe(location, pattern, option);
} catch (SystemException) {
return Enumerable.Empty<string>();
}
}

private static IPythonInterpreterFactory GetFactory(string interpreter, IWorkspace workspace, IInterpreterRegistryService registryService) {
IPythonInterpreterFactory factory = null;
if (interpreter != null && registryService != null) {
Expand Down

0 comments on commit 7cf41e1

Please sign in to comment.