Skip to content

Commit

Permalink
Reduce noisy errors when viewing git diff on VS code (#9407)
Browse files Browse the repository at this point in the history
* Reduce noise when viewing git diff on VS code

- The language server project was return a wrong file path for uris with git scheme
- e.g. this could happen when opening git diff on VS code

- With this hacky fix, we make up a fake git folder to provide for these file paths
- This makes sure the returned file path for the left hand side of the git diff view,
 would not collide with existing file paths (original file path for the right hand side)

Fixes #9365

* Apply PR feedback and add tests

* Minor fix
  • Loading branch information
maryamariyan authored Oct 13, 2023
1 parent 574f167 commit 70de468
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Net;
using Microsoft.AspNetCore.Razor.Utilities;

namespace Microsoft.CodeAnalysis.Razor;

Expand All @@ -22,6 +23,27 @@ public static string GetAbsoluteOrUNCPath(this Uri uri)
}

// Absolute paths are usually encoded.
return uri.AbsolutePath.Contains("%") ? WebUtility.UrlDecode(uri.AbsolutePath) : uri.AbsolutePath;
var absolutePath = uri.AbsolutePath.Contains("%") ? WebUtility.UrlDecode(uri.AbsolutePath) : uri.AbsolutePath;

if (string.Equals(uri.Scheme, "git", StringComparison.OrdinalIgnoreCase))
{
// return a normalized path when we want to add to a fake git directory path (hacky fix for https://github.com/dotnet/razor/issues/9365)
return AddToFakeGitDirectoryAtRoot(absolutePath);
}

return absolutePath;
}

private static string AddToFakeGitDirectoryAtRoot(string decodedAbsolutePath)
{
var normalizedPath = FilePathNormalizer.Normalize(decodedAbsolutePath);
var firstSeparatorIndex = normalizedPath.IndexOf('/');
if (firstSeparatorIndex < 0)
{
// no-op
return decodedAbsolutePath;
}

return normalizedPath.Insert(firstSeparatorIndex + 1, "_git_/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,41 @@ public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesSpacePaths()
Assert.Equal("c:/Some/path/to/file path.cshtml", path);
}

[OSSkipConditionTheory(new[] { "OSX", "Linux" }), WorkItem("https://github.com/dotnet/razor/issues/9365")]
[InlineData(@"git:/c%3A/path/to/dir/Index.cshtml", @"c:/_git_/path/to/dir/Index.cshtml")]
[InlineData(@"git:/c:/path%2Fto/dir/Index.cshtml?%7B%22p", @"c:/_git_/path/to/dir/Index.cshtml")]
[InlineData(@"git:/c:/path/to/dir/Index.cshtml", @"c:/_git_/path/to/dir/Index.cshtml")]
public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesGitScheme(string filePath, string expected)
{
// Arrange
var uri = new Uri(filePath);

// Act
var path = uri.GetAbsoluteOrUNCPath();

// Assert
Assert.Equal(expected, path);
}

[OSSkipConditionTheory(new[] { "OSX", "Linux" })]
[InlineData(@"file:///c:/path/to/dir/Index.cshtml", @"c:/path/to/dir/Index.cshtml")]
[InlineData(@"file:///c:\path/to\dir/Index.cshtml", @"c:/path/to/dir/Index.cshtml")]
[InlineData(@"file:///C:\path\to\dir\Index.cshtml", @"C:/path/to/dir/Index.cshtml")]
[InlineData(@"file:///C:\PATH\TO\DIR\Index.cshtml", @"C:/PATH/TO/DIR/Index.cshtml")]
[InlineData(@"file:\\path\to\dir\Index.cshtml", @"\\path\to\dir\Index.cshtml")]
[InlineData("file:///path/to/dir/Index.cshtml", @"/path/to/dir/Index.cshtml")]
public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesFileScheme(string filePath, string expected)
{
// Arrange
var uri = new Uri(filePath);

// Act
var path = uri.GetAbsoluteOrUNCPath();

// Assert
Assert.Equal(expected, path);
}

[Fact]
public void GetAbsoluteOrUNCPath_UNCPath_ReturnsLocalPath()
{
Expand Down Expand Up @@ -93,4 +128,21 @@ public void GetAbsoluteOrUNCPath_UNCPath_HandlesSpacePaths()
// Assert
Assert.Equal(@"\\some\path\to\file path.cshtml", path);
}

[OSSkipConditionTheory(new[] { "Windows" }), WorkItem("https://github.com/dotnet/razor/issues/9365")]
[InlineData("git:///path/to/dir/Index.cshtml", "/_git_/path/to/dir/Index.cshtml")]
[InlineData("git:///path%2Fto/dir/Index.cshtml", "/_git_/path/to/dir/Index.cshtml")]
[InlineData("file:///path/to/dir/Index.cshtml", @"/path/to/dir/Index.cshtml")]
[InlineData("file:///path%2Fto/dir/Index.cshtml", @"/path/to/dir/Index.cshtml")]
public void GetAbsoluteOrUNCPath_AbsolutePath_HandlesSchemeProperly(string filePath, string expected)
{
// Arrange
var uri = new Uri(filePath);

// Act
var path = uri.GetAbsoluteOrUNCPath();

// Assert
Assert.Equal(expected, path);
}
}

0 comments on commit 70de468

Please sign in to comment.