Skip to content

Commit

Permalink
write a diff when -check fails
Browse files Browse the repository at this point in the history
This makes it easier to understand what went wrong
when depaware fails in a CI or other non-local context.


The diff could be improved, in that a single addition can cause cascading diffs.

For example, adding a bufio dependency to a package can cause this:

-        bytes                                                        from encoding/json+
+        bytes                                                        from bufio+

Observe that bytes didn't really change meaningfully--it was imported by >=2 packages before, and it still is. But now it is imported by bufio, which is alphabetically prior to encoding/json. This adds a bunch of noise to the diffs.
It's not obvious what the best fix to this is.


Sample output, from running on this commit:

$ TERM=dumb go run depaware-main.go -check .
The list of dependencies in /Users/josh/t/depaware/depaware.txt are out of date.

--- before
+++ after
@@ -1,16 +1,23 @@
 github.com/tailscale/depaware dependencies: (generated by github.com/tailscale/depaware)
 
+        github.com/pkg/diff                                          from github.com/tailscale/depaware/depaware
+        github.com/pkg/diff/ctxt                                     from github.com/pkg/diff
+        github.com/pkg/diff/edit                                     from github.com/pkg/diff/ctxt+
+        github.com/pkg/diff/intern                                   from github.com/pkg/diff
+        github.com/pkg/diff/myers                                    from github.com/pkg/diff
+        github.com/pkg/diff/write                                    from github.com/pkg/diff+
         github.com/tailscale/depaware/depaware                       from github.com/tailscale/depaware
-        bytes                                                        from encoding/json+
-        context                                                      from os/exec
+        bufio                                                        from github.com/pkg/diff+
+        bytes                                                        from bufio+
+        context                                                      from github.com/pkg/diff+
         encoding                                                     from encoding/json
         encoding/base64                                              from encoding/json
         encoding/binary                                              from encoding/base64
         encoding/json                                                from github.com/tailscale/depaware/depaware
-        errors                                                       from bytes+
+        errors                                                       from bufio+
         flag                                                         from github.com/tailscale/depaware/depaware
         fmt                                                          from encoding/json+
-        io                                                           from bytes+
+        io                                                           from bufio+
         io/ioutil                                                    from github.com/tailscale/depaware/depaware
         log                                                          from github.com/tailscale/depaware/depaware
         math                                                         from encoding/binary+
@@ -21,12 +28,12 @@
         reflect                                                      from encoding/binary+
         sort                                                         from encoding/json+
         strconv                                                      from encoding/base64+
-        strings                                                      from encoding/json+
+        strings                                                      from bufio+
         sync                                                         from context+
         sync/atomic                                                  from context+
         syscall                                                      from internal/poll+
         time                                                         from context+
         unicode                                                      from bytes+
         unicode/utf16                                                from encoding/json+
-        unicode/utf8                                                 from bytes+
+        unicode/utf8                                                 from bufio+
         unsafe                                                       from internal/bytealg+
exit status 1
  • Loading branch information
josharian committed Sep 14, 2020
1 parent cb75102 commit 3f1070f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
19 changes: 13 additions & 6 deletions depaware.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
github.com/tailscale/depaware dependencies: (generated by github.com/tailscale/depaware)

github.com/pkg/diff from github.com/tailscale/depaware/depaware
github.com/pkg/diff/ctxt from github.com/pkg/diff
github.com/pkg/diff/edit from github.com/pkg/diff/ctxt+
github.com/pkg/diff/intern from github.com/pkg/diff
github.com/pkg/diff/myers from github.com/pkg/diff
github.com/pkg/diff/write from github.com/pkg/diff+
github.com/tailscale/depaware/depaware from github.com/tailscale/depaware
bytes from encoding/json+
context from os/exec
bufio from github.com/pkg/diff+
bytes from bufio+
context from github.com/pkg/diff+
encoding from encoding/json
encoding/base64 from encoding/json
encoding/binary from encoding/base64
encoding/json from github.com/tailscale/depaware/depaware
errors from bytes+
errors from bufio+
flag from github.com/tailscale/depaware/depaware
fmt from encoding/json+
io from bytes+
io from bufio+
io/ioutil from github.com/tailscale/depaware/depaware
log from github.com/tailscale/depaware/depaware
math from encoding/binary+
Expand All @@ -21,12 +28,12 @@ github.com/tailscale/depaware dependencies: (generated by github.com/tailscale/d
reflect from encoding/binary+
sort from encoding/json+
strconv from encoding/base64+
strings from encoding/json+
strings from bufio+
sync from context+
sync/atomic from context+
syscall from internal/poll+
time from context+
unicode from bytes+
unicode/utf16 from encoding/json+
unicode/utf8 from bytes+
unicode/utf8 from bufio+
unsafe from internal/bytealg+
13 changes: 12 additions & 1 deletion depaware/depaware.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import (
"sort"
"strings"
"unicode"

"github.com/pkg/diff"
"github.com/pkg/diff/write"
)

// the go list -json format (parts we care about)
Expand Down Expand Up @@ -146,7 +149,15 @@ func Main() {
// Success. No changes.
return
}
fmt.Fprintf(os.Stderr, "The list of dependencies in %s are out of date.\n", daFile)
var opts []write.Option
if os.Getenv("TERM") != "dumb" {
opts = append(opts, write.TerminalColor())
}
fmt.Fprintf(os.Stderr, "The list of dependencies in %s are out of date.\n\n", daFile)
err = diff.Text("before", "after", was, buf.Bytes(), os.Stderr, opts...)
if err != nil {
log.Fatal(err)
}
os.Exit(1)
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/tailscale/depaware

go 1.15

require github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7 h1:+/+DxvQaYifJ+grD4klzrS5y+KJXldn/2YTl5JG+vZ8=
github.com/pkg/diff v0.0.0-20200914180035-5b29258ca4f7/go.mod h1:zO8QMzTeZd5cpnIkz/Gn6iK0jDfGicM1nynOkkPIl28=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

0 comments on commit 3f1070f

Please sign in to comment.