@@ -24,6 +24,15 @@ public class Workspace
24
24
25
25
#endregion
26
26
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
+
27
36
#region Public Methods
28
37
29
38
/// <summary>
@@ -155,15 +164,20 @@ private void RecursivelyFindReferences(
155
164
ScriptFile scriptFile ,
156
165
Dictionary < string , ScriptFile > referencedScriptFiles )
157
166
{
167
+ // Get the base path of the current script for use in resolving relative paths
168
+ string baseFilePath =
169
+ GetBaseFilePath (
170
+ scriptFile . FilePath ) ;
171
+
158
172
ScriptFile referencedFile ;
159
173
foreach ( string referencedFileName in scriptFile . ReferencedFiles )
160
174
{
161
175
string resolvedScriptPath =
162
176
this . ResolveRelativeScriptPath (
163
- scriptFile . FilePath ,
177
+ baseFilePath ,
164
178
referencedFileName ) ;
165
179
166
- // make sure file exists before trying to get the file
180
+ // Make sure file exists before trying to get the file
167
181
if ( File . Exists ( resolvedScriptPath ) )
168
182
{
169
183
// Get the referenced file if it's not already in referencedScriptFiles
@@ -183,33 +197,62 @@ private void RecursivelyFindReferences(
183
197
184
198
private string ResolveFilePath ( string filePath )
185
199
{
186
- if ( filePath . StartsWith ( @"file://" ) )
200
+ if ( ! IsPathInMemory ( filePath ) )
187
201
{
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
+ }
193
209
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
+ }
196
216
197
217
Logger . Write ( LogLevel . Verbose , "Resolved path: " + filePath ) ;
198
218
199
- return Path . GetFullPath ( filePath ) ;
219
+ return filePath ;
200
220
}
201
221
202
- private string ResolveRelativeScriptPath ( string originalScriptPath , string relativePath )
222
+ internal static bool IsPathInMemory ( string filePath )
203
223
{
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 ) )
205
242
{
206
243
// TODO: Assert instead?
207
244
throw new InvalidOperationException (
208
245
string . Format (
209
246
"Must provide a full path for originalScriptPath: {0}" ,
210
- originalScriptPath ) ) ;
247
+ filePath ) ) ;
211
248
}
212
249
250
+ // Get the directory of the file path
251
+ return Path . GetDirectoryName ( filePath ) ;
252
+ }
253
+
254
+ private string ResolveRelativeScriptPath ( string baseFilePath , string relativePath )
255
+ {
213
256
if ( Path . IsPathRooted ( relativePath ) )
214
257
{
215
258
return relativePath ;
@@ -220,7 +263,7 @@ private string ResolveRelativeScriptPath(string originalScriptPath, string relat
220
263
string combinedPath =
221
264
Path . GetFullPath (
222
265
Path . Combine (
223
- Path . GetDirectoryName ( originalScriptPath ) ,
266
+ baseFilePath ,
224
267
relativePath ) ) ;
225
268
226
269
return combinedPath ;
0 commit comments