All jobs

Replace mitchellh/go-homedir With os.UserHomeDir in a Go CLI

Yes. An agent replaces go-homedir with os.UserHomeDir, tests the binary with HOME unset, and keeps the service starting where the old library did.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

github.com/mitchellh/go-homedir is archived on GitHub, and the standard library has had os.UserHomeDir since Go 1.12. The agent reads how the code uses the old package before trusting that the two are equal. In a scheduler like ajvb/kala there is one call. cmd/root.go runs homedir.Dir() inside initViper when no --kala config flag is given, adds the result as a viper config path so that $HOME/kala.yaml is found, and on an error prints it and calls os.Exit(1). Nothing calls homedir.Expand, the function that has no standard replacement.

The one-line swap compiles, and with HOME set the two binaries behave the same: given a directory holding a kala.yaml, both print Using config file: with that path. The agent then runs the case the swap changes. It starts each build with env -u HOME, so no HOME exists. The old binary starts and serves, because go-homedir falls back to looking the user up when the variable is empty. The new one prints $HOME is not defined and exits with status 1, since os.UserHomeDir only reads the variable.

That matters for this program in particular. The repository ships deployment/systemd/kala.service, whose [Service] section is a single ExecStart line with no User=, and systemd documents HOME as set for units that name a user. A scheduler that exited at boot after a dependency cleanup would be a bad trade. So the agent keeps the old behaviour with the standard library alone: when os.UserHomeDir returns an error it asks os/user.Current() for the home directory, and only exits if that fails too. It rebuilds and repeats the env -u HOME run, once more with CGO_ENABLED=0, and the server starts in both.

Last comes the module graph, where the result is less tidy than the diff suggests. After go mod tidy the direct requirement leaves go.mod, but three go-homedir lines remain in go.sum. go mod why -m github.com/mitchellh/go-homedir shows the chain: the Consul storage backend imports github.com/hashicorp/consul/api, which imports go-rootcerts, which imports go-homedir. The agent reports that the archived module is still compiled into the binary through that path and that removing it for good depends on that upstream chain, not on this repository's own code.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Replace github.com/mitchellh/go-homedir with the standard library in this Go module. List every call to homedir.Dir and homedir.Expand and what the result is used for. Build the binary before changing anything and record its behaviour in three environments: HOME set to a directory that contains the config file the program looks for, HOME unset (run it with env -u HOME, which keeps PATH), and HOME set to an empty string. Then replace homedir.Dir with os.UserHomeDir and repeat the three runs. If the old binary worked with HOME unset and the new one does not, add a fallback to os/user.Current().HomeDir so the behaviour is kept, and repeat the runs again, including a build with CGO_ENABLED=0. For homedir.Expand, write a small helper that expands a leading ~/ and test it. Check how the program is started in the repository (systemd units, Dockerfile, cron examples) and say whether HOME is set there. Run go mod tidy, then go mod why -m github.com/mitchellh/go-homedir, and report whether the module is still in the build through another dependency. Run go vet and the tests with the Go version CI pins. Open a pull request with every command and its output.",
  "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 that changes cmd/root.go and removes one line from go.mod. The description has a three-row table (home set, unset, empty) with the output of the old binary, the bare swap and the final version, the note about the systemd unit, and the go mod why chain that explains why go.sum still names the module.

The reviewer's decision is whether the fallback is wanted. Some teams prefer a tool that fails loudly when HOME is missing; the table shows what each choice does, and dropping the fallback is a few deleted lines. They should also read the last section as a limit of this change. The pull request ends this repository's own use of an archived package. It does not remove the package from the binary, and the description names the dependency that keeps it there.

Run it on every archived dependency report

If you already run a job that lists archived modules across your repositories, let it start the work. Add an automation on the Project with a webhook trigger and the prompt above, and have the job POST to the trigger's URL with Authorization: Bearer and the trigger's secret. The body it sends, up to 64 KB, is appended after the prompt as event text, so the report's line for this repository reaches the agent.

Without such a job, a schedule trigger with a monthly cron does the same on a calendar. Either way the automation's hourly run cap applies, and a fire past it waits as a queued run.

The use case this job follows, with its automation and trigger: Upgrade dependencies weekly.

Why run it on Rush?

Whether this swap is safe depends on an environment a developer's shell never has: no HOME at all. Reproducing it takes a clean process environment and a look at how the service is launched in production. The agent builds each version and runs it under env -u HOME in its sandbox, and the three outputs sit side by side in the pull request, so the behaviour change is seen before a server fails to come up.

The run also follows the dependency past the point where the source diff ends. Asking the module graph why an archived package is still present, and writing the answer down, is the step that usually gets skipped once the import is gone and the build is green.

Frequently asked questions

Is os.UserHomeDir a drop-in replacement for homedir.Dir?

Only while HOME is set. On Unix os.UserHomeDir returns the variable's value or an error when it is empty. go-homedir kept going and looked the current user up. The run shows the difference directly: the old build starts under env -u HOME and the bare swap exits with $HOME is not defined.

What replaces homedir.Expand?

Nothing in the standard library. The usual substitute is a short helper that checks for a leading ~/ and joins the rest onto the home directory. This repository never calls Expand, so no helper is added here; the prompt asks for one, with a test, when the function is used.

Why is go-homedir still in go.sum after the change?

Another dependency imports it. In this repository the chain runs from the Consul storage backend through github.com/hashicorp/consul/api and go-rootcerts. go mod why -m prints it, and the agent includes that output so the leftover lines are explained.

Does the fallback need cgo?

No. The agent builds the final version with CGO_ENABLED=0 and runs it with HOME unset, and it starts. That check is in the pull request because static builds are common for tools shipped as a single binary.

More jobs like this: Upgrade dependencies weekly

Keep reading