Documentation
¶
Overview ¶
Package godebug is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package godebug holds the pieces of running a game under the Go debugger: parsing Delve's listen announcement from nsgo dev -debug output and building the IDE attach configuration for it. Both game dev and the editor use it, so the configuration a CLI session prints and the one the editor copies or writes to .vscode/launch.json are the same.
Index ¶
Examples ¶
Constants ¶
const AttachConfigName = "Attach to game"
AttachConfigName is the name of the VS Code launch configuration, and the suggested GoLand Go Remote configuration, that attaches Delve to a running game.
Variables ¶
This section is empty.
Functions ¶
func AttachConfig ¶
AttachConfig returns the attach configuration for the Delve server at host:port, named AttachConfigName. GoLand's equivalent is a Go Remote configuration with the same host and port.
func AttachConfigJSON ¶
AttachConfigJSON returns AttachConfig as an indented JSON object, ready to paste into a .vscode/launch.json configurations array.
func ParseListenAddress ¶
ParseListenAddress extracts the host and port from Delve's listen announcement line. ok is false for any other line.
Example ¶
ExampleParseListenAddress turns Delve's announcement into an IDE attach configuration.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/tooling/godebug"
)
func main() {
host, port, ok := godebug.ParseListenAddress("API server listening at: 127.0.0.1:40000")
fmt.Println(host, port, ok)
_, _, ok = godebug.ParseListenAddress("building game...")
fmt.Println(ok)
fmt.Println(godebug.AttachConfigJSON(host, port))
}
Output: 127.0.0.1 40000 true false { "name": "Attach to game", "type": "go", "request": "attach", "mode": "remote", "debugAdapter": "dlv-dap", "host": "127.0.0.1", "port": 40000 }
func UpsertVSCodeLaunchConfig ¶
UpsertVSCodeLaunchConfig writes the AttachConfigName configuration for the Delve server at host:port into projectDir/.vscode/launch.json, creating .vscode and the file when they are missing and replacing the entry of the same name when one is there. Other configurations and top-level members are preserved. A launch.json with comments or trailing commas (VS Code allows them) cannot be parsed and is reported as an error rather than rewritten.
Example ¶
ExampleUpsertVSCodeLaunchConfig adds the attach configuration to an existing launch.json and later updates its port in place.
package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/nsgo-dev/game/pkg/tooling/godebug"
)
func main() {
dir, err := os.MkdirTemp("", "godebug-example")
if err != nil {
fmt.Println(err)
return
}
defer os.RemoveAll(dir)
_ = os.MkdirAll(filepath.Join(dir, ".vscode"), 0o755)
existing := `{"version": "0.2.0", "configurations": [{"name": "Run tests", "type": "go"}]}`
_ = os.WriteFile(filepath.Join(dir, ".vscode", "launch.json"), []byte(existing), 0o644)
fmt.Println(godebug.UpsertVSCodeLaunchConfig(dir, "127.0.0.1", 40000))
fmt.Println(godebug.UpsertVSCodeLaunchConfig(dir, "127.0.0.1", 40001))
data, _ := os.ReadFile(filepath.Join(dir, ".vscode", "launch.json"))
var file struct {
Configurations []struct {
Name string `json:"name"`
Port int `json:"port"`
} `json:"configurations"`
}
_ = json.Unmarshal(data, &file)
for _, c := range file.Configurations {
fmt.Printf("%s port=%d\n", c.Name, c.Port)
}
}
Output: <nil> <nil> Run tests port=0 Attach to game port=40001
Types ¶
This section is empty.