From 1e0e754c3dd11041d63a426b1cdf00dfc3ab60f3 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 20 Nov 2024 21:16:59 +0800 Subject: [PATCH 1/5] fix ProjectRoot injection --- cmd/internal/rungo/run.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/cmd/internal/rungo/run.go b/cmd/internal/rungo/run.go index 2b4e540..faeb1ce 100644 --- a/cmd/internal/rungo/run.go +++ b/cmd/internal/rungo/run.go @@ -139,10 +139,7 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s result := make([]string, 0, len(args)) // Prepare the -X flags we want to add - var xFlags []string - if pythonHome != "" { - xFlags = append(xFlags, fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot)) - } + xFlags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot) // Prepare rpath flag if needed var rpathFlag string @@ -179,10 +176,7 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s } // Combine all flags - var allFlags []string - if len(xFlags) > 0 { - allFlags = append(allFlags, xFlags...) - } + allFlags := []string{xFlags} if strings.TrimSpace(existingFlags) != "" { allFlags = append(allFlags, existingFlags) } @@ -203,8 +197,7 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s // If no existing -ldflags found, add new ones at the beginning if we have any flags to add if !foundLDFlags { if len(xFlags) > 0 || rpathFlag != "" { - var allFlags []string - allFlags = append(allFlags, xFlags...) + allFlags := []string{xFlags} if rpathFlag != "" { allFlags = append(allFlags, rpathFlag) } From 68f90fd44d479cf50f13b7703c4515270c449fe7 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 20 Nov 2024 21:17:55 +0800 Subject: [PATCH 2/5] panic when load env failed --- inject.go | 6 ++---- internal/env/env.go | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/inject.go b/inject.go index b35b2a1..5bb660f 100644 --- a/inject.go +++ b/inject.go @@ -11,13 +11,11 @@ var ProjectRoot string func init() { if ProjectRoot == "" { - fmt.Fprintf(os.Stderr, "ProjectRoot is not set\n") - return + panic("ProjectRoot is not set, compile with -ldflags '-X github.com/cpunion/go-python.ProjectRoot=/path/to/project/.deps'") } envs, err := env.ReadEnv(ProjectRoot) if err != nil { - fmt.Fprintf(os.Stderr, "Failed to read env: %s\n", err) - return + panic(fmt.Sprintf("Failed to read env: %s", err)) } for key, value := range envs { os.Setenv(key, value) diff --git a/internal/env/env.go b/internal/env/env.go index c9bc188..ab2e7b9 100644 --- a/internal/env/env.go +++ b/internal/env/env.go @@ -133,7 +133,7 @@ func ReadEnvFile(projectDir string) (map[string]string, error) { envFile := GetEnvConfigPath(projectDir) content, err := os.ReadFile(envFile) if err != nil { - return nil, fmt.Errorf("failed to read env file: %v", err) + return nil, fmt.Errorf("failed to read env file %s: %v", envFile, err) } envs := map[string]string{} for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") { From 5247c5f1bfc659bf97abe9314a8f73174de8579d Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 20 Nov 2024 22:57:14 +0800 Subject: [PATCH 3/5] add inject debug log --- inject.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/inject.go b/inject.go index 5bb660f..d5d3948 100644 --- a/inject.go +++ b/inject.go @@ -10,13 +10,24 @@ import ( var ProjectRoot string func init() { + injectDebug := os.Getenv("GP_INJECT_DEBUG") if ProjectRoot == "" { - panic("ProjectRoot is not set, compile with -ldflags '-X github.com/cpunion/go-python.ProjectRoot=/path/to/project/.deps'") + if injectDebug != "" { + panic("ProjectRoot is not set, compile with -ldflags '-X github.com/cpunion/go-python.ProjectRoot=/path/to/project'") + } + return } envs, err := env.ReadEnv(ProjectRoot) if err != nil { panic(fmt.Sprintf("Failed to read env: %s", err)) } + if injectDebug != "" { + fmt.Fprintf(os.Stderr, "Injecting envs for project: %s\n", ProjectRoot) + for key, value := range envs { + fmt.Fprintf(os.Stderr, " %s=%s\n", key, value) + } + fmt.Fprintf(os.Stderr, "End of envs\n") + } for key, value := range envs { os.Setenv(key, value) } From 6c6dc2b420fcc73dacb48e96d86e7690dc6e0090 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 20 Nov 2024 22:57:54 +0800 Subject: [PATCH 4/5] rewrite ldflags process --- cmd/internal/rungo/run.go | 67 ++++++++++----------------------------- 1 file changed, 16 insertions(+), 51 deletions(-) diff --git a/cmd/internal/rungo/run.go b/cmd/internal/rungo/run.go index faeb1ce..f0dba57 100644 --- a/cmd/internal/rungo/run.go +++ b/cmd/internal/rungo/run.go @@ -139,33 +139,23 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s result := make([]string, 0, len(args)) // Prepare the -X flags we want to add - xFlags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot) + ldflags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot) // Prepare rpath flag if needed - var rpathFlag string - if pythonHome != "" { - pythonLibDir := filepath.Join(pythonHome, "lib") - switch runtime.GOOS { - case "darwin", "linux": - rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath,%s'", pythonLibDir) - case "windows": - // Windows doesn't use rpath - rpathFlag = "" - default: - // Use Linux format for other Unix-like systems - rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath=%s'", pythonLibDir) - } + pythonLibDir := env.GetPythonLibDir(projectRoot) + switch runtime.GOOS { + case "darwin", "linux": + ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath,%s'", pythonLibDir) + case "windows": + // Windows doesn't use rpath + default: + // Use Linux format for other Unix-like systems + ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath=%s'", pythonLibDir) } - // Find existing -ldflags if any - foundLDFlags := false for i := 0; i < len(args); i++ { arg := args[i] if strings.HasPrefix(arg, "-ldflags=") || arg == "-ldflags" { - foundLDFlags = true - // Copy everything before this arg - result = append(result, args[:i]...) - // Get existing flags var existingFlags string if strings.HasPrefix(arg, "-ldflags=") { @@ -174,40 +164,15 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s existingFlags = args[i+1] i++ // Skip the next arg since we've consumed it } - - // Combine all flags - allFlags := []string{xFlags} - if strings.TrimSpace(existingFlags) != "" { - allFlags = append(allFlags, existingFlags) - } - if rpathFlag != "" { - allFlags = append(allFlags, rpathFlag) - } - - // Add combined ldflags - result = append(result, "-ldflags") - result = append(result, strings.Join(allFlags, " ")) - - // Add remaining args - result = append(result, args[i+1:]...) - break - } - } - - // If no existing -ldflags found, add new ones at the beginning if we have any flags to add - if !foundLDFlags { - if len(xFlags) > 0 || rpathFlag != "" { - allFlags := []string{xFlags} - if rpathFlag != "" { - allFlags = append(allFlags, rpathFlag) + existingFlags = strings.TrimSpace(existingFlags) + if ldflags != "" { + ldflags += " " + existingFlags } - result = append(result, "-ldflags") - result = append(result, strings.Join(allFlags, " ")) + } else { + result = append(result, arg) } - result = append(result, args...) } - - return result + return append([]string{"-ldflags", ldflags}, result...) } // GetGoCommandHelp returns the formatted help text for the specified go command From d3c2025ef2445a64b0c5846e4c68f0ed9fa5b9e8 Mon Sep 17 00:00:00 2001 From: Li Jie Date: Wed, 20 Nov 2024 22:59:09 +0800 Subject: [PATCH 5/5] add debug info to ci --- .github/workflows/go.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 26665a3..890f7e2 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -95,13 +95,15 @@ jobs: run: go install -v ./... - name: Test with coverage - run: go test -v -coverprofile=coverage.txt -covermode=atomic ./... + run: go test -coverprofile=coverage.txt -covermode=atomic ./... - name: Test gopy run: | + set -x gopy init $HOME/foo cd $HOME/foo gopy build -v . + export GP_INJECT_DEBUG=1 gopy run -v . gopy install -v .