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 . diff --git a/cmd/internal/rungo/run.go b/cmd/internal/rungo/run.go index 2b4e540..f0dba57 100644 --- a/cmd/internal/rungo/run.go +++ b/cmd/internal/rungo/run.go @@ -139,36 +139,23 @@ 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)) - } + 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=") { @@ -177,44 +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 - var allFlags []string - if len(xFlags) > 0 { - allFlags = append(allFlags, 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 != "" { - var allFlags []string - allFlags = append(allFlags, 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 diff --git a/inject.go b/inject.go index b35b2a1..d5d3948 100644 --- a/inject.go +++ b/inject.go @@ -10,14 +10,23 @@ import ( var ProjectRoot string func init() { + injectDebug := os.Getenv("GP_INJECT_DEBUG") if ProjectRoot == "" { - fmt.Fprintf(os.Stderr, "ProjectRoot is not set\n") + 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 { - fmt.Fprintf(os.Stderr, "Failed to read env: %s\n", err) - return + 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) 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") {