Error while applying patch: Can not calculate a status for patch (C#/Git personal builds)

Hi,

We're using TeamCity Professional 2025.03.3 Build 186370 and we get this error running personal builds through the TeamCity UI.  We're using a Windows build agent running and both server and the agent are running JRE: 21.0.7+8-LTS-245.  We're also using git diff instructions as described in  https://www.jetbrains.com/help/teamcity/personal-build.html#Direct+Patch+Upload.

The error only occurs when the patch includes a change to the first two lines of one or more of our C# source files.  We're using Visual Studio so these C# files are in UTF-8 encoding which would always always includes a Byte Order Mark (BOM) character at the beginning of the very first line.  We believe this to be an encoding problem as patches downloaded from TeamCity contain an additional change to remove the BOM.  We've confirmed that we're using the default value of UTF8 for teamcity.agent.jvm.file.encoding on the hm3 build agent.

We've also just enabled "Native Git operations" (using Git version 2.49.0.0) and that didn't fix this.  The specific versions of TeamCity and JRE don't seem to matter either.  Previously, we were getting the error on the prior TeamCity version and JRE 17.x.

Here's the stack trace for the error from the build agent's Wrapper.log:

INFO   | jvm 1    | 2025/07/01 08:46:18 | [2025-07-01 08:46:18,677]   WARN - .apply.PlainSimplePatchApplier - Failed to apply patch: jetbrains.buildServer.vcs.patches.diff.apply.PlainSimplePatchApplier$PatchApplyException: Unexpected hunk content: expected - 'namespace some.namespace', actual - 'PatchLine{myType=REMOVE, myText='namespace some.namespace', mySuppressNewLine=false}' (enable debug to see stacktrace) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | [2025-07-01 08:46:18,708]   WARN - l.patch.AbstractSourcesUpdater - Exception occurred while patch applying: Can not calculate a status for patch for file.cs 
INFO   | jvm 1    | 2025/07/01 08:46:18 | jetbrains.buildServer.vcs.patches.PatchException: Can not calculate a status for patch for file.cs 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.vcs.patches.UnifiedDiffUndoSupportingPatcher.applyAndCheck(UnifiedDiffUndoSupportingPatcher.java:157) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.vcs.patches.UnifiedDiffUndoSupportingPatcher.applyPatch(UnifiedDiffUndoSupportingPatcher.java:118) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.PatchApplierImpl.applyPatch(PatchApplierImpl.java:19) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.UpdateSourcesPatcherBase.applyPatch(UpdateSourcesPatcherBase.java:75) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.UpdateSourcesFromPersonal.applyPatchWithRelease(UpdateSourcesFromPersonal.java:58) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.UpdateSourcesFromPersonal.updateSources(UpdateSourcesFromPersonal.java:46) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.AbstractSourcesUpdater.doSourceUpdate(AbstractSourcesUpdater.java:52) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.patch.CheckoutPersonalPatchBuildStage.doRecoverableStage(CheckoutPersonalPatchBuildStage.java:38) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.startStages.RecoverableBuildStage.doBuildStage(RecoverableBuildStage.java:76) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor$1.callStage(BuildStagesExecutor.java:33) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor$1.callStage(BuildStagesExecutor.java:24) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.callRunStage(StagesExecutor.java:76) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.StagesExecutor.doStages(StagesExecutor.java:35) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.buildStages.BuildStagesExecutor.doStages(BuildStagesExecutor.java:24) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.BuildRunActionImpl.doStages(BuildRunActionImpl.java:97) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.AgentOperationModeEx.executeRunnerStages(AgentOperationModeEx.java:47) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.BuildRunActionImpl.runBuild(BuildRunActionImpl.java:68) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.BuildAgentImpl.doActualBuild(BuildAgentImpl.java:352) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.BuildAgentImpl.access$100(BuildAgentImpl.java:62) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at jetbrains.buildServer.agent.impl.BuildAgentImpl$1.run(BuildAgentImpl.java:301) 
INFO   | jvm 1    | 2025/07/01 08:46:18 | 	at java.base/java.lang.Thread.run(Thread.java:1583) 

Any help or workarounds would be appreciated.

Thanks,

Mike

 

 

 


 

0
3 comments

Hi,

The issue where TeamCity fails to apply patches when the first line of a C# file is modified. The root cause appears to be related to how TeamCity handles files saved with a UTF-8 Byte Order Mark (BOM) — which is commonly added by Visual Studio.

To avoid this problem, you should ensure that all C# files are saved as UTF-8 without BOM. This can be done easily by adding a .editorconfig file at the root of the solution with the following setting:

[*.cs]
charset = utf-8

This instructs Visual Studio to save all .cs files as UTF-8 without BOM by default.

For Visual Studio 2019/2022 and later, you also can do the following way

• Go to Tools → Options.

• Expand Environment → Documents.

• Check the box: "Save files with a specific encoding" (Unicode(UTF-8 without signature).

• For more control, use the EditorConfig method above.

 

Best Regards,

Tom

0

Thanks Tom, we might try that workaround but its definitely not ideal.  We would need to update all files in our solution to remove the BOM.  That might not even be okay for some files.  E.g. C# strings with special characters  

Is there a reason that TeamCity needs to alter the patch file at all?  Even if there is a reason for some scenario, maybe a setting could be added to disable this “feature” for others?   

Basically, it seems like a massive and unnecessary bug that affects all VS/C# users using Git, default settings, and personal builds.  On the other hand, I could very well be missing something so I look forward to any additional comments on this.

0
Hi,

> Is there a reason TeamCity needs to alter the patch file?

From what I understand, TeamCity applies patches to create a working copy of your code during personal builds. This patching process is quite strict — it expects the file content to match exactly, including invisible characters such as the UTF-8 BOM. While TeamCity doesn’t deliberately "alter" the patch, its patch application logic isn’t BOM-aware. As a result, even a minor mismatch (like a BOM character) can cause the patch to fail.

> Could there be a setting to disable this?

At the moment, there isn’t a setting to disable or bypass this patch validation logic. However, you’re welcome to submit a feature request via issue tracker: YouTrack.(https://youtrack.jetbrains.com/issue/TW)
0

Please sign in to leave a comment.