Skip to content

Comments

toolexec: Add unsafe rewrite support#128

Merged
prashantv merged 2 commits intomainfrom
prashant/toolexec-unsafe
Jul 22, 2025
Merged

toolexec: Add unsafe rewrite support#128
prashantv merged 2 commits intomainfrom
prashant/toolexec-unsafe

Conversation

@prashantv
Copy link
Contributor

Fixes #91.

Add ability to rewrite packages that don't import errtrace to be
rewritten to wrap errors automatically. This would normally cause
errors when trying to build the package as we're adding a new
dependency to the package, which toolexec doesn't support:
golang/go#35204

To workaround this, we use the hack mentioned in the above issue
using go:linkname to "import" errtrace functionality in a way
that doesn't break the package build.

Using go:linkname requires importing unsafe, to the rewriter
is updated to:

  • import the "unsafe" package instead of errtrace, which can be added
    as a new dependency without impacting the build as it's stdlib.
  • Add go:linkname to import errtrace functions into a package.
  • Rewrite to use the go:linkname errtrace identifiers instead of
    the errtrace package.

go:linkname is not compatible with generic functions like WrapN so
disable WrapN when using toolexec. WrapN is for errtrace -w where
source files are directly modified, and formatted with gofmt where
WrapN minimizes rewriting. This isn't necessary for toolexec mode.

Unsafe mode still requires one package that's part of the final linked
object references errtrace somewhere in the binary -- otherwise, the
build fails. This seems like a reasonable limitation since users need to
manage the dependency.

@prashantv
Copy link
Contributor Author

This change is part of the following stack:

Change managed by git-spice.

@prashantv prashantv requested a review from abhinav June 8, 2025 22:11
Copy link
Contributor

@abhinav abhinav left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@iwahbe
Copy link

iwahbe commented Jul 17, 2025

Is this PR ready to merge?

@prashantv prashantv force-pushed the prashant/extract-tracepaths branch from 84f304c to 7c72e04 Compare July 22, 2025 05:04
Base automatically changed from prashant/extract-tracepaths to main July 22, 2025 05:08
Fixes #91.

Add ability to rewrite packages that don't import errtrace to be
rewritten to wrap errors automatically. This would normally cause
errors when trying to build the package as we're adding a new
dependency to the package, which toolexec doesn't support:
golang/go#35204

To workaround this, we use the hack mentioned in the above issue
using `go:linkname` to "import" errtrace functionality in a way
that doesn't break the package build.

Using `go:linkname` requires importing `unsafe`, to the rewriter
is updated to:
 * import the "unsafe" package instead of errtrace, which can be added
   as a new dependency without impacting the build as it's stdlib.
 * Add `go:linkname` to import errtrace functions into a package.
 * Rewrite to use the `go:linkname` errtrace identifiers instead of
   the errtrace package.

`go:linkname` is not compatible with generic functions like `WrapN` so
disable `WrapN` when using toolexec. `WrapN` is for `errtrace -w` where
source files are directly modified, and formatted with `gofmt` where
`WrapN` minimizes rewriting. This isn't necessary for toolexec mode.

Unsafe mode still requires one package that's part of the final linked
object references `errtrace` somewhere in the binary -- otherwise, the
build fails. This seems like a reasonable limitation since users need to
manage the dependency.
@prashantv prashantv force-pushed the prashant/toolexec-unsafe branch from 5cd7bdf to 878b94f Compare July 22, 2025 05:09
@prashantv prashantv merged commit 06ef76b into main Jul 22, 2025
14 checks passed
@prashantv prashantv deleted the prashant/toolexec-unsafe branch July 22, 2025 05:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cmd/toolexec: rewrite of packages that don't import errtrace

3 participants