Skip to content
Open
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
36 changes: 33 additions & 3 deletions src/Cli/dotnet/Commands/Run/CSharpCompilerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,18 @@ public int Execute(out bool fallbackToNormalBuild)
if (BuildResultFile != null &&
CSharpCommandLineParser.Default.Parse(CscArguments, BaseDirectory, sdkDirectory: null) is { OutputFileName: { } outputFileName } parsedArgs)
{
var objFile = parsedArgs.GetOutputFilePath(outputFileName);
Reporter.Verbose.WriteLine($"Copying '{objFile}' to '{BuildResultFile}'.");
File.Copy(objFile, BuildResultFile, overwrite: true);
var objFile = new FileInfo(parsedArgs.GetOutputFilePath(outputFileName));
var binFile = new FileInfo(BuildResultFile);
Comment on lines +113 to +114
Copy link
Preview

Copilot AI Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating FileInfo objects here may cause unnecessary file system calls. Consider creating them only when needed inside the HaveMatchingSizeAndTimeStamp method, or check if the files exist first to avoid exceptions.

Copilot uses AI. Check for mistakes.


if (HaveMatchingSizeAndTimeStamp(objFile, binFile))
{
Reporter.Verbose.WriteLine($"Skipping copy of '{objFile}' to '{BuildResultFile}' because the files have matching size and timestamp.");
}
else
{
Reporter.Verbose.WriteLine($"Copying '{objFile}' to '{BuildResultFile}'.");
File.Copy(objFile.FullName, binFile.FullName, overwrite: true);
}
}

return exitCode;
Expand Down Expand Up @@ -153,6 +162,27 @@ static int ProcessBuildResponse(BuildResponse response, out bool fallbackToNorma
return 1;
}
}

// Inspired by MSBuild: https://github.com/dotnet/msbuild/blob/a7a4d5af02be5aa6dc93a492d6d03056dc811388/src/Tasks/Copy.cs#L208
static bool HaveMatchingSizeAndTimeStamp(FileInfo sourceFile, FileInfo destinationFile)
{
if (!destinationFile.Exists)
{
return false;
}

if (sourceFile.LastWriteTimeUtc != destinationFile.LastWriteTimeUtc)
{
return false;
}

if (sourceFile.Length != destinationFile.Length)
{
return false;
}

return true;
}
}

private void PrepareAuxiliaryFiles(out string rspPath)
Expand Down
31 changes: 31 additions & 0 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3258,6 +3258,37 @@ Release config
""");
}

/// <summary>
/// See <see cref="CscOnly_AfterMSBuild"/>.
/// If hard links are enabled, the <c>bin/app.dll</c> and <c>obj/app.dll</c> files are going to be the same,
/// so our "copy obj to bin" logic must account for that.
/// </summary>
[Fact]
public void CscOnly_AfterMSBuild_HardLinks()
{
var testInstance = _testAssetsManager.CreateTestDirectory(baseDirectory: OutOfTreeBaseDirectory);
var programPath = Path.Join(testInstance.Path, "Program.cs");

var code = $"""
#:property CreateHardLinksForCopyFilesToOutputDirectoryIfPossible=true
#:property CreateSymbolicLinksForCopyFilesToOutputDirectoryIfPossible=true
{s_program}
""";

File.WriteAllText(programPath, code);

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

Build(testInstance, BuildLevel.All);

code = code.Replace("Hello", "Hi");
File.WriteAllText(programPath, code);

Build(testInstance, BuildLevel.Csc, expectedOutput: "Hi from Program");
}

/// <summary>
/// See <see cref="CscOnly_AfterMSBuild"/>.
/// This optimization currently does not support <c>#:project</c> references and hence is disabled if those are present.
Expand Down
Loading