Skip to content

perf(generate): skip nix check and telemetry for direnv --print-envrc - #3022

Merged
mikeland73 merged 3 commits into
mainfrom
mikeland73/perf-direnv-print-envrc
Oct 26, 2026
Merged

mikeland73 merged 3 commits into
mainfrom
mikeland73/perf-direnv-print-envrc

Conversation

@mikeland73

Copy link
Copy Markdown
Collaborator

Summary

Implements item 5 of specs/perf-improvements.md.

direnv runs devbox generate direnv --print-envrc on every reload. It only prints a fixed .envrc template, but it still:

  • inherited ensureNixInstalled from generate's PersistentPreRunE, which runs nix --version, and
  • recorded a telemetry event, which also spawned the devbox upload-telemetry process.

Changes:

  • generate's PersistentPreRunE skips the nix check when --print-envrc is set. The root uses cobra.EnableTraverseRunHooks, so overriding the hook on the direnv subcommand wouldn't stop the parent's hook from running. That's why the check goes in the parent.
  • New midcobra.SkipTelemetry(cmd) lets a command opt out of being recorded for the current run. The telemetry post-run hook checks for it. The --print-envrc path calls it.

Testing

  • New TestGenerateDirenvPrintEnvrcSkipsNixCheck runs the command through RootCmd() and checks the output and that nix.Ensured() stays false. Without the fix it fails.
  • New TestSkipTelemetry.
  • go test ./internal/boxcli/... passes. golangci-lint run ./internal/boxcli/... reports 0 issues.
  • Output is byte-for-byte the same as on main.
  • Timing for generate direnv --print-envrc on macOS, averaged over 20 runs. Binaries were built with fake telemetry keys and network was blocked by a proxy.
main this PR
telemetry on 131.0ms 33.7ms
DO_NOT_TRACK=1 92.7ms 10.6ms

With telemetry on, almost all of the remaining ~23ms is telemetry.Start() running ioreg for the machine ID. #3021 caches that.

🤖 Generated with Claude Code

mikeland73 and others added 2 commits October 25, 2026 14:37
direnv runs `devbox generate direnv --print-envrc` on every reload. It
only prints a fixed .envrc template, but it inherited the nix install
check from `generate` (which runs `nix --version`) and recorded a
telemetry event, which also spawned the telemetry uploader.

Skip the nix check in generate's PersistentPreRunE when --print-envrc is
set, and add midcobra.SkipTelemetry so the command can opt out of being
recorded.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@mikeland73

Copy link
Copy Markdown
Collaborator Author

@claude ptal

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@claude

claude Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

PR #3022 was updated after the trigger (2026-10-25T21:46:37Z >= 2026-10-25T21:44:10Z). Mention Claude again to run on the latest version.

@mikeland73

Copy link
Copy Markdown
Collaborator Author

@claude ptal

@claude

claude Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Claude finished @mikeland73's task in 17s —— View job


Review of PR #3022

Looks good to me. I found no blocking issues. I read the diff and the surrounding code but didn't run the tests or build the binary.

  • Read the diff and surrounding code
  • Check the telemetry skip path and the nix-check skip

What I checked

  • Nix check: generate's PersistentPreRunE now returns early when --print-envrc is set. The reasoning for putting it in the parent holds, because EnableTraverseRunHooks means overriding the hook on direnv wouldn't stop the parent's hook. isPrintEnvrc returns false when the flag isn't defined (GetBool errors), so the other generate subcommands still run ensureNixInstalled.
  • Telemetry: getSubcommand in postRun uses cmd.Find(args), which returns the same *cobra.Command that ran. The annotation set by SkipTelemetry(cmd) in runGenerateDirenvCmd is therefore visible to telemetrySkipped(subcmd). postRun still defers telemetry.Stop() before the early return, so cleanup is unchanged.
  • Tests: TestGenerateDirenvPrintEnvrcSkipsNixCheck exercises the real RootCmd() path. TestSkipTelemetry covers the helper.

Minor, non-blocking notes

  1. TestGenerateDirenvPrintEnvrcSkipsNixCheck skips if nix.Ensured() is already true from an earlier test. That keeps it from failing spuriously, but if some test starts calling ensureNixInstalled earlier in the package, this one will silently stop running. It's fine for now.
  2. telemetrySkipped checks != "", so any non-empty annotation value counts as skipped. That's fine, though comparing against "true" would be slightly stricter.
  3. preRun still calls telemetry.Start() for skipped commands, which is the ~23ms ioreg cost you mention. The PR description says perf(telemetry): reuse opened project, cache device ID, throttle uploads #3021 handles that, so this is expected.
  4. --print-envrc combined with --envrc-dir errors out in runGenerateDirenvCmd, and that happens after the nix check is skipped. This only changes behavior for an invalid flag combination, and the error is the same as before.

Overall, this is ready to merge once CI is green.

@mikeland73
mikeland73 merged commit 2a13c58 into main Oct 26, 2026
28 checks passed
@mikeland73
mikeland73 deleted the mikeland73/perf-direnv-print-envrc branch October 26, 2026 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant