Skip to content

Commit

Permalink
Fix a few issues in export (#48)
Browse files Browse the repository at this point in the history
* Fix ruleset exporting

The export process rewrote the /ruleset option but kept the original as
well

* Fixed issues with output directories
  • Loading branch information
jaredpar authored Aug 2, 2023
1 parent 5aee6c6 commit 89f5720
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
55 changes: 55 additions & 0 deletions src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
Expand Down Expand Up @@ -190,6 +191,60 @@ class C { }
TestExport(1);
}

[Fact]
public void ConsoleWithRuleset()
{
RunDotNet($"new console --name example --output .");
File.WriteAllText(Path.Combine(RootDirectory, "example.csproj"),
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<CodeAnalysisRuleset>example.ruleset</CodeAnalysisRuleset>
</PropertyGroup>
</Project>
""");
File.WriteAllText(Path.Combine(RootDirectory, "example.ruleset"), """
<RuleSet Name="Rules for Hello World project" Description="These rules focus on critical issues for the Hello World app." ToolsVersion="10.0">
<Localization ResourceAssembly="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.dll" ResourceBaseName="Microsoft.VisualStudio.CodeAnalysis.RuleSets.Strings.Localized">
<Name Resource="HelloWorldRules_Name" />
<Description Resource="HelloWorldRules_Description" />
</Localization>
<Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
<Rule Id="CA1001" Action="Warning" />
<Rule Id="CA1009" Action="Warning" />
<Rule Id="CA1016" Action="Warning" />
<Rule Id="CA1033" Action="Warning" />
</Rules>
<Rules AnalyzerId="Microsoft.CodeQuality.Analyzers" RuleNamespace="Microsoft.CodeQuality.Analyzers">
<Rule Id="CA1802" Action="Error" />
<Rule Id="CA1814" Action="Info" />
<Rule Id="CA1823" Action="None" />
<Rule Id="CA2217" Action="Warning" />
</Rules>
</RuleSet>
""");
RunDotNet("build -bl");
TestExport(expectedCount: 1, void (string path) =>
{
var found = false;
var expected = $"/ruleset:{Path.Combine("src", "example.ruleset")}";
foreach (var line in File.ReadAllLines(Path.Combine(path, "build.rsp")))
{
if (line == expected)
{
found = true;
break;
}
}

Assert.True(found);
});
}

[Fact]
public void ContentWin32Elements()
{
Expand Down
35 changes: 14 additions & 21 deletions src/Basic.CompilerLog.Util/ExportUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ private sealed class ContentBuilder
internal ResilientDirectory MiscDirectory { get; }
internal ResilientDirectory AnalyzerDirectory { get; }
internal ResilientDirectory GeneratedCodeDirectory { get; }
internal ResilientDirectory BuildOutput { get; }

internal ContentBuilder(string destinationDirectory, string originalProjectFilePath)
{
Expand All @@ -83,6 +84,7 @@ internal ContentBuilder(string destinationDirectory, string originalProjectFileP
MiscDirectory = new(Path.Combine(destinationDirectory, "misc"));
GeneratedCodeDirectory = new(Path.Combine(destinationDirectory, "generated"));
AnalyzerDirectory = new(Path.Combine(destinationDirectory, "analyzers"));
BuildOutput = new(Path.Combine(destinationDirectory, "output"));
Directory.CreateDirectory(SourceDirectory);
Directory.CreateDirectory(EmbeddedResourceDirectory);
}
Expand Down Expand Up @@ -256,40 +258,31 @@ List<string> ProcessRsp()
span.StartsWith("embed", comparison) ||
span.StartsWith("resource", comparison) ||
span.StartsWith("linkresource", comparison) ||
span.StartsWith("ruleset", comparison) ||
span.StartsWith("keyfile", comparison))
{
continue;
}

// Need to pre-create the output directories to allow the compiler to execute
// Map all of the output items to the build output directory
if (span.StartsWith("out", comparison) ||
span.StartsWith("refout", comparison) ||
span.StartsWith("doc", comparison))
span.StartsWith("doc", comparison) ||
span.StartsWith("generatedfilesout", comparison) ||
span.StartsWith("errorlog", comparison))
{
var index = span.IndexOf(':');
var tempDir = span.Slice(index + 1).ToString();
var argName = span.Slice(0, index).ToString();
var originalValue = span.Slice(index + 1).ToString();
var newValue = builder.BuildOutput.GetNewFilePath(originalValue);
commandLineList.Add($@"/{argName}:{FormatPathArgument(newValue)}");

// The RSP can write out full paths in some cases for these items, rewrite them to local
// outside of obj
if (Path.IsPathRooted(tempDir))
if (span.StartsWith("generatedfilesout", comparison))
{
var argName = span.Slice(0, index).ToString();
var argPath = Path.Combine("obj", argName, Path.GetFileName(tempDir));
var argFullPath = Path.Combine(destinationDir, argPath);
var isDir = string.IsNullOrEmpty(Path.GetExtension(tempDir));
Directory.CreateDirectory(isDir
? argFullPath
: Path.GetDirectoryName(argFullPath)!);
commandLineList.Add($@"/{argName}:""{argPath}""");
continue;
}
else
{
tempDir = Path.Combine(destinationDir, span.Slice(index + 1).ToString());
tempDir = Path.GetDirectoryName(tempDir);
Directory.CreateDirectory(tempDir!);
Directory.CreateDirectory(newValue);
}

continue;
}

lines.Add(line);
Expand Down
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ int RunExport(IEnumerable<string> args)
var excludeAnalyzers = false;
var options = new FilterOptionSet()
{
{ "o|out=", "path to output rsp files", o => baseOutputPath = o },
{ "o|out=", "path to export build content", o => baseOutputPath = o },
{ "e|exclude-analyzers", "emit the compilation without analyzers / generators", e => excludeAnalyzers = e is not null },
};

Expand Down
2 changes: 1 addition & 1 deletion src/Basic.CompilerLog/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"CompilerLogger": {
"commandName": "Project",
"commandLineArgs": "rsp C:\\Users\\jaredpar\\Downloads\\7.0.306.zip.binlog",
"commandLineArgs": "export -o c:\\users\\jaredpar\\temp\\export \"C:\\Users\\jaredpar\\Downloads\\msbuild.complog\"",
"workingDirectory": "C:\\users\\jaredpar\\temp"
}
}
Expand Down

0 comments on commit 89f5720

Please sign in to comment.