From 00787434f741dfc93fdcd927361cda8471ffac48 Mon Sep 17 00:00:00 2001 From: Andy Jordan <2226434+andyleejordan@users.noreply.github.com> Date: Tue, 16 Jul 2024 16:11:30 -0700 Subject: [PATCH] WIP: Look in all workspace folders for PSSA settings file --- .../Services/Analysis/AnalysisService.cs | 4 +- .../Services/Workspace/WorkspaceService.cs | 80 +++++++------------ 2 files changed, 32 insertions(+), 52 deletions(-) diff --git a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs index f6b25c5f7..f346cb6c9 100644 --- a/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs +++ b/src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs @@ -322,7 +322,7 @@ private bool TryFindSettingsFile(out string settingsFilePath) return false; } - settingsFilePath = _workspaceService?.ResolveWorkspacePath(configuredPath); + settingsFilePath = _workspaceService?.FindFileInWorkspace(configuredPath); if (settingsFilePath is null || !File.Exists(settingsFilePath)) @@ -332,6 +332,8 @@ private bool TryFindSettingsFile(out string settingsFilePath) return false; } + _logger.LogInformation($"Found PSSA settings file at '{settingsFilePath}'"); + return true; } diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs index 941fcf736..efd7f82d7 100644 --- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs +++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs @@ -335,6 +335,35 @@ public string GetRelativePath(ScriptFile scriptFile) return fileUri.ToString(); } + /// + /// Finds a file in the first workspace folder where it exists, if possible. + /// Used as a backwards-compatible way to find files in the workspace. + /// + /// + /// Best possible path. + public string FindFileInWorkspace(string filePath) + { + // If the file path is already an absolute path, just return it. + if (Path.IsPathRooted(filePath)) + { + return filePath; + } + + // If the file path is relative, try to find it in the workspace folders. + foreach (WorkspaceFolder workspaceFolder in WorkspaceFolders) + { + string folderPath = workspaceFolder.Uri.GetFileSystemPath(); + string combinedPath = Path.Combine(folderPath, filePath); + if (File.Exists(combinedPath)) + { + return combinedPath; + } + } + + // If the file path is not found in the workspace folders, return the original path. + return filePath; + } + /// /// Enumerate all the PowerShell (ps1, psm1, psd1) files in the workspace in a recursive manner, using default values. /// @@ -409,57 +438,6 @@ internal static string ReadFileContents(DocumentUri uri) return reader.ReadToEnd(); } - internal string ResolveWorkspacePath(string path) => ResolveRelativeScriptPath(InitialWorkingDirectory, path); - - internal string ResolveRelativeScriptPath(string baseFilePath, string relativePath) - { - // TODO: Sometimes the `baseFilePath` (even when its `WorkspacePath`) is null. - string combinedPath = null; - Exception resolveException = null; - - try - { - // If the path is already absolute there's no need to resolve it relatively - // to the baseFilePath. - if (Path.IsPathRooted(relativePath)) - { - return relativePath; - } - - // Get the directory of the original script file, combine it - // with the given path and then resolve the absolute file path. - combinedPath = - Path.GetFullPath( - Path.Combine( - baseFilePath, - relativePath)); - } - catch (NotSupportedException e) - { - // Occurs if the path is incorrectly formatted for any reason. One - // instance where this occurred is when a user had curly double-quote - // characters in their source instead of normal double-quotes. - resolveException = e; - } - catch (ArgumentException e) - { - // Occurs if the path contains invalid characters, specifically those - // listed in System.IO.Path.InvalidPathChars. - resolveException = e; - } - - if (resolveException != null) - { - logger.LogError( - "Could not resolve relative script path\r\n" + - $" baseFilePath = {baseFilePath}\r\n " + - $" relativePath = {relativePath}\r\n\r\n" + - $"{resolveException}"); - } - - return combinedPath; - } - /// /// Returns a normalized string for a given documentUri to be used as key name. /// Case-sensitive uri on Linux and lowercase for other platforms.