diff --git a/src/PowerShellEditorServices/Extensions/FileContext.cs b/src/PowerShellEditorServices/Extensions/FileContext.cs
index 770cc33f7..c8c32e58e 100644
--- a/src/PowerShellEditorServices/Extensions/FileContext.cs
+++ b/src/PowerShellEditorServices/Extensions/FileContext.cs
@@ -58,8 +58,7 @@ public sealed class FileContext
///
/// Gets the workspace-relative path of the file.
///
- public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(
- scriptFile.FilePath);
+ public string WorkspacePath => editorOperations.GetWorkspaceRelativePath(scriptFile);
#endregion
diff --git a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs
index 6f282eeea..f69fb4b2f 100644
--- a/src/PowerShellEditorServices/Extensions/IEditorOperations.cs
+++ b/src/PowerShellEditorServices/Extensions/IEditorOperations.cs
@@ -28,9 +28,8 @@ internal interface IEditorOperations
///
/// Resolves the given file path relative to the current workspace path.
///
- /// The file path to be resolved.
/// The resolved file path.
- string GetWorkspaceRelativePath(string filePath);
+ string GetWorkspaceRelativePath(ScriptFile scriptFile);
///
/// Causes a new untitled file to be created in the editor.
diff --git a/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs b/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs
index fa6da7f90..d7448ff1a 100644
--- a/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs
+++ b/src/PowerShellEditorServices/Services/Extension/EditorOperationsService.cs
@@ -195,7 +195,7 @@ public async Task SaveFileAsync(string currentPath, string newSavePath)
// workspace it's in.
public string GetWorkspacePath() => _workspaceService.InitialWorkingDirectory;
- public string GetWorkspaceRelativePath(string filePath) => _workspaceService.GetRelativePath(filePath);
+ public string GetWorkspaceRelativePath(ScriptFile scriptFile) => _workspaceService.GetRelativePath(scriptFile);
public async Task ShowInformationMessageAsync(string message)
{
diff --git a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs
index f705101f8..aaaa49309 100644
--- a/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs
+++ b/src/PowerShellEditorServices/Services/Workspace/WorkspaceService.cs
@@ -306,18 +306,23 @@ public void CloseFile(ScriptFile scriptFile)
///
/// Gets the workspace-relative path of the given file path.
///
- /// The original full file path.
/// A relative file path
- public string GetRelativePath(string filePath)
+ public string GetRelativePath(ScriptFile scriptFile)
{
- string resolvedPath = filePath;
+ string resolvedPath = scriptFile.FilePath;
- if (!IsPathInMemory(filePath) && !string.IsNullOrEmpty(InitialWorkingDirectory))
+ if (!scriptFile.IsInMemory)
{
- Uri workspaceUri = new(InitialWorkingDirectory);
- Uri fileUri = new(filePath);
-
- resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
+ Uri fileUri = scriptFile.DocumentUri.ToUri();
+ foreach (WorkspaceFolder workspaceFolder in WorkspaceFolders)
+ {
+ Uri workspaceUri = workspaceFolder.Uri.ToUri();
+ if (workspaceUri.IsBaseOf(fileUri))
+ {
+ resolvedPath = workspaceUri.MakeRelativeUri(fileUri).ToString();
+ break;
+ }
+ }
// Convert the directory separators if necessary
if (Path.DirectorySeparatorChar == '\\')