Skip to content

Commit

Permalink
Merge branch 'main' into fixforplan9
Browse files Browse the repository at this point in the history
  • Loading branch information
hugelgupf authored Jan 9, 2022
2 parents dca5793 + fcf634c commit 70913fd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,10 @@ potential conflict and potential solutions you can enact in your code:
replace github.com/insomniacslk/dhcp => ../local/dhcp
> u-bmc/go.mod
require github.com/insomniacslk/dhcp/v2 v2.0.0.-...
require github.com/insomniacslk/dhcp v1.0.2
```

u-root has replaced dhcp, but u-bmc still depends on the remote dhcp/v2.
u-root has replaced dhcp, but u-bmc still depends on the remote dhcp@v1.0.2.

**Solution**: u-root drops local replace rule, or `u-bmc/go.mod` needs
`replace github.com/insomniacslk/dhcp => $samedir/local/dhcp` as well.
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/makebb/makebb.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
flag.Parse()

// Why doesn't the log package export this as a default?
l := log.New(os.Stdout, "", log.LstdFlags)
l := log.New(os.Stdout, "", log.Ltime)

o, err := filepath.Abs(*outputPath)
if err != nil {
Expand Down Expand Up @@ -62,7 +62,7 @@ func main() {
GoBuildOpts: bopts,
GenerateOnly: *genOnly,
}
if err := bb.BuildBusybox(opts); err != nil {
if err := bb.BuildBusybox(l, opts); err != nil {
l.Print(err)
var errGopath *bb.ErrGopathBuild
var errGomod *bb.ErrModuleBuild
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",
"@com_github_u_root_uio//ulog",
"@org_golang_x_mod//modfile",
"@org_golang_x_tools//go/ast/astutil",
"@org_golang_x_tools//go/packages",
Expand Down
32 changes: 16 additions & 16 deletions src/pkg/bb/bb.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
Expand All @@ -43,6 +42,7 @@ import (
"github.com/u-root/gobusybox/src/pkg/bb/findpkg"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/uio/cp"
"github.com/u-root/uio/ulog"
)

func listStrings(m map[string]struct{}) []string {
Expand Down Expand Up @@ -107,7 +107,7 @@ type Opts struct {
//
// For documentation on how this works, please refer to the README at the top
// of the repository.
func BuildBusybox(opts *Opts) (nerr error) {
func BuildBusybox(l ulog.Logger, opts *Opts) (nerr error) {
if opts == nil {
return fmt.Errorf("no options given for busybox build")
} else if err := opts.Env.Valid(); err != nil {
Expand Down Expand Up @@ -146,7 +146,7 @@ func BuildBusybox(opts *Opts) (nerr error) {
}
defer func() {
if nerr != nil {
log.Printf("Preserving bb generated source directory at %s due to error", tmpDir)
l.Printf("Preserving bb generated source directory at %s due to error", tmpDir)
} else {
os.RemoveAll(tmpDir)
}
Expand All @@ -160,7 +160,7 @@ func BuildBusybox(opts *Opts) (nerr error) {
pkgDir := filepath.Join(tmpDir, "src")

// Ask go about all the commands in one batch for dependency caching.
cmds, err := findpkg.NewPackages(opts.Env, opts.CommandPaths...)
cmds, err := findpkg.NewPackages(l, opts.Env, opts.CommandPaths...)
if err != nil {
return fmt.Errorf("finding packages failed: %v", err)
}
Expand Down Expand Up @@ -199,7 +199,7 @@ func BuildBusybox(opts *Opts) (nerr error) {
}

// Collect and write dependencies into pkgDir.
if err := copyLocalDeps(opts.Env, bbDir, tmpDir, pkgDir, cmds); err != nil {
if err := copyLocalDeps(l, opts.Env, bbDir, tmpDir, pkgDir, cmds); err != nil {
return fmt.Errorf("collecting and putting dependencies in place failed: %v", err)
}

Expand Down Expand Up @@ -395,7 +395,7 @@ func copyLocalGoMods(pkgDir, bbDir string, modules map[string]*packages.Module)
// than another). Conflicts generally only arise when a module is replaced by a
// local directory, as Go takes care of other conflicts using
// minimum-version-selection (MVS).
func findLocalModules(mainPkgs []*bbinternal.Package) (map[string]*packages.Module, error) {
func findLocalModules(l ulog.Logger, mainPkgs []*bbinternal.Package) (map[string]*packages.Module, error) {
type localModule struct {
m *packages.Module
provenance string
Expand All @@ -410,7 +410,7 @@ func findLocalModules(mainPkgs []*bbinternal.Package) (map[string]*packages.Modu
if _, ok := localModules[p.Pkg.Module.Path]; !ok {
localModules[p.Pkg.Module.Path] = &localModule{
m: p.Pkg.Module,
provenance: fmt.Sprintf("your request to compile %s", p.Pkg.PkgPath),
provenance: p.Pkg.PkgPath,
}
}
}
Expand Down Expand Up @@ -487,17 +487,17 @@ func findLocalModules(mainPkgs []*bbinternal.Package) (map[string]*packages.Modu
return
}

if l, ok := localModules[p.Module.Path]; ok && l.m.Dir != p.Module.Dir {
if lm, ok := localModules[p.Module.Path]; ok && lm.m.Dir != p.Module.Dir {
fmt.Fprintln(os.Stderr, "")
log.Printf("Conflicting module dependencies on %s:", p.Module.Path)
log.Printf(" module %s depends on it @ %s", mainPkg.Pkg.Module.Path, moduleVersionIdentifier(p.Module))
log.Printf(" %s depends on it @ %s", l.provenance, moduleVersionIdentifier(l.m))
replacePath, err := filepath.Rel(mainPkg.Pkg.Module.Dir, l.m.Dir)
l.Printf("Conflicting module dependencies on %s:", p.Module.Path)
l.Printf(" %s depends on %s @ %s", mainPkg.Pkg.PkgPath, p.Module.Path, moduleVersionIdentifier(p.Module))
l.Printf(" %s depends on %s @ %s", lm.provenance, lm.m.Path, moduleVersionIdentifier(lm.m))
replacePath, err := filepath.Rel(mainPkg.Pkg.Module.Dir, lm.m.Dir)
if err != nil {
replacePath = l.m.Dir
replacePath = lm.m.Dir
}
fmt.Fprintln(os.Stderr, "")
log.Printf("%s: add `replace %s => %s` to %s", term.Bold("Suggestion to resolve"), p.Module.Path, replacePath, mainPkg.Pkg.Module.GoMod)
l.Printf("%s: add `replace %s => %s` to %s", term.Bold("Suggestion to resolve"), p.Module.Path, replacePath, mainPkg.Pkg.Module.GoMod)
fmt.Fprintln(os.Stderr, "")
conflict = true

Expand Down Expand Up @@ -561,8 +561,8 @@ func moduleVersionIdentifier(m *packages.Module) string {
//
// Then, in the generated tree's main module, we create a go.mod file with
// replace directives for all the local modules we just copied over.
func copyLocalDeps(env golang.Environ, bbDir, tmpDir, pkgDir string, mainPkgs []*bbinternal.Package) error {
localModules, err := findLocalModules(mainPkgs)
func copyLocalDeps(l ulog.Logger, env golang.Environ, bbDir, tmpDir, pkgDir string, mainPkgs []*bbinternal.Package) error {
localModules, err := findLocalModules(l, mainPkgs)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions src/pkg/bb/findpkg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"//src/pkg/bb/bbinternal",
"//src/pkg/golang",
"@com_github_u_root_uio//ulog",
"@org_golang_x_sys//unix",
"@org_golang_x_tools//go/packages",
],
Expand Down
21 changes: 11 additions & 10 deletions src/pkg/bb/findpkg/bb.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/u-root/gobusybox/src/pkg/bb/bbinternal"
"github.com/u-root/gobusybox/src/pkg/golang"
"github.com/u-root/uio/ulog"
)

// modules returns a list of module directories => directories of packages
Expand Down Expand Up @@ -60,7 +61,7 @@ func modules(filesystemPaths []string) (map[string][]string, []string) {
//
// Namely, PWD determines which go.mod to use. We want each
// package to use its own go.mod, if it has one.
func loadFSPackages(env golang.Environ, filesystemPaths []string) ([]*packages.Package, error) {
func loadFSPackages(l ulog.Logger, env golang.Environ, filesystemPaths []string) ([]*packages.Package, error) {
var absPaths []string
for _, fsPath := range filesystemPaths {
absPath, err := filepath.Abs(fsPath)
Expand All @@ -86,7 +87,7 @@ func loadFSPackages(env golang.Environ, filesystemPaths []string) ([]*packages.P
return nil, fmt.Errorf("could not find packages in module %s: %v", moduleDir, err)
}
for _, pkg := range pkgs {
allps = addPkg(allps, pkg)
allps = addPkg(l, allps, pkg)
}
}

Expand All @@ -98,21 +99,21 @@ func loadFSPackages(env golang.Environ, filesystemPaths []string) ([]*packages.P
return nil, fmt.Errorf("could not find packages: %v", err)
}
for _, p := range vendoredPkgs {
allps = addPkg(allps, p)
allps = addPkg(l, allps, p)
}
}
return allps, nil
}

func addPkg(plist []*packages.Package, p *packages.Package) []*packages.Package {
func addPkg(l ulog.Logger, plist []*packages.Package, p *packages.Package) []*packages.Package {
if len(p.Errors) > 0 {
// TODO(chrisko): should we return an error here instead of warn?
log.Printf("Skipping package %v for errors:", p)
l.Printf("Skipping package %v for errors:", p)
packages.PrintErrors([]*packages.Package{p})
} else if len(p.GoFiles) == 0 {
log.Printf("Skipping package %v because it has no Go files", p)
l.Printf("Skipping package %v because it has no Go files", p)
} else if p.Name != "main" {
log.Printf("Skipping package %v because it is not a command (must be `package main`)", p)
l.Printf("Skipping package %v because it is not a command (must be `package main`)", p)
} else {
plist = append(plist, p)
}
Expand All @@ -122,7 +123,7 @@ func addPkg(plist []*packages.Package, p *packages.Package) []*packages.Package
// NewPackages collects package metadata about all named packages.
//
// names can either be directory paths or Go import paths.
func NewPackages(env golang.Environ, names ...string) ([]*bbinternal.Package, error) {
func NewPackages(l ulog.Logger, env golang.Environ, names ...string) ([]*bbinternal.Package, error) {
var goImportPaths []string
var filesystemPaths []string

Expand All @@ -143,11 +144,11 @@ func NewPackages(env golang.Environ, names ...string) ([]*bbinternal.Package, er
return nil, fmt.Errorf("failed to load package %v: %v", goImportPaths, err)
}
for _, p := range importPkgs {
ps = addPkg(ps, p)
ps = addPkg(l, ps, p)
}
}

pkgs, err := loadFSPackages(env, filesystemPaths)
pkgs, err := loadFSPackages(l, env, filesystemPaths)
if err != nil {
return nil, fmt.Errorf("could not load packages from file system: %v", err)
}
Expand Down

0 comments on commit 70913fd

Please sign in to comment.