-
Notifications
You must be signed in to change notification settings - Fork 36
Update go dependencies #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Update go dependencies #2008
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4437894
Add script to automatically update deps.
f724337
Run dependency update script.
7fa010c
Add a comment to main.
b87cf07
Disable gosec
04057d6
Comment out gosec build in Makefile.
529d2ce
Merge remote-tracking branch 'origin/master' into jb-update-godeps
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io/ioutil" | ||
"log" | ||
"os/exec" | ||
"time" | ||
|
||
"github.com/rogpeppe/go-internal/modfile" | ||
"github.com/rogpeppe/go-internal/semver" | ||
) | ||
|
||
// Use a custom branch for the following dependencies | ||
var customBranches = map[string]string{ | ||
"github.com/trussworks/pdfcpu": "afero", | ||
} | ||
|
||
// This program exists so that we can work around go mod's MVS behavior. | ||
// For each dependency listed in go.mod, this program will update that dependency | ||
// to either the latest released patch (using go get -u=patch) or the latest commit | ||
// on the master branch based on if we're currently using a tagged version or a | ||
// commit. | ||
// | ||
// There is a special case for pdfcpu, where we need to pull in the latest commit on a | ||
// non-master branch. | ||
func main() { | ||
content, readErr := ioutil.ReadFile("go.mod") | ||
if readErr != nil { | ||
log.Fatal(readErr) | ||
} | ||
|
||
file, parseErr := modfile.Parse("go.mod", content, nil) | ||
if parseErr != nil { | ||
log.Fatal(parseErr) | ||
} | ||
|
||
for _, req := range file.Require { | ||
fmt.Printf("%s", req.Mod.Path) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
args := updateArgs(req) | ||
|
||
out, cmdErr := exec.CommandContext(ctx, "go", args...).CombinedOutput() | ||
if cmdErr != nil { | ||
fmt.Println(" ×") | ||
if ctx.Err() == context.DeadlineExceeded { | ||
log.Fatalf("timed out trying trying to run %s %s", "go", args) | ||
} else { | ||
log.Fatalf("failed to update %s: ran %s %v, got %s", req.Mod.Path, "go", args, string(out)) | ||
} | ||
} | ||
|
||
cancel() | ||
fmt.Println(" ✓") | ||
} | ||
|
||
if output, err := modTidy(); err != nil { | ||
log.Fatalf("failed to run go mod tidy: got %s, error was %s", output, err) | ||
} | ||
} | ||
|
||
func updateArgs(req *modfile.Require) []string { | ||
if semver.Prerelease(req.Mod.Version) == "" { | ||
// Use the latest patch release if we're already using a tagged release | ||
return []string{"get", "-u=patch", req.Mod.Path} | ||
} | ||
|
||
branch := "master" | ||
customBranch, ok := customBranches[req.Mod.Path] | ||
if ok { | ||
branch = customBranch | ||
} | ||
|
||
return []string{"get", req.Mod.Path + "@" + branch} | ||
} | ||
|
||
func modTidy() ([]byte, error) { | ||
return exec.Command("go", "mod", "tidy").CombinedOutput() | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# How to Manage Dependencies With go mod | ||
|
||
[Go modules](https://github.com/golang/go/wiki/Modules) is the built-in dependency system provided by Go. It supersedes [dep](https://golang.github.io/dep/), which we previously used to manage Go dependencies. | ||
|
||
It's important to note that go mod uses a [different dependency resolution algorithm](https://github.com/golang/go/wiki/Modules#version-selection) than many other packaging tools. It will install _oldest_ indirect | ||
dependency (called _minimal version selection_) that will satisfy all direct dependencies, whereas other package managers will tend to install the _newest_. | ||
You can read more about the rationale behind this approach [in the original proposal](https://github.com/golang/proposal/blob/master/design/24301-versioned-go.md#update-timing--high-fidelity-builds). | ||
|
||
For the most part, a developer interacts with `go mod` using `go get`. The other go tools are likewise aware of how to work with go modules. | ||
|
||
## Update all go dependencies | ||
|
||
```console | ||
$ go get -u | ||
``` | ||
|
||
## Update a specific dependency | ||
|
||
```console | ||
$ go get -u github.com/pkg/errors | ||
``` | ||
|
||
## Update a specific dependency to a specific branch | ||
|
||
The following updates `github.com/pkg/errors` to the latest version available on the `master` branch: | ||
|
||
```console | ||
$ go get -u github.com/pkg/errors@master | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to also have an example of getting the dependency
@master
, which I think is the right syntax. I think a lot of our deps we want at the master version.