globusdigital / deep-copy

Deep copy generator
BSD 3-Clause "New" or "Revised" License
150 stars 34 forks source link

Don't truncate output file before code generation #6

Closed sorinescu closed 4 years ago

sorinescu commented 4 years ago

I've found the following bug with Go 1.13 and earlier (the code works fine with Go 1.14):

I want to generate a DeepCopy method on my type in a file inside my package, with a command like deep-copy -o generated_mytype_deepcopy.go -type MyType -pointer-receiver my/package. This works fine.

I then call DeepCopy from a piece of code within my/package.

However, if I make changes to my type and I want to regenerate the file, it will fail because the tool first truncates the output file, then it tries to load my/package, which will now fail for two reasons:

  1. The output file is empty when it should start with package ...
  2. The code that calls DeepCopy can no longer be compiled because the method is gone.

This PR postpones opening the output file until it actually has to write to it, but it still checks that the file can be written without opening it.

codecov[bot] commented 4 years ago

Codecov Report

Merging #6 into master will decrease coverage by 2.30%. The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #6      +/-   ##
==========================================
- Coverage   67.37%   65.06%   -2.31%     
==========================================
  Files           1        1              
  Lines         282      292      +10     
==========================================
  Hits          190      190              
- Misses         73       83      +10     
  Partials       19       19              
Impacted Files Coverage Δ
main.go 65.06% <0.00%> (-2.31%) :arrow_down: