@@ -24,6 +24,15 @@ public class Workspace
2424
2525 #endregion
2626
27+ #region Properties
28+
29+ /// <summary>
30+ /// Gets or sets the root path of the workspace.
31+ /// </summary>
32+ public string WorkspacePath { get ; set ; }
33+
34+ #endregion
35+
2736 #region Public Methods
2837
2938 /// <summary>
@@ -155,15 +164,20 @@ private void RecursivelyFindReferences(
155164 ScriptFile scriptFile ,
156165 Dictionary < string , ScriptFile > referencedScriptFiles )
157166 {
167+ // Get the base path of the current script for use in resolving relative paths
168+ string baseFilePath =
169+ GetBaseFilePath (
170+ scriptFile . FilePath ) ;
171+
158172 ScriptFile referencedFile ;
159173 foreach ( string referencedFileName in scriptFile . ReferencedFiles )
160174 {
161175 string resolvedScriptPath =
162176 this . ResolveRelativeScriptPath (
163- scriptFile . FilePath ,
177+ baseFilePath ,
164178 referencedFileName ) ;
165179
166- // make sure file exists before trying to get the file
180+ // Make sure file exists before trying to get the file
167181 if ( File . Exists ( resolvedScriptPath ) )
168182 {
169183 // Get the referenced file if it's not already in referencedScriptFiles
@@ -183,33 +197,62 @@ private void RecursivelyFindReferences(
183197
184198 private string ResolveFilePath ( string filePath )
185199 {
186- if ( filePath . StartsWith ( @"file://" ) )
200+ if ( ! IsPathInMemory ( filePath ) )
187201 {
188- // Client sent the path in URI format, extract the local path and trim
189- // any extraneous slashes
190- Uri fileUri = new Uri ( filePath ) ;
191- filePath = fileUri . LocalPath . TrimStart ( '/' ) ;
192- }
202+ if ( filePath . StartsWith ( @"file://" ) )
203+ {
204+ // Client sent the path in URI format, extract the local path and trim
205+ // any extraneous slashes
206+ Uri fileUri = new Uri ( filePath ) ;
207+ filePath = fileUri . LocalPath . TrimStart ( '/' ) ;
208+ }
193209
194- // Some clients send paths with UNIX-style slashes, replace those if necessary
195- filePath = filePath . Replace ( '/' , '\\ ' ) ;
210+ // Some clients send paths with UNIX-style slashes, replace those if necessary
211+ filePath = filePath . Replace ( '/' , '\\ ' ) ;
212+
213+ // Get the absolute file path
214+ filePath = Path . GetFullPath ( filePath ) ;
215+ }
196216
197217 Logger . Write ( LogLevel . Verbose , "Resolved path: " + filePath ) ;
198218
199- return Path . GetFullPath ( filePath ) ;
219+ return filePath ;
200220 }
201221
202- private string ResolveRelativeScriptPath ( string originalScriptPath , string relativePath )
222+ internal static bool IsPathInMemory ( string filePath )
203223 {
204- if ( ! Path . IsPathRooted ( originalScriptPath ) )
224+ // When viewing PowerShell files in the Git diff viewer, VS Code
225+ // sends the contents of the file at HEAD with a URI that starts
226+ // with 'inmemory'. Untitled files which have been marked of
227+ // type PowerShell have a path starting with 'untitled'.
228+ return
229+ filePath . StartsWith ( "inmemory" ) ||
230+ filePath . StartsWith ( "untitled" ) ;
231+ }
232+
233+ private string GetBaseFilePath ( string filePath )
234+ {
235+ if ( IsPathInMemory ( filePath ) )
236+ {
237+ // If the file is in memory, use the workspace path
238+ return this . WorkspacePath ;
239+ }
240+
241+ if ( ! Path . IsPathRooted ( filePath ) )
205242 {
206243 // TODO: Assert instead?
207244 throw new InvalidOperationException (
208245 string . Format (
209246 "Must provide a full path for originalScriptPath: {0}" ,
210- originalScriptPath ) ) ;
247+ filePath ) ) ;
211248 }
212249
250+ // Get the directory of the file path
251+ return Path . GetDirectoryName ( filePath ) ;
252+ }
253+
254+ private string ResolveRelativeScriptPath ( string baseFilePath , string relativePath )
255+ {
213256 if ( Path . IsPathRooted ( relativePath ) )
214257 {
215258 return relativePath ;
@@ -220,7 +263,7 @@ private string ResolveRelativeScriptPath(string originalScriptPath, string relat
220263 string combinedPath =
221264 Path . GetFullPath (
222265 Path . Combine (
223- Path . GetDirectoryName ( originalScriptPath ) ,
266+ baseFilePath ,
224267 relativePath ) ) ;
225268
226269 return combinedPath ;
0 commit comments