grafana / xk6

Build k6 with extensions
Apache License 2.0
215 stars 20 forks source link

Update build flags to make `-trimpath` go flag configurable #82

Closed oleiade closed 9 months ago

oleiade commented 11 months ago

Context and Problem

While working on integrating the Streams API into k6 using xk6 extensions, I encountered a significant challenge in debugging with VSCode. Specifically, the debugger could not associate breakpoints with the correct file paths. This issue stems from the -trimpath argument in our build process, which removes file system paths from the executable, thus hindering the debugger's ability to function correctly.

Solution

This PR modifies the build process for xk6 extensions. By adjusting where and how the -trimpath flag is applied, we allow extension developers to override this setting using the XK6_BUILD_FLAGS environment variable. This change offers more flexibility in preserving debug symbols and optimizing the debugging experience in VSCode.

For the history books: Debugging in VSCode using this fix

For those looking to debug xk6 extensions in VSCode, here's a guide based on my setup:

Build Task Configuration

Create a VSCode task to build the xk6 binary with the necessary flags for debugging. This task utilizes the changes introduced in this PR. Here's the configuration in tasks.json:

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "command": "xk6",
            "args": [
                "build",
                "--with",
                "github.com/grafana/xk6-streams=.",  // Replace with your extension
                "--output",
                "${workspaceFolder}/k6"
            ],
            "group": "build",
            "label": "xk6: build",
            "options": {
                "env": {
                    "XK6_BUILD_FLAGS": "-gcflags 'all=-N -l'"
                }
            }
        }
    ]
}

Define a launch.json file in VSCode to run the above build task before starting the debugger. Here's the configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "xk6",
            "type": "go",
            "request": "launch",
            "mode": "exec",
            "preLaunchTask": "xk6: build",
            "cwd": "${workspaceFolder}",
            "program": "${workspaceFolder}/k6",
            "args": ["run", "${workspaceFolder}/examples/readablestream-numbers.js"]
        }
    ]
}

References

ref #51 ref #43