# Migrate golang.org/x/net/context to the Standard Library context Package in Go

Yes. An agent rewrites every golang.org/x/net/context import to the standard context package, drops x/net from go.mod, and reruns the tests.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-x-net-context-to-context/x-net-context/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The `context` package lived at `golang.org/x/net/context` before it joined the standard library, and the old path has been an alias for the new one ever since. Code that still imports it compiles, which is why the import survives. The cost is indirect: the module keeps a dependency on all of `golang.org/x/net` for one alias. graphql-go/relay shows the cost in numbers. The library imports the old path in six files: `node.go`, `mutation.go`, three test files and `examples/starwars/schema.go`. It has no `go.mod`, and its `.travis.yml` still names Go 1.4.

The agent sets a baseline first. It runs `go mod init` and `go mod tidy` on the untouched code and reads what comes out. Tidy selects the newest `golang.org/x/net`. When that release's own `go.mod` requires a newer Go than the toolchain in use, which is what a Go 1.25 toolchain meets, the generated `go.mod` is stamped with that Go version and the `go` command downloads a matching toolchain before it will build. A library that only needed `context.Context` now forces a compiler upgrade on everyone who imports it. The agent then runs `go test ./...` and records the result. Here two tests in the root package fail before any change, `TestMutateAndGetPayload_AddsErrors` and `TestNodeInterfaceAndFields_AllowsRefetching_ReturnsNullForBadIDs`, both comparing error values produced by the current graphql-go release.

The rewrite is one line per file. On a Go 1.25 toolchain the agent can use the built-in rewriter, `go tool fix -r context`, which reports `fixed context` for each of the six files. Go 1.26 replaced `go fix` with a new set of analyzers that does not include that rewrite, so on a newer toolchain it edits the import lines directly and runs `gofmt`, which moves `"context"` to the top of each sorted import block. Either way it then searches for the old path in every `.go` file, including build-tagged ones the default build skips.

It regenerates `go.mod` and compares. After the change `go mod tidy` no longer lists `golang.org/x/net` at all, and the `go` directive falls back to the local toolchain's version because nothing demands a newer one. The agent reruns the tests and checks that the same two fail with the same messages and nothing else moved. It also checks the exported API: `MutationFn`, `IDFetcherFn` and `GlobalIDFetcherFn` all take a `ctx context.Context`, and because the old type is an alias for the new one, callers who still import the old path keep compiling against the migrated library.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Migrate this Go project from golang.org/x/net/context to the standard library context package. First capture a baseline on the unchanged code: make sure there is a go.mod (create one with the repository's import path if not), run go mod tidy, and record the go directive, the golang.org/x/net version it selected and the output of go build ./..., go vet ./... and go test ./.... Note any tests that already fail. Then replace every import of golang.org/x/net/context with context, in source, tests, examples and files behind build tags. Use go tool fix -r context if this toolchain still has that rewrite, otherwise edit the imports and run gofmt. Do not change function signatures or behaviour. Check subpackages too: golang.org/x/net/context/ctxhttp has no alias in the standard library, so replace its calls with http.NewRequestWithContext and the client's Do method, and say where you did. Run go mod tidy again and report whether golang.org/x/net left go.mod or which other import still needs it, and whether the go directive changed. Run the same build, vet and test commands and confirm the results match the baseline apart from what you intended. List every exported function or type whose signature mentions Context and explain why callers still compile. Update CI configuration only if it pins a Go version older than the standard context package. Open a pull request.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

A pull request whose source diff is six changed import blocks and whose `go.mod` diff is the point. The description puts the two generated `go.mod` files next to each other: before, with `golang.org/x/net` required and the `go` line raised to whatever that release demands, and after, with the dependency gone and the `go` line back at the toolchain default. For a library, that second file is what downstream users inherit.

The test section is a comparison, not a claim of green. It names the tests that failed on the unchanged code, shows they fail identically afterwards, and says they are outside this change. A reviewer who sees two red tests in CI can match them to the baseline in the description without bisecting.

It lists the exported signatures that mention `Context` and states that the old and new types are identical, so the change needs no major version. The reviewer confirms with a search that the old import path is gone, checks that no `ctxhttp` call was missed, and reads the one CI edit: a file like relay's `.travis.yml`, which names Go 1.4, cannot build code that imports the standard package, so the agent raises that version and says so.

## Run it on every Monday morning

Imports that the standard library has absorbed pile up quietly, because they keep compiling. Save a wider version of the prompt as an automation on the repository's Project: ask the agent to find one import per run that has a standard-library replacement, migrate it with a before and after of `go.mod`, and open a pull request. Give it a `schedule` trigger with `cron` set to `0 8 * * 1` and your `timezone`. Each Monday produces one small pull request with its own test comparison.

For many repositories, a script is simpler. List the repositories whose Go files contain the old import path and send `POST /o/{org}/sessions` once for each with the body above. A `request_id` that is a UUID v5 of the repository name and the import path makes a rerun of the script return the runs already started.

## Why run it on Rush?

The edit is trivial, and the proof that nothing else changed is the real work. That means generating `go.mod` twice from a clean module cache, running the suite twice, and comparing. On a laptop the cache is warm and the toolchain may auto-upgrade in the background, which hides the very effect this migration removes. The sandbox starts clean, so the toolchain download the old dependency triggers shows up in the output, and the session keeps both runs for the reviewer.

It also parallelises well. A team with many services has a copy of this import in each, and each is a separate small diff with its own test baseline. The requests for all of them can be sent together with an API key. Each run gets its own sandbox, the ones past the key's concurrency limit wait in line, and each returns a pull request that says exactly which tests were already failing. Nobody has to hold all those baselines in their head.

## Frequently asked questions

### Is golang.org/x/net/context the same type as context.Context?

Yes. In current releases of `golang.org/x/net` the file declares `type Context = context.Context`, a type alias, with `CancelFunc` aliased the same way and `Canceled` and `DeadlineExceeded` assigned from the standard package. A value of one is a value of the other, so the two import paths can be mixed across packages during a migration.

### Does go fix still rewrite the import for me?

It depends on the toolchain. Go 1.25 lists a `context` rewrite under `go tool fix`, described as changing imports of `golang.org/x/net/context` to `context`. Go 1.26 ships a different `go fix` whose analyzer list has no such entry. The agent checks which one it has and falls back to editing the import lines.

### Will this break users of my library?

Not at compile time, since exported signatures refer to the same type under either name. What changes for them is your `go.mod`: they stop inheriting an indirect requirement on `golang.org/x/net` from you, and any Go version floor that came with it.

### What about golang.org/x/net/context/ctxhttp?

That subpackage has no alias in the standard library. Its helpers are replaced by building the request with `http.NewRequestWithContext` and calling the client's `Do`. The prompt asks the agent to make that change where it finds the import and to list each call site, since it is a small code change and not only an import path.

## More jobs like this: Upgrade dependencies weekly

- [Pin Docker Base Images by Digest](https://getrush.ai/run/pin-image-versions/docker/)
- [Pin and Upgrade Container Images in Docker Compose](https://getrush.ai/run/pin-image-versions/docker-compose/)
- [Pin and Upgrade Container Images in Kustomize Manifests](https://getrush.ai/run/pin-image-versions/kustomize/)
- [Pin the Python Dependencies of a Streamlit App](https://getrush.ai/run/pin-python-dependencies/streamlit/)
- [Remove six and Python 2 Compatibility Code From a Python Project](https://getrush.ai/run/remove-six/six/)
- [Replace passlib in a FastAPI App Without Breaking Stored Hashes](https://getrush.ai/run/replace-passlib/passlib/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
