Skip to content

Skip obj→bin copy on build failure in CSharpCompilerCommand - #51614

Merged
jjonescz merged 9 commits into
release/10.0.2xxfrom
copilot/modify-csharp-compiler-command
Jan 7, 2026
Merged

jjonescz merged 9 commits into
release/10.0.2xxfrom
copilot/modify-csharp-compiler-command

Conversation

Copilot AI commented Nov 7, 2025

Copy link
Copy Markdown
Contributor

CSharpCompilerCommand.Execute() was copying the obj dll to bin regardless of build success, causing unnecessary work and potential errors when the dll doesn't exist due to compilation failures.

Changes

  • CSharpCompilerCommand.cs: Gate the obj→bin copy with exitCode == 0 check
  • RunFileTests.cs: Add CscOnly_CompilationFailure_NoCopyToBin test verifying bin dll is not updated after failed compilation

The fix ensures failed builds don't produce or modify output artifacts:

// Process the response.
var exitCode = ProcessBuildResponse(responseTask.Result, out fallbackToNormalBuild);

// Copy from obj to bin only if the build succeeded.
if (exitCode == 0 &&
    BuildResultFile != null &&
    CSharpCommandLineParser.Default.Parse(...) is { OutputFileName: { } outputFileName } parsedArgs)
{
    // ... copy logic
}
Original prompt

Let's modify CSharpCompilerCommand to skip copying the obj dll to bin directory if the build fails (it's an unnecessary work and can lead to spurious errors, for example if the source file was not even produced by roslyn due to compilation errors - add a test for that too)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI self-assigned this Nov 7, 2025
Copilot AI and others added 2 commits November 7, 2025 13:36
Modified CSharpCompilerCommand to only copy the obj dll to bin when exitCode == 0.
Added test CscOnly_CompilationFailure_NoCopyToBin to verify bin dll is not updated on failure.

Co-authored-by: jjonescz <3669664+jjonescz@users.noreply.github.com>
Removed unnecessary Thread.Sleep(100) from the test as we're comparing timestamps
for equality, not testing for differences after a delay.

Co-authored-by: jjonescz <3669664+jjonescz@users.noreply.github.com>
Copilot AI changed the title [WIP] Update CSharpCompilerCommand to skip copying obj dll on build failure Skip obj→bin copy on build failure in CSharpCompilerCommand Nov 7, 2025
Copilot AI requested a review from jjonescz November 7, 2025 13:42
@jjonescz
jjonescz marked this pull request as ready for review November 7, 2025 17:25
@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Nov 7, 2025
@jjonescz
jjonescz requested review from a team and Copilot November 7, 2025 17:25

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a safeguard to prevent copying DLL files from the obj directory to the bin directory when C# compilation fails. The change ensures that spurious errors don't occur from attempting to copy non-existent or incomplete DLL files after compilation errors.

Key Changes

  • Modified the copy logic in CSharpCompilerCommand.cs to only copy files when exitCode == 0
  • Added comprehensive test coverage for the compilation failure scenario

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Cli/dotnet/Commands/Run/CSharpCompilerCommand.cs Added exit code check to prevent copying DLLs when compilation fails
test/dotnet.Tests/CommandTests/Run/RunFileTests.cs Added test to verify DLLs are not copied when compilation fails

@RikkiGibson

Copy link
Copy Markdown
Member

Is there a specific issue that is being solved by this PR? If so, please link to it.

@jjonescz

Copy link
Copy Markdown
Member

There is not an existing issue afaik; I noticed the problem while working on another PR. The added test has the "repro" for the issue though.

File.Delete(binDll);

// Write invalid code that causes compilation to fail
code = code.Replace(";", "");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rather than a compile error that could potentially be adjusted in the future, let's just add #error error to the file. Then we can know exactly what the content will be and be resilient to future compiler changes.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@jjonescz it looks like we're still asserting a specific error code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Right, I see what you mean now, I will adjust, thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Btw, this is just a test relying on a compiler error code; whereas for example in #51609, I'm relying on compiler code in product code (although if the error code changes, only that one scenario might stop working optimally, it shouldn't break anything else; also I'm not sure how else to do it).

@jjonescz
jjonescz requested a review from RikkiGibson November 24, 2025 10:00
@jjonescz

jjonescz commented Dec 1, 2025

Copy link
Copy Markdown
Member

@RikkiGibson for another look, thanks

2 similar comments
@jjonescz

jjonescz commented Dec 5, 2025

Copy link
Copy Markdown
Member

@RikkiGibson for another look, thanks

@jjonescz

Copy link
Copy Markdown
Member

@RikkiGibson for another look, thanks

@jjonescz
jjonescz enabled auto-merge (squash) January 7, 2026 13:19
@jjonescz
jjonescz merged commit 2781029 into release/10.0.2xx Jan 7, 2026
23 of 26 checks passed
@jjonescz
jjonescz deleted the copilot/modify-csharp-compiler-command branch January 7, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-run-file Items related to the "dotnet run <file>" effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants