Skip to content

Commit

Permalink
integrations/golang: Use full package paths when building.
Browse files Browse the repository at this point in the history
Carefully tracking the path to the built package within the Go module allows us to refer to binaries in a build plan across modules.

Previously given the following specification in example.com/foo/xxx/binary.cue

    build_plan: [{binary: "example.com/bar/yyy"}]

we would try to build a package with the desired local name (yyy), but within the "calling" module (example.com/foo/yyy).
  • Loading branch information
nichtverstehen committed Sep 9, 2024
1 parent 8a5b1ac commit d50397a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
8 changes: 3 additions & 5 deletions internal/integrations/golang/buildkit.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package golang
import (
"context"
"fmt"
"path"
"strings"

"github.com/moby/buildkit/client/llb"
Expand Down Expand Up @@ -73,12 +74,9 @@ func buildUsingBuildkit(ctx context.Context, env pkggraph.SealedContext, bin GoB
goBuild = append(goBuild, quoteArgs(goBuildArgs(version, bin.StripBinary))...)
goBuild = append(goBuild, fmt.Sprintf("-o=/out/%s", bin.BinaryName))

relPath, err := makePkg(bin.GoWorkspacePath, bin.SourcePath)
if err != nil {
return nil, err
}
pkg := path.Join(bin.GoModule, bin.SourcePath)

goBuild = append(goBuild, relPath)
goBuild = append(goBuild, pkg)

state := (llbutil.RunGo{
Base: prepareGoMod(base, src, conf.TargetPlatform()).Root(),
Expand Down
25 changes: 18 additions & 7 deletions internal/integrations/golang/generate.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/integrations/golang/generate.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ message FrameworkExt {

// Relative path to go.work, or to go.mod.
string go_workspace_path = 4;

// Path to the Go package within the module.
string rel_package = 5;
}
9 changes: 7 additions & 2 deletions internal/integrations/golang/gobinary.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type GoBinary struct {
GoWorkspacePath string `json:"workspacePath"`
GoModule string `json:"module"` // Go module name.
GoVersion string `json:"goVersion"`
SourcePath string `json:"sourcePath"` // Relative to ns workspace root.
SourcePath string `json:"sourcePath"` // Relative to GoModule.
BinaryName string `json:"binaryName"`

BinaryOnly bool
Expand Down Expand Up @@ -59,6 +59,11 @@ func FromLocation(loc pkggraph.Location, pkgName string) (*GoBinary, error) {
return nil, err
}

pkgInsideMod, err := filepath.Rel(filepath.Dir(modFile), absSrc)
if err != nil {
return nil, err
}

gowork, _ := findroot.Find("go work", filepath.Dir(modFile), findroot.LookForFile("go.work"))
if gowork == "" {
gowork = filepath.Dir(modFile)
Expand All @@ -73,7 +78,7 @@ func FromLocation(loc pkggraph.Location, pkgName string) (*GoBinary, error) {
PackageName: loc.PackageName,
GoWorkspacePath: relMod,
GoModule: mod.Module.Mod.Path,
SourcePath: loc.Rel(pkgName),
SourcePath: pkgInsideMod,
GoVersion: mod.Go.Version,
}, nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/integrations/golang/golang.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (impl) PrepareBuild(ctx context.Context, _ assets.AvailableBuildAssets, ser
GoWorkspacePath: ext.GoWorkspacePath,
GoModule: ext.GoModule,
GoVersion: ext.GoVersion,
SourcePath: server.Location.Rel(),
SourcePath: ext.RelPackage,
BinaryName: serverName(server),
}

Expand Down Expand Up @@ -222,6 +222,7 @@ func (impl) PreParseServer(ctx context.Context, loc pkggraph.Location, ext *pars
GoVersion: bin.GoVersion,
GoModule: bin.GoModule,
GoWorkspacePath: bin.GoWorkspacePath,
RelPackage: bin.SourcePath,
})
if err != nil {
return err
Expand Down
6 changes: 2 additions & 4 deletions internal/integrations/golang/localbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"

specs "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -100,10 +101,7 @@ func compile(ctx context.Context, sdk golang.LocalSDK, absWorkspace string, targ

modulePath := filepath.Join(absWorkspace, bin.GoWorkspacePath)
out := filepath.Join(targetDir, bin.BinaryName)
pkg, err := makePkg(bin.GoWorkspacePath, bin.SourcePath)
if err != nil {
return err
}
pkg := path.Join(bin.GoModule, bin.SourcePath)

var cmd localexec.Command
cmd.Label = "go build"
Expand Down

0 comments on commit d50397a

Please sign in to comment.