Skip to content
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
54 changes: 37 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -135,6 +136,40 @@ func runTime(cmd *exec.Cmd) error {
return cmd.Run()
}

func gorun(srcFilename string, env []string, buildDir string, runDir string, args ...string) error {
exeDir, err := os.MkdirTemp("", "goeval*")
if err != nil {
return err
}
defer func() {
if err := os.RemoveAll(exeDir); err != nil {
log.Printf("RemoveAll(%q): %v", exeDir, err)
}
}()

exePath := filepath.Join(exeDir, "goeval-run")
if runtime.GOOS == "windows" {
exePath += ".exe"
}

cmdBuild := exec.Command(goCmd, "build", "-o", exePath, srcFilename)
cmdBuild.Env = env
cmdBuild.Dir = buildDir
cmdBuild.Stdout = os.Stdout
cmdBuild.Stderr = os.Stderr
if err := run(cmdBuild); err != nil {
return fmt.Errorf("failed to build: %w", err)
}

cmdRun := exec.Command(exePath, args...)
cmdRun.Env = env
cmdRun.Dir = runDir // In Go module mode we run from the temp module dir
cmdRun.Stdin = os.Stdin
cmdRun.Stdout = os.Stdout
cmdRun.Stderr = os.Stderr
return run(cmdRun)
}

var goCmd = "go"

func getGOMODCACHE(env []string) (string, error) {
Expand Down Expand Up @@ -187,7 +222,7 @@ func flagAction(name string, a actionBits, usage string) {

func _main() error {
imports := imports{
packages: map[string]string{" ": "os"},
packages: map[string]string{},
onlySemVer: true,
}
flag.Var(&imports, "i", "* import package: [alias=]import-path\n* switch to Go module mode and import package: [alias=]import-path@version")
Expand Down Expand Up @@ -366,10 +401,6 @@ func _main() error {
}
src.WriteString("func main() {\n")
if action <= actionDump {
src.WriteString("os.Args[1] = os.Args[0]\nos.Args = os.Args[1:]\n")
if moduleMode {
fmt.Fprintf(&src, "_ = os.Chdir(%q)\n", origDir)
}
src.WriteString("//line :1\n")
}
src.WriteString(code)
Expand All @@ -391,22 +422,11 @@ func _main() error {
srcFinal = f
srcFilename = f.Name()

runArgs := make([]string, 0, 3+len(args))
runArgs = append(runArgs, "run", srcFilename, "--")
runArgs = append(runArgs, args...)
// log.Println(goCmd, runArgs)

cmd := exec.Command(goCmd, runArgs...)
cmd.Env = env
cmd.Dir = dir // In Go module mode we run from the temp module dir
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
tail = func() error {
if err = f.Close(); err != nil {
return err
}
return run(cmd)
return gorun(srcFilename, env, dir, origDir, args...)
}
case actionPlay:
var cleanup func()
Expand Down
7 changes: 1 addition & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,9 @@ func Example_dump() {
// Output:
// package main
//
// import (
// "fmt"
// "os"
// )
// import "fmt"
//
// func main() {
// os.Args[1] = os.Args[0]
// os.Args = os.Args[1:]
// //line :1
// fmt.Println("OK")
// }
Expand Down