Skip to content

Commit

Permalink
Copy .sum file
Browse files Browse the repository at this point in the history
Previously we added support for not deleting the sum file, but it
was deleted anyway because bingo deletes all *.tmp.* files.
We need to rename not just .mod file but .sum file as well.

We also need to copy the sum file to temporary file.
Existing users don't have .sum files in their .bingo directory,
so we ignore file not found errors for .sum files.
  • Loading branch information
martin-sucha committed Nov 3, 2021
1 parent 20a817b commit 479d590
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion get.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ func getPackage(ctx context.Context, logger *log.Logger, c installPackageConfig,
tmpModFilePath = filepath.Join(c.modDir, fmt.Sprintf("%s.%d.tmp.mod", name, i))
}

outSumFile := strings.TrimSuffix(outModFile, ".mod")+".sum"

// If we don't have all information or update is set, resolve version.
var fetchedDirectives bingo.NonRequireDirectives
if target.Module.Version == "" || !strings.HasPrefix(target.Module.Version, "v") || target.Module.Path == "" || c.update != runner.NoUpdatePolicy {
Expand Down Expand Up @@ -600,7 +602,10 @@ func getPackage(ctx context.Context, logger *log.Logger, c installPackageConfig,

// We were working on tmp file, do atomic rename.
if err := os.Rename(tmpModFile.FileName(), outModFile); err != nil {
return errors.Wrap(err, "rename")
return errors.Wrap(err, "rename mod file")
}
if err := os.Rename(tmpModFile.SumFileName(), outSumFile); err != nil {
return errors.Wrap(err, "rename sum file")
}
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/bingo/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ func CreateFromExistingOrNew(ctx context.Context, r *runner.Runner, logger *log.
if err := copyFile(existingFile, modFile); err != nil {
return nil, err
}
existingSumFile := sumFilePath(existingFile)
sumFile := sumFilePath(modFile)
if err := copyFile(existingSumFile, sumFile); err != nil && !os.IsNotExist(err) {
return nil, err
}
return OpenModFile(modFile)
}
logger.Printf("bingo tool module file %v is malformed; it will be recreated; err: %v\n", existingFile, err)
Expand All @@ -150,6 +155,10 @@ func CreateFromExistingOrNew(ctx context.Context, r *runner.Runner, logger *log.
return OpenModFile(modFile)
}

func sumFilePath(modFilePath string) string {
return strings.TrimSuffix(modFilePath, ".mod")+".sum"
}

func copyFile(src, dst string) error {
source, err := os.Open(src)
if err != nil {
Expand Down Expand Up @@ -184,6 +193,10 @@ func (mf *ModFile) FileName() string {
return mf.filename
}

func (mf *ModFile) SumFileName() string {
return sumFilePath(mf.filename)
}

func (mf *ModFile) IsDirectivesAutoFetchDisabled() bool {
return mf.directivesAutoFetchDisabled
}
Expand Down

0 comments on commit 479d590

Please sign in to comment.