Skip to content

cmd/compile,cmd/go: decouple export data formats used by compiler and x/tools #79427

Open

Description

@adonovan

This is @cherrymui's idea.

Background: Export data is the compact binary notation for serialized type information: conceptually it is a serialized types.Package. There are two different formats today: the unified (u) format written and read by the compiler, and the indexed (i) format written and read by x/tools (incl. gopls). However, the x/tools reader must also be capable of reading u format because go list -export runs the compiler to generate export data files. This creates a complex and inconvenient coupling between the compiler and x/tools.

Proposal: Let's split the two formats apart. The compiler would read and write only u format written by the exact same version of the toolchain. This would allow it to evolve independently: any commit on any given day would be free to change the reader and writer. We would provide no supported way to read the compiler's format; even its existence would be an internal implementation detail.

The x/tools format would read and write only i format; however, it would still need to retain the ability to read files produced by go list -export. So, we could change go list -export to run not cmd/compile but a new frontend tool, cmd/export, based on go/ast + go/types + vendor/x/tools, that simply reads, parses, and type-checks Go source files, reading export data for dependencies and writing it out for the current package. (The cmd/export tool should be straightforward to write: it's basically unitchecker with the analysis parts ripped out, accepting its command-line arguments in cmd/compile not cmd/vet form).

The only unknown quantity is the go/importer package. Today it may be used to read files named by go list -export; since these would in future be produced by cmd/export, go/importer would use the vendored x/tools reader. But it's possible that some users are also using it to parse .a files produced by the compiler that they found some other way. (I vaguely recall seeing such file names in the output of go list -json but I cannot now find them. go help install talks about package files being installed, but I don't know exactly what that implies.) We need to investigate who would be broken if we stopped making go/importer support compiler export data files.

When a language change affects exported types, the process would be:

  1. add support to go/types{,2}. Now you can use the feature in single-package tests of go/types and/or the compiler.
  2. add support to the compiler and its export data reader/writer. Now you can use the feature in multi-package tests of the compiler.
  3. add support to x/tools reader and writer.
  4. vendor x/tools into GOROOT. Now Go developers' go list -export would be emitting the new format (whether or not the package actually uses new language features). Any x/tools-based commands installed on their $PATH would need to be re-built with a version of x/tools later than step 3.
  5. Go release. At this point all users of the new toolchain would be using the new format. Tools binaries on their PATH would need updating.

Several people have asked why not guarantee backward compatibility of the x/tools file format so that installed x/tools-based tools are not broken by each toolchain update. This is appealing, but in general it is impossible: if a package makes use of a new language feature that cannot yet be written, there is really nothing sound that the reader can do other than report a version skew error. It might seem gentler to the user not to report an error until we actually encounter a usage of the new language feature (not just the newer file version number), but typically the new feature will be quickly adopted in low-level packages, so there is little practical benefit to reporting the error lazily.

@griesemer @mrkfrmn

Activity

  1. added this to the Proposal milestone on May 15, 2026
  2. added
    ToolsThis label describes issues relating to any tools in the x/tools repository.
    on May 15, 2026
  3. moved this to Incoming in Proposalson May 15, 2026
  4. griesemer commented on May 15, 2026

    @griesemer
    Contributor

    It may still be useful to have a separate reader/viewer for the compiler export format (we don't really have that), for debugging purposes. It could show a lot of compiler related information. But this would not affect this proposal, of course, and we wouldn't need to make this a public tool, necessarily (to avoid backward-compatibility requirements).

    For completeness, per our discussion, there is also a gccgo export data importer but that is effectively defunct since it never supported generics.

    I also note that the proposed new stand-alone tool would not need to type -check function bodies I think and that go/types already supports such a mode. So this could be pretty fast.

    I wonder if there's any overlap between this and the work we do in the API checker.

  5. adonovan commented on May 18, 2026

    @adonovan
    MemberAuthor

    It may still be useful to have a separate reader/viewer for the compiler export format

    Certainly that may be useful, but it is (as you say) orthogonal to the proposal.

    For completeness, per our discussion, there is also a gccgo export data importer but that is effectively defunct since it never supported generics.

    FYI I filed #79453 to drop support for gccgo.

    I also note that the proposed new stand-alone tool would not need to type -check function bodies I think and that go/types already supports such a mode. So this could be pretty fast.

    Exactly: the cmd/export tool should be much faster than the actual compiler, and it critical path much shorter.

  6. griesemer commented on May 27, 2026

    @griesemer
    Contributor

    We believe this is a good idea but we should get a better understanding of the impact of not being able to read the compiler export data by tools.

    As an experiment we can poison the compiler export data (so that it cannot be read by the tools anymore) and see what needs to be fixed on the tools side.

  7. aclements commented on May 28, 2026

    @aclements
    Member

    This proposal has been added to the active column of the proposals project
    and will now be reviewed at the weekly proposal review meetings.
    — aclements for the proposal review group

  8. moved this from Incoming to Active in Proposalson May 28, 2026
  9. self-assigned this
    on Jun 17, 2026
  10. 45 remaining items

  11. modified the milestones: Backlog, Go1.28 on Oct 8, 2026
  12. changed the title [-]decouple export data formats used by compiler and x/tools[/-] [+]cmd/compile,cmd/go: decouple export data formats used by compiler and x/tools[/+] on Oct 8, 2026
  13. mvdan commented on Oct 13, 2026

    @mvdan
    Member

    Raised what I think could be a regression I spotted today downstream: #82186

  14. gopherbot commented on Oct 21, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/852225 mentions this issue: go/gcexportdata: clarify which export files require NewReader

  15. Itz-Agasta commented on Nov 11, 2026

    @Itz-Agasta

    one more case this breaks..... toolexec tools that add imports while compiling. i hit it in OpenTelemetry's compile-time instrumentation (open-telemetry/opentelemetry-go-compile-instrumentation#1740). when a rule adds an import to a package, i run go list -export for it and put the Export file into the compiler's -importcfg. on tip that's now a cmd/export file, so the compiler can't read it.

    The only workaround i found loorking around is the one in the pending compilebench change (CL 849326): getting the importcfg out of go build -n / -work.

    so yaa is there any better supported way to get a package's compiler archive for an importcfg ?

  16. adonovan commented on Nov 11, 2026

    @adonovan
    MemberAuthor

    The only workaround i found looking around is the one in the pending compilebench change (CL 849326): getting the importcfg out of go build -n / -work.

    Sorry for the breakage. That does seem like the smallest workaround, but the whole approach is unsound in general. There are many ways in which the build graph for the original build and for the one extra package built in isolation might differ. For example, test variants, PGO, a whole list of compiler flags. Presumably you would also need to modify the linker's -importcfg too and ensure that every common dependency of the two builds was compiled under exactly the same regime.

    A more robust approach would be to inject the new imports where the build system can see them, using the -overlay mechanism to inject additional files.

  17. gopherbot commented on Nov 11, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/861988 mentions this issue: internal/gcimporter: break dependency on objectpath

  18. gopherbot commented on Nov 21, 2026

    @gopherbot
    Contributor

    Change https://go.dev/cl/863174 mentions this issue: internal/gcimporter: move legacy code to gcexportdata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

ProposalProposal-AcceptedToolsThis label describes issues relating to any tools in the x/tools repository.compiler/runtimeIssues related to the Go compiler and/or runtime.release-blocker

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions