Skip to content

internal/imports: lazily query go env for GOROOT - #700

Open
hailaz wants to merge 1 commit into
golang:masterfrom
hailaz:lazy-goroot
Open

hailaz wants to merge 1 commit into
golang:masterfrom
hailaz:lazy-goroot

Conversation

@hailaz

@hailaz hailaz commented Oct 20, 2026 •

Copy link
Copy Markdown

Since the Source interface refactoring (x/tools v0.28.0), getFixes has
called env.goEnv unconditionally, spawning a go env subprocess on every
imports.Process call -- even when the file's imports are already complete
and the result is never used.

GOROOT's only consumer is a self-import guard in addStdlibCandidates,
which runs only after the first two passes fail. Resolve it lazily so
that the happy path requires zero subprocesses.

Measured on a file with complete imports (linux/amd64, local SSD):
3.6-4.9ms per imports.Process call before, 0.12-0.75ms after (matching
x/tools v0.26.0 behavior). A single go env costs ~5ms here; library
consumers that format many generated files pay it once per file.

Fixes golang/go#82361

@google-cla

google-cla Bot commented Oct 20, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 8df2686) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/851285.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@lingcoder

Copy link
Copy Markdown

mark

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from David Chase:

Patch Set 1: Auto-Submit+1 Commit-Queue+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

Dry run: CV is trying the patch.

Bot data: {"action":"start","triggered_at":"2026-10-26T01:23:31Z","revision":"a1eb49ead45da6ec3138fbcb8da0a563f717b841"}


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from David Chase:

Patch Set 1: -Commit-Queue

(Performed by <GERRIT_ACCOUNT_60063> on behalf of <GERRIT_ACCOUNT_7061>)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1:

This CL has passed the run


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from golang-scoped@luci-project-accounts.iam.gserviceaccount.com:

Patch Set 1: LUCI-TryBot-Result+1


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 66992b8) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/851285.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

lingcoder added a commit to gogf/gf that referenced this pull request Oct 28, 2026
…4929)

历史资料 #4498
#4479

## What

`GoFmt` no longer uses `golang.org/x/tools/imports.Process`. It is
reimplemented with `go/parser` + `astutil` + `go/format`, removing
unused imports as before but requiring zero subprocesses.
`golang.org/x/tools` is upgraded from v0.26.0 to v0.36.0.

## Why

Since x/tools v0.28.0 (the `Source` interface refactor),
`imports.Process` spawns a `go env` subprocess on **every single call**
— even when the file's imports are already complete and the result is
never used. `gf gen dao` formats every generated file through `GoFmt`,
so a project with N generated files spawns N `go env` subprocesses.

This made `gf gen dao` about 3x slower and version-sensitive: cmd/gf
currently has to pin x/tools v0.26.0 to dodge the regression, and any
future upgrade silently reintroduces the slowdown (much worse on slow
disks / CI).

## How

- `goFormatContent` parses the file, drops unused imports via
`astutil.UsesImport` + `DeleteImport`/`DeleteNamedImport`, then formats
with `format.Node` — all in-process.
- x/tools upgraded to v0.36.0, the highest version whose `go.mod` still
accepts go 1.23.0, so the `go` directive is unchanged and existing users
are unaffected.
- Added `Test_GoFmt` covering unused-import removal, named imports and
idempotency.

## Verification

Real project (hotgo, 156 generated files, 3 rounds each):

| Build | Time | `go env` subprocesses |
| --- | --- | --- |
| master + x/tools v0.26.0 (pinned) | 0.81s | 0 |
| master + x/tools v0.52.0 | 0.80s | 117 |
| **this PR + x/tools v0.36.0** | **0.28s** | **0** |
| **this PR + x/tools v0.52.0** | **0.28s** | **0** |

- Performance is now decoupled from the x/tools version.
- All 16 testdata generated files produce byte-identical output.
- Full `cmd/gf` test suite passes.

## Notes

- One cosmetic difference vs `imports.Process`: after deleting an unused
import, the parenthesized `import (...)` block is preserved instead of
collapsing to a single-line import. Output remains valid,
gofmt-compliant code.
- The root cause is an upstream x/tools regression (`getFixes` calls
`env.goEnv()` unconditionally while GOROOT is only needed on the slow
path); I'll file an issue + fix with golang/tools separately.


golang/tools#700

---------

Co-authored-by: Jack Ling <34231795+lingcoder@users.noreply.github.com>
@gopherbot

Copy link
Copy Markdown
Contributor

Message from 黄谷谷:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from 黄谷谷:

Patch Set 2:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

@gopherbot

Copy link
Copy Markdown
Contributor

Message from Peter Weinberger:

Patch Set 2:

(4 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

Since the Source interface refactoring (x/tools v0.28.0), getFixes has
called env.goEnv unconditionally, spawning a `go env` subprocess on every
imports.Process call -- even when the file's imports are already complete
and the result is never used.

GOROOT's only consumer is a self-import guard in addStdlibCandidates,
which runs only after the first two passes fail. Resolve it lazily so
that the happy path requires zero subprocesses.

Measured on a file with complete imports (linux/amd64, local SSD):
3.6-4.9ms per imports.Process call before, 0.12-0.75ms after (matching
x/tools v0.26.0 behavior). A single `go env` costs ~5ms here; library
consumers that format many generated files pay it once per file.

Fixes golang/go#82361
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 3126c7d) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/tools/+/851285.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot

Copy link
Copy Markdown
Contributor

Message from 黄谷谷:

Patch Set 3:

(3 comments)


Please don’t reply on this GitHub thread. Visit golang.org/cl/851285.
After addressing review feedback, remember to publish your drafts!

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.

x/tools/imports: Process spawns a go env subprocess on every call, even when no fixes are needed

3 participants