Skip to content

fix(cli): support DO_NOT_TRACK and ensure lazy telemetry initialization#2066

Open
Skywalkingzulu1 wants to merge 1 commit into
codemod:mainfrom
Skywalkingzulu1:fix/telemetry-leak-and-windows-spawn-einval
Open

fix(cli): support DO_NOT_TRACK and ensure lazy telemetry initialization#2066
Skywalkingzulu1 wants to merge 1 commit into
codemod:mainfrom
Skywalkingzulu1:fix/telemetry-leak-and-windows-spawn-einval

Conversation

@Skywalkingzulu1
Copy link
Copy Markdown

Summary

This PR addresses Issue #2058 (telemetry leak) and a Windows-specific \spawn EINVAL\ error.

Changes

  • DO_NOT_TRACK Support: Added support for the industry-standard \DO_NOT_TRACK\ environment variable in \main.rs.
  • Lazy Telemetry Initialization: Refactored \PostHogSender\ in \crates/telemetry\ to use \OnceCell. The PostHog client is now only initialized on the first \send_event\ call, preventing early network handshakes even if analytics are enabled.
  • Windows Fix: Added \shell: true\ to the CLI's \spawn\ implementation in \crates/cli/npm/codemod\ to resolve \EINVAL\ errors when executing .cmd\ files on Windows.

Verified locally in a win32 environment.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Apr 3, 2026

CLA assistant check
All committers have signed the CLA.

- Support industry-standard DO_NOT_TRACK environment variable.
- Defer PostHog client initialization until the first send_event call.
- Fix Windows spawn EINVAL error by adding shell: true for .cmd execution.
@Skywalkingzulu1 Skywalkingzulu1 force-pushed the fix/telemetry-leak-and-windows-spawn-einval branch from f660713 to e0c2b5d Compare April 3, 2026 10:51
@mohebifar mohebifar requested a review from Copilot April 3, 2026 16:02
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the CLI telemetry pipeline to better respect user opt-out signals and to avoid initializing the PostHog client until it’s actually needed, and it also fixes a Windows-specific spawn failure in the npm wrapper.

Changes:

  • Add support for DO_NOT_TRACK to disable analytics in the CLI entrypoint.
  • Make PostHogSender lazily initialize the PostHog client via tokio::sync::OnceCell (no client creation during sender construction).
  • On Windows, spawn the npm bootstrap command with shell: true to avoid .cmd EINVAL errors.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
crates/telemetry/src/send_event.rs Refactors PostHog sender to lazily create the client on first use (and in the panic hook).
crates/cli/src/main.rs Expands analytics-disable detection to include DO_NOT_TRACK (and env-driven disablement).
crates/cli/npm/codemod Adds shell: true for win32 when spawning the bootstrap npm.cmd path.
Comments suppressed due to low confidence (1)

crates/cli/npm/codemod:155

  • The new Windows-only shell behavior isn’t covered by the existing wrapper tests (they only exercise the linux path). Adding a unit test case that runs run({ argv: [], platform: "win32", ... }) and asserts spawnImpl receives { shell: true } would help prevent regressions for the .cmd EINVAL fix.
function spawnLatestCli({ env = process.env, platform = process.platform, spawnImpl = spawn }) {
  return spawnImpl(npmExecutable(platform), latestBootstrapArgs(), {
    stdio: "inherit",
    shell: platform === "win32",
    env: {
      ...env,
      [CODEMOD_BOOTSTRAPPED_ENV]: "1",
    },
  });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/cli/src/main.rs
Comment on lines +393 to +399
let is_analytics_disabled = cli.disable_analytics
|| std::env::var("DO_NOT_TRACK").is_ok()
|| std::env::var("DISABLE_ANALYTICS").is_ok()
|| implicit_cli_params
.as_ref()
.map(|params| params.disable_analytics)
.unwrap_or(false)
{
.unwrap_or(false);
Copy link

Copilot AI Apr 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DO_NOT_TRACK and DISABLE_ANALYTICS are treated as disabled when the env var merely exists (even if set to "0"/"false"/empty). This is a behavior change that can unintentionally force-disable analytics. Consider parsing these env vars the same way you later do for DISABLE_ANALYTICS (e.g., only treat "1"/"true" (case-insensitive) as disabled), and keep --disable-analytics as the explicit override.

Copilot uses AI. Check for mistakes.
@mohebifar
Copy link
Copy Markdown
Member

Thank you so much! @Skywalkingzulu1 Could you please address the comment by Copilot?

@alexbit-codemod
Copy link
Copy Markdown
Contributor

@Skywalkingzulu1 do you want to wrap this up so we can get it merged? thanks for the great work and contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants