Summary
The tests in internal/cli run against the developer's own per-user state unless a test redirects it, and most do not. The clearest symptom is hooks: with a user-level hooks.json in place, go test ./internal/cli runs those hooks, thousands of times, with whatever the tests feed them. Depending on what the hooks do the run fails or goes past the timeout, and either way it has executed the developer's commands.
@FabioLeitao reported the first sign of this on #1234: TestNewHookDispatcherWithExtraFoldsPluginHooks fails on a machine with a user-level beforeTool hook.
Reproduction
Main at 06b39784, Windows 11. The same file is read on Linux and macOS, the path is $XDG_CONFIG_HOME/zero/hooks.json or ~/.config/zero/hooks.json.
- Make a directory, and in it
zero/hooks.jsonwith four enabled hooks (sessionStart,sessionEnd,beforeTool,afterTool), each a command that appends one line to a marker file. Nothing else. XDG_CONFIG_HOME=<that directory> go test ./internal/cli -count=1 -timeout 40m- Count the lines in the marker file.
What I measured
| the user hooks file | result on main |
|---|---|
| none (an empty config directory) | passes, about 2 minutes |
| four enabled marker hooks | hooks ran 2,161 times (827 sessionStart, 800 sessionEnd, 273 beforeTool, 261 afterTool); five tests failed (TestRunExecEscalationSwitchKeepsStoredKey, TestRunExecStreamJSONEmitsAndRecordsPermissionEvents, TestRunExecWarnsAboutScratchFileWhenProviderErrors, TestRunAddDirDispatchForwardsGrantIntoExecScope, TestRunExecUseSpecCreatesDraftSession); the package did not finish in 40 minutes |
only {"enabled": false} with two hooks listed | one test fails: TestNewHookDispatcherWithExtraFoldsPluginHooks, Ran=0 |
The override is not needed. With XDG_CONFIG_HOME unset and HOME and USERPROFILE pointing at a directory that holds .config/zero/hooks.json, the last row fails the same way. So the default path is read, and any developer with user-level hooks is affected today.
I ran every other package the same way, with the four marker hooks planted. None of them ran a hook and none failed, so this is internal/cli only. As a check that the scan could see something, a few TestRunExec tests on main under the same setting produced 346 lines.
I measured hooks because they leave a trace. The same tests resolve user config, plugins and stores from the same directories, and I have not measured those.
Cause
Tests here drive the root command, and runWithDeps fills whatever a test leaves out with the production wiring, which loads from the per-user directories. isolateCLIUserState redirects them, and 26 calls use it. The package has about 770 tests.
Direction
Redirect the per-user directories once for the whole package, before any test runs, and keep isolateCLIUserState for tests that need a root of their own. Asking several hundred tests to each remember a helper is what got us here. I have this working and will open the PR.
Activity
0No activity yet.