Skip to content

Commit 6fdf93f

Browse files
Only call IsPathFullyQualified on Windows and apply code review feedback
1 parent 82e3cbe commit 6fdf93f

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultRazorProjectFileSystem.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Diagnostics;
77
using System.IO;
8+
using Microsoft.AspNetCore.Razor.Utilities;
89

910
namespace Microsoft.AspNetCore.Razor.Language;
1011

@@ -93,6 +94,23 @@ protected override string NormalizeAndEnsureValidPath(string path)
9394

9495
var normalizedPath = path.Replace('\\', '/');
9596

97+
// On Windows, check to see if this is a rooted file path. If it is, just return it.
98+
// This covers the following cases:
99+
//
100+
// 1. It is rooted within the project root. That's valid and we would have checked
101+
// specifically for that case below.
102+
// 2. It is rooted outside of the project root. That's invalid, and we don't want to
103+
// concatenate it with the project root. That would potentially produce an invalid
104+
// Windows path like 'C:/project/C:/other-project/some-file.cshtml'.
105+
//
106+
// Note that returning a path that is rooted outside of the project root will cause
107+
// the GetItem(...) method to throw, but it could be overridden by a descendant file
108+
// system.
109+
if (PlatformInformation.IsWindows && PathUtilities.IsPathFullyQualified(path))
110+
{
111+
return normalizedPath;
112+
}
113+
96114
// Check if the given path is an absolute path. It is absolute if...
97115
//
98116
// 1. It is a network share path and starts with a '//' (e.g. //server/some/network/folder) or...
@@ -103,14 +121,6 @@ protected override string NormalizeAndEnsureValidPath(string path)
103121
return normalizedPath;
104122
}
105123

106-
// This might be an absolute path rooted outside of the project root. In that case,
107-
// we just return it. This will mean that the GetItem(...) method will throw above,
108-
// but it could be overridden by a descendant file system.
109-
if (PathUtilities.IsPathFullyQualified(path))
110-
{
111-
return normalizedPath;
112-
}
113-
114124
// This is not an absolute path, so we combine it with Root to produce the final path.
115125

116126
// If the root doesn't end in a '/', and the path doesn't start with a '/', we'll need to add one.

src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/TestRazorProjectService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.AspNetCore.Razor.Language;
77
using Microsoft.AspNetCore.Razor.LanguageServer.Common;
88
using Microsoft.AspNetCore.Razor.Test.Common;
9+
using Microsoft.CodeAnalysis.Razor;
910
using Microsoft.CodeAnalysis.Razor.Logging;
1011
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1112
using Microsoft.CodeAnalysis.Razor.Serialization;
@@ -45,7 +46,7 @@ public async Task AddDocumentToPotentialProjectsAsync(string filePath, Cancellat
4546
var projectDirectory = FilePathNormalizer.GetNormalizedDirectoryName(projectSnapshot.FilePath);
4647
var normalizedFilePath = FilePathNormalizer.Normalize(filePath);
4748

48-
var targetPath = normalizedFilePath.StartsWith(projectDirectory)
49+
var targetPath = normalizedFilePath.StartsWith(projectDirectory, FilePathComparison.Instance)
4950
? normalizedFilePath[projectDirectory.Length..]
5051
: normalizedFilePath;
5152

0 commit comments

Comments
 (0)