Skip to content

Commit 62f4187

Browse files
authored
Merge pull request #29 from cpunion/cli
fix ProjectRoot injection, panic if injection failed
2 parents 87c68df + d3c2025 commit 62f4187

File tree

4 files changed

+32
-63
lines changed

4 files changed

+32
-63
lines changed

.github/workflows/go.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ jobs:
9595
run: go install -v ./...
9696

9797
- name: Test with coverage
98-
run: go test -v -coverprofile=coverage.txt -covermode=atomic ./...
98+
run: go test -coverprofile=coverage.txt -covermode=atomic ./...
9999

100100
- name: Test gopy
101101
run: |
102+
set -x
102103
gopy init $HOME/foo
103104
cd $HOME/foo
104105
gopy build -v .
106+
export GP_INJECT_DEBUG=1
105107
gopy run -v .
106108
gopy install -v .
107109

cmd/internal/rungo/run.go

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -139,36 +139,23 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s
139139
result := make([]string, 0, len(args))
140140

141141
// Prepare the -X flags we want to add
142-
var xFlags []string
143-
if pythonHome != "" {
144-
xFlags = append(xFlags, fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot))
145-
}
142+
ldflags := fmt.Sprintf("-X 'github.com/cpunion/go-python.ProjectRoot=%s'", projectRoot)
146143

147144
// Prepare rpath flag if needed
148-
var rpathFlag string
149-
if pythonHome != "" {
150-
pythonLibDir := filepath.Join(pythonHome, "lib")
151-
switch runtime.GOOS {
152-
case "darwin", "linux":
153-
rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath,%s'", pythonLibDir)
154-
case "windows":
155-
// Windows doesn't use rpath
156-
rpathFlag = ""
157-
default:
158-
// Use Linux format for other Unix-like systems
159-
rpathFlag = fmt.Sprintf("-extldflags '-Wl,-rpath=%s'", pythonLibDir)
160-
}
145+
pythonLibDir := env.GetPythonLibDir(projectRoot)
146+
switch runtime.GOOS {
147+
case "darwin", "linux":
148+
ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath,%s'", pythonLibDir)
149+
case "windows":
150+
// Windows doesn't use rpath
151+
default:
152+
// Use Linux format for other Unix-like systems
153+
ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath=%s'", pythonLibDir)
161154
}
162155

163-
// Find existing -ldflags if any
164-
foundLDFlags := false
165156
for i := 0; i < len(args); i++ {
166157
arg := args[i]
167158
if strings.HasPrefix(arg, "-ldflags=") || arg == "-ldflags" {
168-
foundLDFlags = true
169-
// Copy everything before this arg
170-
result = append(result, args[:i]...)
171-
172159
// Get existing flags
173160
var existingFlags string
174161
if strings.HasPrefix(arg, "-ldflags=") {
@@ -177,44 +164,15 @@ func ProcessArgsWithLDFlags(args []string, projectRoot, pythonPath, pythonHome s
177164
existingFlags = args[i+1]
178165
i++ // Skip the next arg since we've consumed it
179166
}
180-
181-
// Combine all flags
182-
var allFlags []string
183-
if len(xFlags) > 0 {
184-
allFlags = append(allFlags, xFlags...)
185-
}
186-
if strings.TrimSpace(existingFlags) != "" {
187-
allFlags = append(allFlags, existingFlags)
188-
}
189-
if rpathFlag != "" {
190-
allFlags = append(allFlags, rpathFlag)
191-
}
192-
193-
// Add combined ldflags
194-
result = append(result, "-ldflags")
195-
result = append(result, strings.Join(allFlags, " "))
196-
197-
// Add remaining args
198-
result = append(result, args[i+1:]...)
199-
break
200-
}
201-
}
202-
203-
// If no existing -ldflags found, add new ones at the beginning if we have any flags to add
204-
if !foundLDFlags {
205-
if len(xFlags) > 0 || rpathFlag != "" {
206-
var allFlags []string
207-
allFlags = append(allFlags, xFlags...)
208-
if rpathFlag != "" {
209-
allFlags = append(allFlags, rpathFlag)
167+
existingFlags = strings.TrimSpace(existingFlags)
168+
if ldflags != "" {
169+
ldflags += " " + existingFlags
210170
}
211-
result = append(result, "-ldflags")
212-
result = append(result, strings.Join(allFlags, " "))
171+
} else {
172+
result = append(result, arg)
213173
}
214-
result = append(result, args...)
215174
}
216-
217-
return result
175+
return append([]string{"-ldflags", ldflags}, result...)
218176
}
219177

220178
// GetGoCommandHelp returns the formatted help text for the specified go command

inject.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,23 @@ import (
1010
var ProjectRoot string
1111

1212
func init() {
13+
injectDebug := os.Getenv("GP_INJECT_DEBUG")
1314
if ProjectRoot == "" {
14-
fmt.Fprintf(os.Stderr, "ProjectRoot is not set\n")
15+
if injectDebug != "" {
16+
panic("ProjectRoot is not set, compile with -ldflags '-X github.com/cpunion/go-python.ProjectRoot=/path/to/project'")
17+
}
1518
return
1619
}
1720
envs, err := env.ReadEnv(ProjectRoot)
1821
if err != nil {
19-
fmt.Fprintf(os.Stderr, "Failed to read env: %s\n", err)
20-
return
22+
panic(fmt.Sprintf("Failed to read env: %s", err))
23+
}
24+
if injectDebug != "" {
25+
fmt.Fprintf(os.Stderr, "Injecting envs for project: %s\n", ProjectRoot)
26+
for key, value := range envs {
27+
fmt.Fprintf(os.Stderr, " %s=%s\n", key, value)
28+
}
29+
fmt.Fprintf(os.Stderr, "End of envs\n")
2130
}
2231
for key, value := range envs {
2332
os.Setenv(key, value)

internal/env/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func ReadEnvFile(projectDir string) (map[string]string, error) {
133133
envFile := GetEnvConfigPath(projectDir)
134134
content, err := os.ReadFile(envFile)
135135
if err != nil {
136-
return nil, fmt.Errorf("failed to read env file: %v", err)
136+
return nil, fmt.Errorf("failed to read env file %s: %v", envFile, err)
137137
}
138138
envs := map[string]string{}
139139
for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") {

0 commit comments

Comments
 (0)