Skip to content
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

WIP: Look in all workspace folders for PSSA settings file #2167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -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))
Expand All @@ -332,6 +332,8 @@ private bool TryFindSettingsFile(out string settingsFilePath)
return false;
}

_logger.LogInformation($"Found PSSA settings file at '{settingsFilePath}'");

return true;
}

Expand Down
80 changes: 29 additions & 51 deletions src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,35 @@ public string GetRelativePath(ScriptFile scriptFile)
return fileUri.ToString();
}

/// <summary>
/// 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.
/// </summary>
/// <param name="filePath"></param>
/// <returns>Best possible path.</returns>
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;
}

/// <summary>
/// Enumerate all the PowerShell (ps1, psm1, psd1) files in the workspace in a recursive manner, using default values.
/// </summary>
Expand Down Expand Up @@ -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;
}

/// <summary>
/// Returns a normalized string for a given documentUri to be used as key name.
/// Case-sensitive uri on Linux and lowercase for other platforms.
Expand Down