Skip to content

Commit 0078743

Browse files
committed
WIP: Look in all workspace folders for PSSA settings file
1 parent 869c578 commit 0078743

File tree

2 files changed

+32
-52
lines changed

2 files changed

+32
-52
lines changed

src/PowerShellEditorServices/Services/Analysis/AnalysisService.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ private bool TryFindSettingsFile(out string settingsFilePath)
322322
return false;
323323
}
324324

325-
settingsFilePath = _workspaceService?.ResolveWorkspacePath(configuredPath);
325+
settingsFilePath = _workspaceService?.FindFileInWorkspace(configuredPath);
326326

327327
if (settingsFilePath is null
328328
|| !File.Exists(settingsFilePath))
@@ -332,6 +332,8 @@ private bool TryFindSettingsFile(out string settingsFilePath)
332332
return false;
333333
}
334334

335+
_logger.LogInformation($"Found PSSA settings file at '{settingsFilePath}'");
336+
335337
return true;
336338
}
337339

src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs

+29-51
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,35 @@ public string GetRelativePath(ScriptFile scriptFile)
335335
return fileUri.ToString();
336336
}
337337

338+
/// <summary>
339+
/// Finds a file in the first workspace folder where it exists, if possible.
340+
/// Used as a backwards-compatible way to find files in the workspace.
341+
/// </summary>
342+
/// <param name="filePath"></param>
343+
/// <returns>Best possible path.</returns>
344+
public string FindFileInWorkspace(string filePath)
345+
{
346+
// If the file path is already an absolute path, just return it.
347+
if (Path.IsPathRooted(filePath))
348+
{
349+
return filePath;
350+
}
351+
352+
// If the file path is relative, try to find it in the workspace folders.
353+
foreach (WorkspaceFolder workspaceFolder in WorkspaceFolders)
354+
{
355+
string folderPath = workspaceFolder.Uri.GetFileSystemPath();
356+
string combinedPath = Path.Combine(folderPath, filePath);
357+
if (File.Exists(combinedPath))
358+
{
359+
return combinedPath;
360+
}
361+
}
362+
363+
// If the file path is not found in the workspace folders, return the original path.
364+
return filePath;
365+
}
366+
338367
/// <summary>
339368
/// Enumerate all the PowerShell (ps1, psm1, psd1) files in the workspace in a recursive manner, using default values.
340369
/// </summary>
@@ -409,57 +438,6 @@ internal static string ReadFileContents(DocumentUri uri)
409438
return reader.ReadToEnd();
410439
}
411440

412-
internal string ResolveWorkspacePath(string path) => ResolveRelativeScriptPath(InitialWorkingDirectory, path);
413-
414-
internal string ResolveRelativeScriptPath(string baseFilePath, string relativePath)
415-
{
416-
// TODO: Sometimes the `baseFilePath` (even when its `WorkspacePath`) is null.
417-
string combinedPath = null;
418-
Exception resolveException = null;
419-
420-
try
421-
{
422-
// If the path is already absolute there's no need to resolve it relatively
423-
// to the baseFilePath.
424-
if (Path.IsPathRooted(relativePath))
425-
{
426-
return relativePath;
427-
}
428-
429-
// Get the directory of the original script file, combine it
430-
// with the given path and then resolve the absolute file path.
431-
combinedPath =
432-
Path.GetFullPath(
433-
Path.Combine(
434-
baseFilePath,
435-
relativePath));
436-
}
437-
catch (NotSupportedException e)
438-
{
439-
// Occurs if the path is incorrectly formatted for any reason. One
440-
// instance where this occurred is when a user had curly double-quote
441-
// characters in their source instead of normal double-quotes.
442-
resolveException = e;
443-
}
444-
catch (ArgumentException e)
445-
{
446-
// Occurs if the path contains invalid characters, specifically those
447-
// listed in System.IO.Path.InvalidPathChars.
448-
resolveException = e;
449-
}
450-
451-
if (resolveException != null)
452-
{
453-
logger.LogError(
454-
"Could not resolve relative script path\r\n" +
455-
$" baseFilePath = {baseFilePath}\r\n " +
456-
$" relativePath = {relativePath}\r\n\r\n" +
457-
$"{resolveException}");
458-
}
459-
460-
return combinedPath;
461-
}
462-
463441
/// <summary>
464442
/// Returns a normalized string for a given documentUri to be used as key name.
465443
/// Case-sensitive uri on Linux and lowercase for other platforms.

0 commit comments

Comments
 (0)