Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ProjectRoot injection, panic if injection failed #29

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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)

Check warning on line 142 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L142

Added line #L142 was not covered by tests

// 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":

Check warning on line 149 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L145-L149

Added lines #L145 - L149 were not covered by tests
// Windows doesn't use rpath
default:
// Use Linux format for other Unix-like systems
ldflags += fmt.Sprintf(" -extldflags '-Wl,-rpath=%s'", pythonLibDir)

Check warning on line 153 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L151-L153

Added lines #L151 - L153 were not covered by tests
}

// 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 @@
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

Check warning on line 169 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L167-L169

Added lines #L167 - L169 were not covered by tests
}
result = append(result, "-ldflags")
result = append(result, strings.Join(allFlags, " "))
} else {
result = append(result, arg)

Check warning on line 172 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L171-L172

Added lines #L171 - L172 were not covered by tests
}
result = append(result, args...)
}

return result
return append([]string{"-ldflags", ldflags}, result...)

Check warning on line 175 in cmd/internal/rungo/run.go

View check run for this annotation

Codecov / codecov/patch

cmd/internal/rungo/run.go#L175

Added line #L175 was not covered by tests
}

// 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 @@
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'")

Check warning on line 16 in inject.go

View check run for this annotation

Codecov / codecov/patch

inject.go#L16

Added line #L16 was not covered by tests
}
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))

Check warning on line 22 in inject.go

View check run for this annotation

Codecov / codecov/patch

inject.go#L22

Added line #L22 was not covered by tests
}
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")

Check warning on line 29 in inject.go

View check run for this annotation

Codecov / codecov/patch

inject.go#L24-L29

Added lines #L24 - L29 were not covered by tests
}
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
Loading