Skip to content

Fix escaping of paths in editor config #198

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void GlobalConfigMapsPaths()
.Single();

var found = false;
var pattern = $"[{path}";
var pattern = $"[{RoslynUtil.EscapeEditorConfigSectionPath(path)}";
foreach (var line in File.ReadAllLines(configFilePath))
{
if (line.StartsWith(pattern, StringComparison.Ordinal))
Expand Down
92 changes: 71 additions & 21 deletions src/Basic.CompilerLog.UnitTests/RoslynUtilTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using Basic.CompilerLog.Util;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;
Expand Down Expand Up @@ -148,27 +149,76 @@ public void IsGlobalConfig(IsGlobalConfigData data)
[Fact]
public void RewriteGlobalEditorConfigPaths()
{
Core(
"""
is_global = true
[c:\example.cs]
""",
"""
is_global = true
[d:\example.cs]
""",
x => x.Replace("c:", "d:"));

Core(
"""
is_global = true
[c:\example.cs]
""",
"""
is_global = true
[c:\test.cs]
""",
x => x.Replace("example", "test"));
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Core(
"""
is_global = true
[c:/example.cs]
""",
"""
is_global = true
[d:/example.cs]
""",
x => x.Replace("c:", "d:"));

Core(
"""
is_global = true
[c:\example.cs]
""",
"""
is_global = true
[d:/example.cs]
""",
x => x.Replace("c:", "d:"));

Core(
"""
is_global = true
[c:\example.cs]
""",
"""
is_global = true
[c:/test.cs]
""",
x => x.Replace("example", "test"));

Core(
"""
is_global = true
[c:/example.cs]
""",
"""
is_global = true
[c:/test.cs]
""",
x => x.Replace("example", "test"));
}
else
{
Core(
"""
is_global = true
[/c/example.cs]
""",
"""
is_global = true
[/d/example.cs]
""",
x => x.Replace("/c", "/d"));

Core(
"""
is_global = true
[/c/example.cs]
""",
"""
is_global = true
[/c/test.cs]
""",
x => x.Replace("example", "test"));
}

void Core(string content, string expected, Func<string, string> mapFunc)
{
Expand Down
19 changes: 18 additions & 1 deletion src/Basic.CompilerLog.Util/RoslynUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
Expand Down Expand Up @@ -239,7 +240,7 @@
{
var mapped = pathMapFunc(line.Slice(1, index - 1).ToString());
builder.Append('[');
builder.Append(mapped);
builder.Append(EscapeEditorConfigSectionPath(mapped));
builder.Append(line.Slice(index));
}
}
Expand All @@ -255,6 +256,22 @@
return builder.ToString();
}

/// <summary>
/// The path used in a editor config section needs to be escaped on Windows.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
internal static string EscapeEditorConfigSectionPath(string path)
{
// The \ on windows need to be escaped when emitted as part of a section header
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
return path.Replace(@"\", @"/");

Check warning on line 269 in src/Basic.CompilerLog.Util/RoslynUtil.cs

View check run for this annotation

Codecov / codecov/patch

src/Basic.CompilerLog.Util/RoslynUtil.cs#L268-L269

Added lines #L268 - L269 were not covered by tests
}

return path;
}

/// <summary>
/// Open a file from a build on the current machine.
/// </summary>
Expand Down
17 changes: 16 additions & 1 deletion src/Scratch/Scratch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@

#pragma warning disable 8321

var export = @"C:\Users\jaredpar\Downloads\customer-bug\.complog";
if (Directory.Exists(export))
{
Directory.Delete(export, recursive: true);
}

var sdkDirs = SdkUtil.GetSdkDirectories();
using var reader = CompilerLogReader.Create(@"C:\Users\jaredpar\Downloads\customer-bug\msbuild.complog");
var exportUtil = new ExportUtil(reader, includeAnalyzers: true);
exportUtil.ExportAll(export, sdkDirs);



/*
using var reader = CompilerCallReaderUtil.Create("/home/jaredpar/code/msbuild/artifacts/log/Debug/Build.binlog", BasicAnalyzerKind.None);
var compilerCall = reader
.ReadAllCompilerCalls()
Expand All @@ -33,8 +47,9 @@
var compilation = data.GetCompilationAfterGenerators();
Console.WriteLine(compilation.AssemblyName);
var diagnostics = compilation.GetDiagnostics();
*/

Bing();
//Bing();
void Bing()
{
var filePath = @"C:\Users\jaredpar\code\snrcode\msbuild.binlog";
Expand Down
Loading