Skip to content

Commit

Permalink
Merge pull request #29 from cpunion/cli
Browse files Browse the repository at this point in the history
fix ProjectRoot injection, panic if injection failed
  • Loading branch information
cpunion authored Nov 20, 2024
2 parents 87c68df + d3c2025 commit 62f4187
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 63 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand Down
74 changes: 16 additions & 58 deletions cmd/internal/rungo/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=") {
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 62f4187

Please sign in to comment.