|
3 | 3 | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
4 | 4 | // |
5 | 5 |
|
6 | | -using System.IO; |
7 | 6 | using Microsoft.PowerShell.EditorServices.Utility; |
8 | 7 | using System; |
9 | | -using System.Reflection; |
10 | 8 | using System.Collections; |
11 | | -using System.Linq; |
12 | | -using System.Collections.Generic; |
| 9 | +using System.IO; |
| 10 | +using System.Reflection; |
| 11 | +using System.Security; |
13 | 12 |
|
14 | 13 | namespace Microsoft.PowerShell.EditorServices.Protocol.Server |
15 | 14 | { |
@@ -66,31 +65,47 @@ public void Update( |
66 | 65 |
|
67 | 66 | string settingsPath = settings.SettingsPath; |
68 | 67 |
|
69 | | - if (string.IsNullOrWhiteSpace(settingsPath)) |
| 68 | + try |
70 | 69 | { |
71 | | - settingsPath = null; |
72 | | - } |
73 | | - else if (!Path.IsPathRooted(settingsPath)) |
74 | | - { |
75 | | - if (string.IsNullOrEmpty(workspaceRootPath)) |
| 70 | + if (string.IsNullOrWhiteSpace(settingsPath)) |
76 | 71 | { |
77 | | - // The workspace root path could be an empty string |
78 | | - // when the user has opened a PowerShell script file |
79 | | - // without opening an entire folder (workspace) first. |
80 | | - // In this case we should just log an error and let |
81 | | - // the specified settings path go through even though |
82 | | - // it will fail to load. |
83 | | - logger.Write( |
84 | | - LogLevel.Error, |
85 | | - "Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath."); |
| 72 | + settingsPath = null; |
86 | 73 | } |
87 | | - else |
| 74 | + else if (!Path.IsPathRooted(settingsPath)) |
88 | 75 | { |
89 | | - settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath)); |
| 76 | + if (string.IsNullOrEmpty(workspaceRootPath)) |
| 77 | + { |
| 78 | + // The workspace root path could be an empty string |
| 79 | + // when the user has opened a PowerShell script file |
| 80 | + // without opening an entire folder (workspace) first. |
| 81 | + // In this case we should just log an error and let |
| 82 | + // the specified settings path go through even though |
| 83 | + // it will fail to load. |
| 84 | + logger.Write( |
| 85 | + LogLevel.Error, |
| 86 | + "Could not resolve Script Analyzer settings path due to null or empty workspaceRootPath."); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + settingsPath = Path.GetFullPath(Path.Combine(workspaceRootPath, settingsPath)); |
| 91 | + } |
90 | 92 | } |
| 93 | + |
| 94 | + this.SettingsPath = settingsPath; |
| 95 | + logger.Write(LogLevel.Verbose, $"Using Script Analyzer settings path - '{settingsPath ?? ""}'."); |
91 | 96 | } |
| 97 | + catch (Exception ex) when ( |
| 98 | + ex is NotSupportedException || |
| 99 | + ex is PathTooLongException || |
| 100 | + ex is SecurityException) |
| 101 | + { |
| 102 | + // Invalid chars in path like ${env:HOME} can cause Path.GetFullPath() to throw, catch such errors here |
| 103 | + logger.WriteException( |
| 104 | + $"Invalid Script Analyzer settings path - '{settingsPath}'.", |
| 105 | + ex); |
92 | 106 |
|
93 | | - this.SettingsPath = settingsPath; |
| 107 | + this.SettingsPath = null; |
| 108 | + } |
94 | 109 | } |
95 | 110 | } |
96 | 111 | } |
|
0 commit comments