osx
dataf3l opened this issue · 3 comments
Tried to build, it shows an error:
./main.go:61:3: unknown field 'Pdeathsig' in struct literal of type syscall.SysProcAttr
what to do?
I also would like to use that on OSX and tried some stuff but didn't make it work (yet).
I got the same error in Windows 11
go build -ldflags="-s -w"
.\main.go:61:3: unknown field Pdeathsig in struct literal of type syscall.SysProcAttr
@zanezhub maybe change main.go and who knows what else, I asked ChatGPT, it did this:
The given code appears to be written for Linux, and it makes use of specific commands and file paths that might not be valid or exist on Windows and macOS.
Here's how to make this code platform-agnostic:
-
Platform-specific logic:
- For things like paths, commands, and system calls, you should differentiate the logic based on the OS. This is done using the
runtime
package'sGOOS
constant.
- For things like paths, commands, and system calls, you should differentiate the logic based on the OS. This is done using the
-
File Paths:
- Linux often uses paths like
/home/username/...
. This is not valid on Windows (which usesC:\Users\username\...
) or macOS. - Use Go's
os
andfilepath
packages to work with paths to make it OS-agnostic.
- Linux often uses paths like
-
System Calls:
syscall.SysProcAttr
andPdeathsig
are Linux-specific. These aren't applicable to macOS and Windows.
-
Commands:
- The command being executed (
dlv
) should be available on all platforms. This will only work if thedlv
command is available on the PATH of each OS.
- The command being executed (
Here's a simplified version of your code to make it work on all platforms:
// ... [other imports]
import (
"runtime"
)
// ...
func startDebugger(commandArgs []string) int {
log.Printf("Starting dlv-backend: dlv %s", strings.Join(commandArgs, " "))
cmd := exec.Command(
"dlv",
commandArgs...,
)
// Platform-specific logic for Linux
if runtime.GOOS == "linux" {
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
}
// ... [rest of the function]
}
// ...
func init() {
// ...
// Platform-specific default log path logic
defaultLogFile := "$XDG_DATA_HOME/dlvtui.log"
if runtime.GOOS == "windows" {
defaultLogFile = "$APPDATA/dlvtui.log"
} else if runtime.GOOS == "darwin" { // macOS
defaultLogFile = "$HOME/Library/Application Support/dlvtui.log"
}
exFlags.StringVar(&logfile, "logfile", defaultLogFile, "Path to the log file.")
// ... [rest of the function]
}
// ...
func main() {
// ...
// Platform-specific logic to resolve dir
var defaultPrefix string
switch runtime.GOOS {
case "linux":
defaultPrefix = "/home/"
case "windows":
defaultPrefix = "C:/Users/"
case "darwin":
defaultPrefix = "/Users/"
default:
defaultPrefix = ""
}
// Replace /home/ with the relevant prefix for the OS
for _, f := range fileList {
if strings.HasPrefix(f, defaultPrefix) && !strings.Contains(f, "/go/pkg") {
dir = filepath.Dir(f)
break
}
}
// ... [rest of the function]
}
Changes made:
- Added the
runtime
package to detect the platform. - Updated
startDebugger
function to only setPdeathsig
for Linux. - Updated the
init
function to set the default log path based on the OS. - Updated the
main
function to have OS-specific logic for determining the directory prefix.
Keep in mind:
- This will make the code work across all platforms, assuming the
dlv
command is available on the PATH of each OS. - Testing on each platform is crucial, as platform-specific quirks can sometimes introduce subtle bugs.
I hope this ChatGPT thing is useful, have a nice day