Skip to content
Merged
13 changes: 13 additions & 0 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ public override int Execute()
cache.CurrentEntry.Run = LastRunProperties;

CacheCscArguments(cache, buildResult);
WriteCscRsp(cache);
CollectAdditionalSources(cache, buildRequest.ProjectInstance);

MarkBuildSuccess(cache);
Comment thread
jjonescz marked this conversation as resolved.
Expand Down Expand Up @@ -489,6 +490,18 @@ void ReuseInfoFromPreviousCacheEntry(CacheInfo cache)
}
}

void WriteCscRsp(CacheInfo cache)
{
if (cache.CurrentEntry.CscArguments.IsDefaultOrEmpty)
{
return;
}

string rspPath = Path.Join(Builder.ArtifactsPath, "csc.rsp");
File.WriteAllLines(rspPath, cache.CurrentEntry.CscArguments);
Comment thread
jjonescz marked this conversation as resolved.
Outdated
Reporter.Verbose.WriteLine($"Written '{rspPath}'.");
Comment thread
jjonescz marked this conversation as resolved.
Outdated
}

bool CanSaveCache(ProjectInstance projectInstance)
{
if (!MSBuildUtilities.ConvertStringToBool(projectInstance.GetPropertyValue(FileBasedProgramCanSkipMSBuild), defaultValue: true))
Expand Down
Comment thread
jjonescz marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -7092,4 +7092,4 @@
}
}
]
}
}
Comment thread
jjonescz marked this conversation as resolved.
28 changes: 28 additions & 0 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5401,6 +5401,34 @@ public void CscOnly_AfterMSBuild_AuxiliaryFilesNotReused()
Build(testInstance, BuildLevel.Csc, expectedOutput: "v3 ");
}

/// <summary>
/// Verifies that <c>csc.rsp</c> is written to disk after a full MSBuild build,
/// so that IDEs can read it to create a virtual project.
/// </summary>
[Fact]
public void MSBuild_WritesCscRsp()
{
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory);

var programPath = Path.Join(testInstance.Path, "Program.cs");
File.WriteAllText(programPath, """
#:property Configuration=Release
Console.Write("Hello");
""");

// Remove artifacts from possible previous runs of this test.
var artifactsDir = VirtualProjectBuilder.GetArtifactsPath(programPath);
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);

// A build directive forces a full MSBuild build.
Build(testInstance, BuildLevel.All, expectedOutput: "Hello");

// csc.rsp should be written to disk after a full MSBuild build.
var rspPath = Path.Join(artifactsDir, "csc.rsp");
File.Exists(rspPath).Should().BeTrue("csc.rsp should be written after a full MSBuild build");
File.ReadAllLines(rspPath).Should().NotBeEmpty("csc.rsp should contain compiler arguments");
}

/// <summary>
/// Testing <see cref="CscOnly"/> optimization when the NuGet cache is cleared between builds.
/// See <see href="https://github.com/dotnet/sdk/issues/45169"/>.
Expand Down
Loading