Skip to content

Commit

Permalink
Avoid putting /noconfig option into the .rsp file (#164)
Browse files Browse the repository at this point in the history
* Avoid putting `/noconfig` option into the `.rsp` file

* Simplify to one bool variable
  • Loading branch information
jjonescz authored Oct 14, 2024
1 parent 39d24c8 commit 417b5cc
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 9 deletions.
87 changes: 79 additions & 8 deletions src/Basic.CompilerLog.UnitTests/ExportUtilTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public ExportUtilTests(ITestOutputHelper testOutputHelper, CompilerLogFixture fi
Fixture = fixture;
}

private void TestExport(int expectedCount, Action<string>? verifyExportCallback = null, bool runBuild = true)
private void TestExport(
int expectedCount,
Action<string>? verifyExportCallback = null,
bool runBuild = true,
Action<ProcessResult>? verifyBuildResult = null)
{
using var scratchDir = new TempDir("export test");
var binlogFilePath = Path.Combine(RootDirectory, "msbuild.binlog");
Expand All @@ -37,19 +41,58 @@ private void TestExport(int expectedCount, Action<string>? verifyExportCallback
// ensures our builds below don't succeed because old files are being referenced
Root.EmptyDirectory();

TestExport(compilerLogFilePath, expectedCount, verifyExportCallback: verifyExportCallback, runBuild: runBuild);
TestExport(
compilerLogFilePath,
expectedCount,
verifyExportCallback: verifyExportCallback,
runBuild: runBuild,
verifyBuildResult: verifyBuildResult);
}

private void TestExport(string compilerLogFilePath, int? expectedCount, bool includeAnalyzers = true, Action<string>? verifyExportCallback = null, bool runBuild = true) =>
TestExport(TestOutputHelper, compilerLogFilePath, expectedCount, includeAnalyzers, verifyExportCallback, runBuild);

internal static void TestExport(ITestOutputHelper testOutputHelper, string compilerLogFilePath, int? expectedCount, bool includeAnalyzers = true, Action<string>? verifyExportCallback = null, bool runBuild = true)
private void TestExport(
string compilerLogFilePath,
int? expectedCount,
bool includeAnalyzers = true,
Action<string>? verifyExportCallback = null,
bool runBuild = true,
Action<ProcessResult>? verifyBuildResult = null) =>
TestExport(
TestOutputHelper,
compilerLogFilePath,
expectedCount,
includeAnalyzers,
verifyExportCallback,
runBuild,
verifyBuildResult);

internal static void TestExport(
ITestOutputHelper testOutputHelper,
string compilerLogFilePath,
int? expectedCount,
bool includeAnalyzers = true,
Action<string>? verifyExportCallback = null,
bool runBuild = true,
Action<ProcessResult>? verifyBuildResult = null)
{
using var reader = CompilerLogReader.Create(compilerLogFilePath);
TestExport(testOutputHelper, reader, expectedCount, includeAnalyzers, verifyExportCallback, runBuild);
TestExport(
testOutputHelper,
reader,
expectedCount,
includeAnalyzers,
verifyExportCallback,
runBuild,
verifyBuildResult);
}

internal static void TestExport(ITestOutputHelper testOutputHelper, CompilerLogReader reader, int? expectedCount, bool includeAnalyzers = true, Action<string>? verifyExportCallback = null, bool runBuild = true)
internal static void TestExport(
ITestOutputHelper testOutputHelper,
CompilerLogReader reader,
int? expectedCount,
bool includeAnalyzers = true,
Action<string>? verifyExportCallback = null,
bool runBuild = true,
Action<ProcessResult>? verifyBuildResult = null)
{
#if NET
var sdkDirs = SdkUtil.GetSdkDirectories();
Expand All @@ -71,6 +114,7 @@ internal static void TestExport(ITestOutputHelper testOutputHelper, CompilerLogR
var buildResult = RunBuildCmd(tempDir.DirectoryPath);
testOutputHelper.WriteLine(buildResult.StandardOut);
testOutputHelper.WriteLine(buildResult.StandardError);
verifyBuildResult?.Invoke(buildResult);
Assert.True(buildResult.Succeeded, $"Cannot build {compilerCall.ProjectFileName}");
}

Expand Down Expand Up @@ -324,6 +368,33 @@ unsafe class C { }
runBuild: true);
}

/// <summary>
/// <c>/noconfig</c> should not be part of <c>.rsp</c> files
/// (the compiler gives a warning if it is and ignores the option).
/// </summary>
[Fact]
public void ExportNoconfig()
{
RunDotNet("new console --name example --output .");
RunDotNet("build -bl -nr:false");

var binlog = Path.Combine(RootDirectory, "msbuild.binlog");
var complog = Path.Combine(RootDirectory, "msbuild.complog");
var result = CompilerLogUtil.TryConvertBinaryLog(binlog, complog);
Assert.True(result.Succeeded);

TestExport(
compilerLogFilePath: complog,
expectedCount: 1,
includeAnalyzers: false,
runBuild: true,
verifyBuildResult: static result =>
{
// warning CS2023: Ignoring /noconfig option because it was specified in a response file
Assert.DoesNotContain("CS2023", result.StandardOut);
});
}

private void EmbedLineCore(string contentFilePath)
{
RunDotNet($"new console --name example --output .");
Expand Down
13 changes: 12 additions & 1 deletion src/Basic.CompilerLog.Util/ExportUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public void Export(CompilerCall compilerCall, string destinationDir, IEnumerable
var builder = new ContentBuilder(destinationDir, compilerCall.ProjectFilePath);

var commandLineList = new List<string>();
bool hasNoConfigOption = false;
var data = Reader.ReadRawCompilationData(compilerCall);
Directory.CreateDirectory(destinationDir);
WriteGeneratedFiles();
Expand Down Expand Up @@ -154,7 +155,11 @@ void WriteBuildCmd(string sdkDir, string cmdFileName)
? Path.Combine(execPath, "csc.dll")
: Path.Combine(execPath, "vbc.dll");

lines.Add($@"dotnet exec ""{execPath}"" @build.rsp");
var noConfig = hasNoConfigOption
? "/noconfig "
: string.Empty;

lines.Add($@"dotnet exec ""{execPath}"" {noConfig}@build.rsp");
var cmdFilePath = Path.Combine(destinationDir, cmdFileName);
File.WriteAllLines(cmdFilePath, lines);

Expand Down Expand Up @@ -188,6 +193,12 @@ List<string> ProcessRsp()

var span = line.AsSpan().Slice(1);

if (span.Equals("noconfig".AsSpan(), comparison))
{
hasNoConfigOption = true;
continue;
}

// These options are all rewritten below
if (span.StartsWith("reference", comparison) ||
span.StartsWith("analyzer", comparison) ||
Expand Down

0 comments on commit 417b5cc

Please sign in to comment.