Skip to content

Commit

Permalink
clone and build dlv instead of go install
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Luhring <[email protected]>
  • Loading branch information
luhring authored and imjasonh committed Jun 11, 2024
1 parent 9bafc92 commit 1c83f79
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
43 changes: 30 additions & 13 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,15 @@ func doesPlatformSupportDebugging(platform v1.Platform) bool {
}

func getDelve(ctx context.Context, platform v1.Platform) (string, error) {
const delveCloneURL = "https://github.com/go-delve/delve.git"

if platform.OS == "" || platform.Architecture == "" {
return "", fmt.Errorf("platform os (%q) or arch (%q) is empty",
platform.OS,
platform.Architecture,
)
}

env, err := buildEnv(platform, os.Environ(), nil)
if err != nil {
return "", fmt.Errorf("could not create env for Delve build: %w", err)
Expand All @@ -303,13 +306,26 @@ func getDelve(ctx context.Context, platform v1.Platform) (string, error) {
if err != nil {
return "", fmt.Errorf("could not create tmp dir for Delve installation: %w", err)
}
cloneDir := filepath.Join(tmpInstallDir, "delve")
err = os.MkdirAll(cloneDir, 0755)
if err != nil {
return "", fmt.Errorf("making dir for delve clone: %v", err)
}
err = git.Clone(ctx, cloneDir, delveCloneURL)
if err != nil {
return "", fmt.Errorf("cloning delve repo: %v", err)
}
osArchDir := fmt.Sprintf("%s_%s", platform.OS, platform.Architecture)
delveBinaryPath := filepath.Join(tmpInstallDir, "bin", osArchDir, "dlv")

// install delve to tmp directory
env = append(env, fmt.Sprintf("GOPATH=%s", tmpInstallDir))

args := []string{
"install",
"github.com/go-delve/delve/cmd/dlv@latest",
"build",
"-C",
cloneDir,
"./cmd/dlv",
"-o",
delveBinaryPath,
}

gobin := getGoBinary()
Expand All @@ -326,17 +342,11 @@ func getDelve(ctx context.Context, platform v1.Platform) (string, error) {
return "", fmt.Errorf("go build Delve: %w: %s", err, output.String())
}

// find the delve binary in tmpInstallDir/bin/
osArchDir := ""
if platform.OS != runtime.GOOS || platform.Architecture != runtime.GOARCH {
osArchDir = fmt.Sprintf("%s_%s", platform.OS, platform.Architecture)
}
delveBinary := filepath.Join(tmpInstallDir, "bin", osArchDir, "dlv")
if _, err := os.Stat(delveBinary); err != nil {
return "", fmt.Errorf("could not find Delve binary at %q: %w", delveBinary, err)
if _, err := os.Stat(delveBinaryPath); err != nil {
return "", fmt.Errorf("could not find Delve binary at %q: %w", delveBinaryPath, err)
}

return delveBinary, nil
return delveBinaryPath, nil
}

func build(ctx context.Context, buildCtx buildContext) (string, error) {
Expand Down Expand Up @@ -941,6 +951,13 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
return nil, err
}
if platform == nil {
if cf.OS == "" {
cf.OS = "linux"
}
if cf.Architecture == "" {
cf.Architecture = "amd64"
}

platform = &v1.Platform{
OS: cf.OS,
Architecture: cf.Architecture,
Expand Down
2 changes: 1 addition & 1 deletion pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,7 +1465,7 @@ func TestDebugger(t *testing.T) {
context.Background(),
"",
WithBaseImages(func(context.Context, string) (name.Reference, Result, error) { return baseRef, base, nil }),
WithPlatforms("all"),
WithPlatforms("linux/amd64"),
WithDebugger(),
)
if err != nil {
Expand Down
25 changes: 25 additions & 0 deletions pkg/internal/git/clone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package git

import (
"context"
"fmt"
"os/exec"
)

// Clone the git repository from the repoURL to the specified dir.
func Clone(ctx context.Context, dir string, repoURL string) error {
rc := runConfig{
dir: dir,
args: []string{"clone", "--depth", "1", repoURL},
}

cmd := exec.CommandContext(ctx, "git", "clone", repoURL, dir)
cmd.Dir = dir

_, err := run(ctx, rc)
if err != nil {
return fmt.Errorf("running git clone: %v", err)
}

return nil
}

0 comments on commit 1c83f79

Please sign in to comment.