14

I need to remove absolute path in the trace, that corresponds to the imported module. Even though I compile my program so: go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH I still get the full path to the module file, where the panic appeared, though the non-module file of the program doesn't show the full path:

goroutine 1 [running]:
monitors/fibre_monitor/logging.FileHandler(0x5e6755, 0x1a, 0x441, 0x0, 0x6fc23ac00, 0x1, 0x500000, 0xc000000002, 0xb)
        /home/gtristan/go/src/monitors/fibre_monitor/logging/file_handler.go:182 +0x11f
main.python_logger(0x5e1383, 0x5, 0x5e6755, 0x1a, 0x101)
        src/monitors/fibre_monitor/fibre_monitor.go:73 +0x1b5
main.main_check(0x0, 0xc00008e058)
        src/monitors/fibre_monitor/fibre_monitor.go:343 +0x65
main.main()
        src/monitors/fibre_monitor/fibre_monitor.go:428 +0x56

what can be a solution to get rid of GOPATH everywhere in the trace?

gtristan
  • 355
  • 2
  • 10

1 Answers1

25

Use the -trimpath argument to go build (not to gcflags or asmflags):

Without -trimpath:

$ go build .
$ ./panic 
panic: bleh

goroutine 1 [running]:
main.example(0xc000046738, 0x2, 0x4, 0x473f2b, 0x5, 0xa)
    /home/me/stuff/src/github.com/me/testing/panic/main.go:9 +0x39
main.main()
    /home/me/stuff/src/github.com/me/testing/panic/main.go:4 +0x72

With -trimpath:

$ go build -trimpath  .
$ ./panic 
panic: bleh

goroutine 1 [running]:
main.example(0xc000046738, 0x2, 0x4, 0x473f2b, 0x5, 0xa)
    github.com/me/testing/panic/main.go:9 +0x39
main.main()
    github.com/me/testing/panic/main.go:4 +0x72

Per go help build:

    -trimpath
        remove all file system paths from the resulting executable.
        Instead of absolute file system paths, the recorded file names
        will begin with either "go" (for the standard library),
        or a module path@version (when using modules),
        or a plain import path (when using GOPATH).
Marc
  • 19,394
  • 6
  • 47
  • 51
  • 1
    oh, thanks! Now the path looks a bit weird, but it serves my purpose :) – gtristan Sep 10 '20 at 14:32
  • 1
    I ended up compiling so: `go build -gcflags=-trimpath=$GOPATH -asmflags=-trimpath=$GOPATH -trimpath` (added -trimpath in addition to other flags), because otherwise the trace showed `command-line-arguments/fibre_monitor.go - main.python_logger:90` , where command-line-arguments was the thing which seemed weird. But now everything is perfect. Sorry for not upvoting your answer as I have not enough points to vote :( – gtristan Sep 10 '20 at 14:50