Skip to content

Commit

Permalink
lintcmd: make binary output OS agnostic
Browse files Browse the repository at this point in the history
We were storing absolute paths and full token.Position in the binary
output. This works fine when merging multiple runs from the same OS, but
not so when dealing with multiple OSs, which have different path
separators and different absolute paths. We also have to zero out
token.Position.Offset to handle different newlines on different OSs.

(cherry picked from commit 87cceb1)
  • Loading branch information
dominikh committed Apr 4, 2022
1 parent c285ca0 commit 1846305
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lintcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"log"
"os"
"path/filepath"
"reflect"
"runtime"
"runtime/pprof"
Expand Down Expand Up @@ -436,7 +437,46 @@ func (cmd *Command) Run() {
fmt.Fprintln(os.Stderr, "warning:", w)
}

cwd, err := os.Getwd()
if err != nil {
cwd = ""
}
relPath := func(s string) string {
if cwd == "" {
return filepath.ToSlash(s)
}
out, err := filepath.Rel(cwd, s)
if err != nil {
return filepath.ToSlash(s)
}
return filepath.ToSlash(out)
}

if cmd.flags.formatter == "binary" {
for i, s := range res.CheckedFiles {
res.CheckedFiles[i] = relPath(s)
}
for i := range res.Diagnostics {
// We turn all paths into relative, /-separated paths. This is to make -merge work correctly when
// merging runs from different OSs, with different absolute paths.
//
// We zero out Offset, because checkouts of code on different OSs may have different kinds of
// newlines and thus different offsets. We don't ever make use of the Offset, anyway. Line and
// column numbers are precomputed.

d := &res.Diagnostics[i]
d.Position.Filename = relPath(d.Position.Filename)
d.Position.Offset = 0
d.End.Filename = relPath(d.End.Filename)
d.End.Offset = 0
for j := range d.Related {
r := &d.Related[j]
r.Position.Filename = relPath(r.Position.Filename)
r.Position.Offset = 0
r.End.Filename = relPath(r.End.Filename)
r.End.Offset = 0
}
}
err := gob.NewEncoder(os.Stdout).Encode(res)
if err != nil {
fmt.Fprintf(os.Stderr, "failed writing output: %s\n", err)
Expand Down

0 comments on commit 1846305

Please sign in to comment.