Skip to content

Commit 1846305

Browse files
committed
lintcmd: make binary output OS agnostic
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)
1 parent c285ca0 commit 1846305

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

lintcmd/cmd.go

+40
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"io"
1212
"log"
1313
"os"
14+
"path/filepath"
1415
"reflect"
1516
"runtime"
1617
"runtime/pprof"
@@ -436,7 +437,46 @@ func (cmd *Command) Run() {
436437
fmt.Fprintln(os.Stderr, "warning:", w)
437438
}
438439

440+
cwd, err := os.Getwd()
441+
if err != nil {
442+
cwd = ""
443+
}
444+
relPath := func(s string) string {
445+
if cwd == "" {
446+
return filepath.ToSlash(s)
447+
}
448+
out, err := filepath.Rel(cwd, s)
449+
if err != nil {
450+
return filepath.ToSlash(s)
451+
}
452+
return filepath.ToSlash(out)
453+
}
454+
439455
if cmd.flags.formatter == "binary" {
456+
for i, s := range res.CheckedFiles {
457+
res.CheckedFiles[i] = relPath(s)
458+
}
459+
for i := range res.Diagnostics {
460+
// We turn all paths into relative, /-separated paths. This is to make -merge work correctly when
461+
// merging runs from different OSs, with different absolute paths.
462+
//
463+
// We zero out Offset, because checkouts of code on different OSs may have different kinds of
464+
// newlines and thus different offsets. We don't ever make use of the Offset, anyway. Line and
465+
// column numbers are precomputed.
466+
467+
d := &res.Diagnostics[i]
468+
d.Position.Filename = relPath(d.Position.Filename)
469+
d.Position.Offset = 0
470+
d.End.Filename = relPath(d.End.Filename)
471+
d.End.Offset = 0
472+
for j := range d.Related {
473+
r := &d.Related[j]
474+
r.Position.Filename = relPath(r.Position.Filename)
475+
r.Position.Offset = 0
476+
r.End.Filename = relPath(r.End.Filename)
477+
r.End.Offset = 0
478+
}
479+
}
440480
err := gob.NewEncoder(os.Stdout).Encode(res)
441481
if err != nil {
442482
fmt.Fprintf(os.Stderr, "failed writing output: %s\n", err)

0 commit comments

Comments
 (0)