-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/errtrace: Add toolexec mode for automatic rewriting (#90)
Fixes #17 The go command supports `-toolexec` to intercept calls to the underlying compile/link tools. By intercepting compile commands, we can modify the .go files passed to it to rewrite as part of the build process. There are some limitations: we can't add new dependencies to a package, so this initial version only rewrites packages that already import errtrace, acting as an opt-in to rewriting. We automatically determine if we're in toolexec mode based on the arguments/environment to simplify usage, ``` # pass -toolexec=errtrace, can use absolute paths if errtrace is not in PATH $ go build -toolexec=errtrace pkg/to/build # also compatible with go run, which is used by tests $ go run -toolexec=errtrace pkg/to/run ```
- Loading branch information
Showing
13 changed files
with
466 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package foo | ||
|
||
import "errors" | ||
|
||
func Foo() error { | ||
return errors.New("test") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module braces.dev/errtrace/cmd/errtrace/testdata/main | ||
|
||
go 1.21.4 | ||
|
||
require braces.dev/errtrace v0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
braces.dev/errtrace v0.3.0 h1:pzfd6LcWgfWtXLaNFWRnxV/7NP+FSOlIjRLwDuHfPxs= | ||
braces.dev/errtrace v0.3.0/go.mod h1:YQpXdo+u5iimgQdZzFoic8AjedEDncXGpp6/2SfazzI= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"braces.dev/errtrace/cmd/errtrace/testdata/main/foo" | ||
) | ||
|
||
func main() { | ||
if err := foo.Foo(); err != nil { | ||
fmt.Printf("%+v\n", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package main | ||
|
||
// Opt-in to errtrace wrapping with toolexec. | ||
import _ "braces.dev/errtrace" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
|
||
"braces.dev/errtrace/cmd/errtrace/testdata/toolexec-test/p1" | ||
) | ||
|
||
func main() { | ||
if err := callP1(); err != nil { | ||
fmt.Printf("%+v\n", err) | ||
} | ||
} | ||
|
||
func callP1() error { | ||
return p1.WrapP2() // @trace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package p1 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"braces.dev/errtrace/cmd/errtrace/testdata/toolexec-test/p2" | ||
) | ||
|
||
// WrapP2 wraps an error return from p2. | ||
func WrapP2() error { | ||
return fmt.Errorf("test2: %w", p2.CallP3()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package p2 | ||
|
||
import ( | ||
"braces.dev/errtrace" | ||
|
||
"braces.dev/errtrace/cmd/errtrace/testdata/toolexec-test/p3" | ||
) | ||
|
||
// CallP3 calls p3, and wraps the error. | ||
func CallP3() error { | ||
return errtrace.Wrap(p3.ReturnErr()) // @trace | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package p3 | ||
|
||
// Opt-in to errtrace wrapping with toolexec. | ||
import _ "braces.dev/errtrace" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package p3 | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// ReturnErr returns an error. | ||
func ReturnErr() error { | ||
return errors.New("test") // @trace | ||
} |
Oops, something went wrong.