0

I need to include some DLL files inside git. I know this is not a good practice. But this solution solves one of our problems for now.

But the problem is that, while I have not changed anything at all, and not a single line of code is modified and not even a character is modified, when I use dotnet build I see that git marks the output as modified.

I tried to compare them using Beyond Compare's Hex comparison, and here's an image of the beginning bytes:

enter image description here

I can't find out why it's modified. I mean, I expect dotnet build to be deterministic and give me the same results for the same inputs. But apparently something is changed.

Does anyone know why dotnet build has this behavior?

I'm using .NET 6.

Hossein Fallah
  • 1,859
  • 2
  • 18
  • 44
  • 3
    my guess: it includes a _timestamp_ of when the binary was built. which, unless you have a time machine, should be different each time. see also [this](https://stackoverflow.com/questions/1600962/displaying-the-build-date) for reference – Franz Gleichmann Dec 10 '21 at 14:57
  • 3
    You may want to take a look at [reproducible builds](https://github.com/dotnet/reproducible-builds) – MindSwipe Dec 10 '21 at 14:59
  • @FranzGleichmann is correct, a timestamp is stored in the PE Header of the built assembly. – Lasse V. Karlsen Dec 10 '21 at 15:01
  • @FranzGleichmann, if that's the case, is there a way that I can shut it off. I think it's a very stupid decision, since as much as I know, determinisim is a key to create reliable CI/CD pipelines. – Hossein Fallah Dec 10 '21 at 15:08
  • @HosseinFallah i don't think it's a stupid decision. at least it's smarter than storing binaries inside a git repo. you should instead store it as an artifact - should be trivial if you're using CI/CD. also: if you need it to stay the same, just don't _rebuild_ it unless the code base changed? – Franz Gleichmann Dec 10 '21 at 15:11
  • 1
    Roslyn supports [deterministic builds](https://gist.github.com/aelij/b20271f4bd0ab1298e49068b388b54ae) – Klaus Gütter Dec 10 '21 at 15:15
  • @KlausGütter, excelletn link. Thank you. I used `True` in my `.csproj` file and that solved the problem. Thank you so much. – Hossein Fallah Dec 10 '21 at 15:20
  • 1
    @KlausGütter: Thanks for the link. The non-determinism of PE files produced by .NET compilers has been a bugaboo forever. I know that Microsoft produced a tool way back when that would do a binary compare of two PE files that excluded the bits that changed on every build. It was essential in some regulated industries. As far as I know, it was never widely available (you needed a Premier Support contract and an inquisitive TAM or ADC to get access). This is great to know. Thanks! – Flydog57 Dec 10 '21 at 16:27

1 Answers1

1

Let me summarize the comments, so that there's an answer here (OP should probably have done this): Git says the file is modified because it is in fact modified. There is a build timestamp (in this case it is just a timestamp; other situations in other systems may include additional items) that makes the build not reproducible or deterministic.

This is a pretty common problem. Some systems have a knob you can set to get deterministic builds, and this particular system did. Turning it on solved the problem. See the link provided in Klaus Gütter's comment.

torek
  • 448,244
  • 59
  • 642
  • 775