From 46447dc30a8311dbb486ffdf873ce755d0d2e6d3 Mon Sep 17 00:00:00 2001 From: Aofei Sheng Date: Tue, 9 Jul 2024 15:04:46 +0800 Subject: [PATCH] cmd: change "llgo run" to use a temp dir for binaries Changed "llgo run" to generate binaries in a temp dir instead of GOBIN. The temp dir is automatically removed after execution, mimicking the behavior of "go run". --- cmd/internal/run/run.go | 10 ++++++++++ internal/build/build.go | 1 + 2 files changed, 11 insertions(+) diff --git a/cmd/internal/run/run.go b/cmd/internal/run/run.go index 4d314cde4..4b4fe1f42 100644 --- a/cmd/internal/run/run.go +++ b/cmd/internal/run/run.go @@ -19,6 +19,8 @@ package run import ( "errors" + "fmt" + "os" "path/filepath" "github.com/goplus/llgo/cmd/internal/base" @@ -57,8 +59,16 @@ func runCmpTest(cmd *base.Command, args []string) { func runCmdEx(cmd *base.Command, args []string, mode build.Mode) { args, runArgs, err := parseRunArgs(args) check(err) + + tempBinPath, err := os.MkdirTemp("", "llgo-run-") + if err != nil { + panic(fmt.Errorf("os.MkdirTemp failed: %v", err)) + } + defer os.RemoveAll(tempBinPath) // NOTE: this will not work if os.Exit is called; remove it manually if needed + conf := build.NewDefaultConf(mode) conf.RunArgs = runArgs + conf.BinPath = tempBinPath build.Do(args, conf) } diff --git a/internal/build/build.go b/internal/build/build.go index b920cfcf0..73c5eaa92 100644 --- a/internal/build/build.go +++ b/internal/build/build.go @@ -401,6 +401,7 @@ func linkMainPkg(pkg *packages.Package, pkgs []*aPackage, llFiles []string, conf cmd.Stderr = os.Stderr cmd.Run() if s := cmd.ProcessState; s != nil { + os.RemoveAll(conf.BinPath) // remove temp dir before exit os.Exit(s.ExitCode()) } case ModeCmpTest: