Skip to content
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

go.mod gen: use modfile package #49

Merged
merged 1 commit into from
May 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def go_dependencies():
go_repository(
name = "org_golang_x_mod",
importpath = "golang.org/x/mod",
sum = "h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=",
version = "v0.3.0",
sum = "h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=",
version = "v0.4.2",
)
go_repository(
name = "org_golang_x_net",
Expand Down
1 change: 1 addition & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.13
require (
github.com/google/goterm v0.0.0-20200907032337-555d40f16ae2
github.com/u-root/uio v0.0.0-20210528151154-e40b768296a7
golang.org/x/mod v0.4.2
golang.org/x/sys v0.0.0-20210525143221-35b2ab0089ea
golang.org/x/tools v0.0.0-20200904185747-39188db58858
)
3 changes: 2 additions & 1 deletion src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
Expand Down
1 change: 1 addition & 0 deletions src/pkg/bb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ go_library(
"//src/pkg/golang",
"@com_github_google_goterm//term",
"@com_github_u_root_uio//cp",
"@org_golang_x_mod//modfile",
"@org_golang_x_tools//go/ast/astutil",
"@org_golang_x_tools//go/packages",
],
Expand Down
20 changes: 16 additions & 4 deletions src/pkg/bb/bb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"strings"

"github.com/google/goterm/term"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"

Expand Down Expand Up @@ -511,7 +512,9 @@ func dealWithDeps(env golang.Environ, bbDir, tmpDir, pkgDir string, mainPkgs []*
//
// The module name is something that'll never be online, lest Go
// decides to go on the internet.
content := `module bb.u-root.com/bb`
var mod modfile.File

mod.AddModuleStmt("bb.u-root.com/bb")
for mpath, module := range localModules {
v := module.Version
if len(v) == 0 {
Expand All @@ -526,15 +529,24 @@ func dealWithDeps(env golang.Environ, bbDir, tmpDir, pkgDir string, mainPkgs []*
// the most accurate thing.
v = "v0.0.0"
}
content += fmt.Sprintf("\nrequire %s %s\n", mpath, v)
content += fmt.Sprintf("replace %s => ../../%s\n", mpath, mpath)
if err := mod.AddRequire(mpath, v); err != nil {
return fmt.Errorf("could not add requiring %v to go.mod: %v", mpath, err)
}
if err := mod.AddReplace(mpath, "", path.Join("..", "..", mpath), ""); err != nil {
return fmt.Errorf("could not add replace rule for %v to go.mod: %v", mpath, err)
}
}

gomod, err := mod.Format()
if err != nil {
return fmt.Errorf("could not generated go.mod: %v", err)
}

// TODO(chrisko): add other go.mod files' replace and exclude
// directives.
//
// Warn the user if they are potentially incompatible.
if err := ioutil.WriteFile(filepath.Join(bbDir, "go.mod"), []byte(content), 0755); err != nil {
if err := ioutil.WriteFile(filepath.Join(bbDir, "go.mod"), gomod, 0755); err != nil {
return err
}
return nil
Expand Down