Skip to content

Commit

Permalink
Make JIT debug logs more verbose
Browse files Browse the repository at this point in the history
  • Loading branch information
Anonymous committed Jan 11, 2024
1 parent 7e1d084 commit 64f9710
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
23 changes: 20 additions & 3 deletions jit/go_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -111,6 +112,9 @@ type PackageError struct {
}

func GoModDownload(goCmd, workDir string, verbose bool, args ...string) error {
if verbose {
args = append([]string{"-x"}, args...)
}
dlCmd := exec.Command(goCmd, append([]string{"mod", "download"}, args...)...)
if verbose {
dlCmd.Stdout = os.Stdout
Expand All @@ -135,7 +139,11 @@ func GoModDownload(goCmd, workDir string, verbose bool, args ...string) error {
}

func GoGet(goCmd, packagePath, workDir string, verbose bool) error {
goGetCmd := exec.Command(goCmd, "get", packagePath)
var args = []string{"get"}
if verbose {
args = append(args, "-x")
}
goGetCmd := exec.Command(goCmd, append(args, packagePath)...)
goGetCmd.Dir = workDir
if verbose {
goGetCmd.Stderr = os.Stderr
Expand Down Expand Up @@ -170,13 +178,22 @@ func GoListStd(goCmd string) map[string]struct{} {
}

func GoList(goCmd, absPath, workDir string, verbose bool) (*Package, error) {
golistCmd := exec.Command(goCmd, "list", "-json", absPath)
args := []string{"list", "-json"}
if verbose {
args = append(args, "-x")
}
args = append(args, absPath)
golistCmd := exec.Command(goCmd, args...)
golistCmd.Dir = workDir

stdoutBuf, stdErrBuf := &bytes.Buffer{}, &bytes.Buffer{}

if verbose {
golistCmd.Stderr = io.MultiWriter(stdErrBuf, os.Stderr)
} else {
golistCmd.Stderr = stdErrBuf
}
golistCmd.Stdout = stdoutBuf
golistCmd.Stderr = stdErrBuf
pkg := Package{}

err := golistCmd.Run()
Expand Down
68 changes: 66 additions & 2 deletions jit/jit.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,20 +488,36 @@ func BuildGoFiles(config BuildConfig, pathToGoFile string, extraFiles ...string)
if err != nil {
return nil, fmt.Errorf("failed to patch gc: %w", err)
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s'\n", absPath)
}

pkg, err := GoList(config.GoBinary, absPath, workDir, config.DebugLog)
if err != nil {
return nil, err
}

if len(pkg.DepsErrors) > 0 {
if config.DebugLog {
log.Printf("Executing 'go mod download -x'\n")
}

err = GoModDownload(config.GoBinary, workDir, config.DebugLog)
if err != nil {
return nil, err
}
if config.DebugLog {
log.Printf("Executing 'go get -x %s'\n", workDir)
}
err = GoGet(config.GoBinary, workDir, workDir, config.DebugLog)
if err != nil {
return nil, err
}
if config.DebugLog {
log.Printf("Executing 'go list -json -x %s' (again)\n", absPath)
}

pkg, err = GoList(config.GoBinary, absPath, "", config.DebugLog)
if err != nil {
return nil, err
Expand Down Expand Up @@ -573,9 +589,10 @@ func BuildGoText(config BuildConfig, goText string) (*LoadableUnit, error) {
}
config.TmpDir = absPathBuildDir
}
buildDir, err := os.MkdirTemp(config.TmpDir, "jit_*")
buildDir := filepath.Join(config.TmpDir, fmt.Sprintf("jit_%s", hexHash))
err := os.MkdirAll(buildDir, os.ModePerm)
if err != nil {
return nil, fmt.Errorf("could not create new tmp directory: %w", err)
return nil, fmt.Errorf("could not create new tmp directory %s: %w", buildDir, err)
}

if !config.KeepTempFiles {
Expand All @@ -596,12 +613,21 @@ func BuildGoText(config BuildConfig, goText string) (*LoadableUnit, error) {
if err != nil {
return nil, fmt.Errorf("failed to patch gc: %w", err)
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s'\n", tmpFilePath)
}

pkg, err := GoList(config.GoBinary, tmpFilePath, "", config.DebugLog)
if err != nil {
return nil, err
}

if len(pkg.DepsErrors) > 0 {
if config.DebugLog {
log.Printf("Executing 'go mod download -x'\n")
}

err = GoModDownload(config.GoBinary, buildDir, config.DebugLog)
if err != nil {
return nil, err
Expand All @@ -611,10 +637,18 @@ func BuildGoText(config BuildConfig, goText string) (*LoadableUnit, error) {
return nil, fmt.Errorf("could not get absolute path of directory containing file %s: %w", tmpFilePath, err)
}

if config.DebugLog {
log.Printf("Executing 'go get -x %s'\n", absPackagePath)
}

err = GoGet(config.GoBinary, absPackagePath, "", config.DebugLog)
if err != nil {
return nil, err
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s' (again)\n", tmpFilePath)
}
pkg, err = GoList(config.GoBinary, tmpFilePath, "", config.DebugLog)
if err != nil {
return nil, err
Expand Down Expand Up @@ -665,6 +699,10 @@ func BuildGoPackage(config BuildConfig, pathToGoPackage string) (*LoadableUnit,
if err != nil {
return nil, fmt.Errorf("failed to patch gc: %w", err)
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s'\n", absPath)
}
// Execute list from within the package folder so that go list resolves the module correctly from that path
pkg, err := GoList(config.GoBinary, absPath, absPath, config.DebugLog)
if err != nil {
Expand All @@ -676,14 +714,25 @@ func BuildGoPackage(config BuildConfig, pathToGoPackage string) (*LoadableUnit,
}

if len(pkg.DepsErrors) > 0 {
if config.DebugLog {
log.Printf("Executing 'go mod download -x'\n")
}
err = GoModDownload(config.GoBinary, absPath, config.DebugLog)
if err != nil {
return nil, err
}

if config.DebugLog {
log.Printf("Executing 'go get -x %s'\n", absPath)
}
err = GoGet(config.GoBinary, absPath, absPath, config.DebugLog)
if err != nil {
return nil, err
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s' (again)\n", absPath)
}
pkg, err = GoList(config.GoBinary, absPath, "", config.DebugLog)
if err != nil {
return nil, err
Expand Down Expand Up @@ -774,11 +823,17 @@ func BuildGoPackageRemote(config BuildConfig, goPackage string, version string)
versionSuffix = "@" + version
}

if config.DebugLog {
log.Printf("Executing 'go get -x %s'\n", goPackage+versionSuffix)
}
err = GoGet(config.GoBinary, goPackage+versionSuffix, workDir, config.DebugLog)
if err != nil {
return nil, err
}

if config.DebugLog {
log.Printf("Executing 'go list -json -x %s'\n", goPackage)
}
pkg, err := GoList(config.GoBinary, goPackage, workDir, config.DebugLog)
if err != nil {
return nil, err
Expand All @@ -789,14 +844,23 @@ func BuildGoPackageRemote(config BuildConfig, goPackage string, version string)
}

if len(pkg.DepsErrors) > 0 {
if config.DebugLog {
log.Printf("Executing 'go mod download -x'\n")
}
err = GoModDownload(config.GoBinary, workDir, config.DebugLog, pkg.Module.Path)
if err != nil {
return nil, err
}
if config.DebugLog {
log.Printf("Executing 'go get -x %s'\n", goPackage)
}
err = GoGet(config.GoBinary, goPackage, workDir, config.DebugLog)
if err != nil {
return nil, err
}
if config.DebugLog {
log.Printf("Executing 'go list -json -x %s' (again)\n", goPackage)
}
pkg, err = GoList(config.GoBinary, goPackage, "", config.DebugLog)
if err != nil {
return nil, err
Expand Down

0 comments on commit 64f9710

Please sign in to comment.