Go routes: compile route.go/proxy.go directories as importable packages, not package main (#101898)
### What?
`route.go` and `proxy.go` directories are now ordinary Go packages
(`package route`, `package proxy`, …) instead of `package main`. Next.js
owns the program entrypoint: it generates a separate `main` package that
imports the route directory and wires its handler into the server.
A directory that still declares `package main` gets a clear error:
> route.go must not use `package main`. Next.js generates the program
entrypoint and imports this directory as a package. Rename the package
in every .go file in this directory, for example to `package route`.
### Why?
With `package main`, user code and Next.js both claim the entrypoint,
and a route can never be imported. That blocks bundling several handlers
into one binary, because Go refuses to import a `main` package (`import
"…" is a program, not an importable package`). It also means `go
build`/`go vet` on a route directory fails with `function main is
undeclared in the main package`.
Making the route an importable package isn't enough by itself: route
directory names like `[id]`, `(group)`, `@slot`, or `café a 100%` aren't
valid Go import paths (`malformed import path … invalid char '['`).
### How?
- **turbopack-go**: new `go_package_bundle_from_sources(sources,
session, alias, generated)`. The selected source directory is overlaid
(via `go build -overlay`) at a virtual, module-relative directory
`alias` with a valid import path. Generated files under `alias` join
that package; the other generated Go files form the importing `main`
package. Nothing is written to the project.
- Discovery mirrors the directory's regular files, plus the subtree
below each `//go:embed` pattern's fixed prefix (Go reports patterns even
when they match nothing, so discovery mirrors and re-lists). A `go.mod`
in the package directory itself is not mirrored, so the virtual
directory doesn't become a nested module.
- `go list` results for the virtual package are mapped back to the
source directory before tracking, so source edits, embedded files, and
directory inventories stay compiler inputs.
- Mirrored `.go` files get a `//line <source path>:1` prefix, so
compiler errors **and runtime stack traces** name the original file (for
example `app/x/[id]/route.go:8`). Remaining diagnostics (`go list`
errors, `# pkg` headers) are mapped back to source paths.
- `go list`/`go build` now run from the module root, because named entry
files can live in a virtual directory.
- `GoSourceSet` exposes the go.mod `module_path`; `go_declarations`
reports `packages`.
- **next-api**: the route/proxy directory is aliased at
`<prefix>/_next_go_{route|proxy}_<sanitized rest>_<hash>`, where
`<prefix>` is the deepest ancestor-or-self of the route directory that
is a valid import path (e.g. `app/(group)/[id]` →
`app/_next_go_route_group_id_<hash>`). Go checks `internal` imports
against the importer's path, and any `x/internal` the route could import
has a valid `x` that is a prefix of `<prefix>`, so `internal` visibility
is unchanged. The generated `main` lives beside it
(`<prefix>/_next_go_main_<hash>`) so it can import the package even when
`<prefix>` contains `internal`. The generated handler/dispatch files use
the user's package name. The generated `main.go` imports the package and
calls the exported `NextInternalGoHandler(trustInternalHeaders bool)`.
The trust flag moved from a `main`-package const to that parameter.
Directories without a go.mod get a generated `go.mod` overlay so the
entrypoint can import them.
- Fixtures, docs (`route-go.mdx`, `proxy-go.mdx`), and the turbopack-go
README are updated.
**Behavior change:** because the route directory is now compiled as a
package (not as named files), Go's normal file selection applies: build
constraints and `_GOOS` filename suffixes now select sibling files. The
docs are updated to say so. Method declarations are still analyzed from
all files, so a method in an excluded file surfaces as a compile error.
**Not in this PR:** each route still builds its own executable.
Combining routes into one binary should now be a matter of generating
one `main` that imports several aliased packages (aliases are already
unique per directory), plus endpoint/runtime/adapter changes.
**Follow-up needed:** `vercel/vercel-internal` fixtures under
`packages/next/test/fixtures/00-go-adapter` still use `package main` and
will need the same rename.
### Verification
- `cargo test -p turbopack-go` (lib, `live`, `persistent`): all pass
with Go 1.24.4, including the new
`generated_entry_imports_source_directory_as_package` test
(`app/(group)/[id]` with a sibling file, an embedded asset, an
`internal` import, and a generated file; rebuilds on source and asset
edits; asserts nothing is written to the project and that diagnostics
name the source file with no `_next_go` paths) and new unit tests for
path/diagnostic mapping and go.mod parsing.
- Built every `native-route-go` and `native-proxy-go` fixture directory
with Go 1.24.4 using the same overlay layout and the updated templates.
All compiled; I ran the `app/(group)/native/[id]` binary and it served a
request with the embedded asset, sibling helper, internal dependency,
and path value intact.
- `cargo clippy -p turbopack-go --tests` and `cargo clippy -p next-api
--tests`: clean. I ran the new `go_package_alias` unit test standalone;
the full `next-api` test binary is left to CI.
- The `native-route-go` fixture's `[id]` route now imports
`app/internal/visibility`, which fails to compile if the package is
aliased outside `app` (I checked this against the old module-root alias
with Go 1.24). The turbopack-go live test also imports an `app/internal`
package.
- Added an e2e case: `package main` produces the error above and
recovers after renaming.
- Not run locally: the `native-route-go`/`native-proxy-go` e2e suites,
which need a native `next-swc` build (too large for a 2 vCPU / 4 GB
sandbox). CI ran them on the first commit and they passed.
<!-- NEXT_JS_LLM -->
---------
Co-authored-by: vercel-v[bot] <353008837+vercel-v[bot]@users.noreply.github.com>