Skip to content

Mergetool fixes#291

Merged
infinisil merged 3 commits intomasterfrom
mergetool-fixes
Apr 4, 2025
Merged

Mergetool fixes#291
infinisil merged 3 commits intomasterfrom
mergetool-fixes

Conversation

@infinisil
Copy link
Member

Turns out there are some problems to fix, see the commit messages for details. I made sure to add regression tests.


This work is funded by Antithesis and Tweag

Turns out, `git merge-tool` wants the base version in the _second_
argument, and not the first! This was never noticed, because for changes
_only_ related to formatting, it won't matter, since for usual rebases
and merges, the actual first argument, the local version, only contains
the formatting changes, which can be entirely ignored anyways.

So this only causes problems if there's not exclusively
formatting-related changes, which can of course happen.
Turns out, if the mergetool tries to write files larger than the buffer
size, it gets stuck, because in the previous code there was nothing that
reads that buffer asynchronously.

This now fixes that by writing to a file instead of a handle.
@github-actions
Copy link

github-actions bot commented Apr 2, 2025

Nixpkgs diff

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/nix-formatting-team-full-nixpkgs-reformat/61867/1

Copy link
Collaborator

@jfly jfly left a comment

Choose a reason for hiding this comment

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

Thanks!

main/Main.hs Outdated
(proc "git" ["merge-file", "--stdout", local, base, remote])
{ std_out = CreatePipe
}
exitCode <- lift $ withFile merged WriteMode $ \out -> do
Copy link
Collaborator

Choose a reason for hiding this comment

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

Feel free to ignore this comment:

My haskell is too weak to really grok this code. Is there any issue if we crash partway through invoking git merge-file? Could we end up with a cut off file?

I was expecting to see something like "write to a temp file and then atomically move". But I guess we didn't have that in the old code, or at least I don't see anything like that.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! I now added a commit that both simplifies the code and takes care of this. It works by not using the --stdout flag, causing git merge-file to write it to the local file directly (which is a temporary file created by git mergetool), and then move that file to merged after formatting :D

More resilient in case e.g. git merge-file fails in the middle
@infinisil infinisil enabled auto-merge April 4, 2025 11:57
@infinisil infinisil merged commit 65af4b6 into master Apr 4, 2025
2 checks passed
@infinisil infinisil deleted the mergetool-fixes branch April 4, 2025 11:59
@github-project-automation github-project-automation bot moved this from Todo to Done in Nix formatting Apr 4, 2025
infinisil added a commit to tweag/nixpkgs that referenced this pull request Apr 4, 2025
Probably the last unstable update before stabilising nixfmt. Includes:
- Crucial fixes for the merge tool: NixOS/nixfmt#291
- Recursive mode deprecation warning informing of nixfmt-tree: NixOS/nixfmt#287
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants