Skip to content

fix(core): skip analytics and DB connection when global bin hands off to local#34914

Merged
FrozenPandaz merged 4 commits into
masterfrom
fix-analytics-global
Mar 19, 2026
Merged

fix(core): skip analytics and DB connection when global bin hands off to local#34914
FrozenPandaz merged 4 commits into
masterfrom
fix-analytics-global

Conversation

@FrozenPandaz
Copy link
Copy Markdown
Contributor

Current Behavior

When the globally installed Nx binary (bin/nx.ts) runs, it goes through the following flow before determining whether to hand off to a local Nx installation:

  1. ensureAnalyticsPreferenceSet() — prompts user if analytics preference not set
  2. startAnalytics() — which calls getDbConnection()connectToNxDb(directory, NX_VERSION) using the global Nx version and native bindings, then calls initializeTelemetry(dbConnection, ...) to initialize telemetry
  3. Sets NX_ANALYTICS_SESSION_ID env var

Then it checks which execution path to take:

  • isNxCloudCommand — executes commands directly (no handoff)
  • isLocalInstall — the global IS the local, calls initLocal() (no handoff)
  • localNx exists — hands off to the local Nx via require(localNx)

In the handoff case, the local bin/nx.ts runs main() from scratch, which calls startAnalytics() again. It sees NX_ANALYTICS_SESSION_ID is already set and takes the "reuse session" shortcut — but telemetry was already initialized with the wrong version's native bindings and DB connection.

Problems:

  • The global bin opens a DB connection with the wrong NX_VERSION (global version, not local version) and wrong native bindings
  • Analytics is initialized twice — once from global (incorrect), once from local (correct but using session reuse path)
  • The DB connection opened by the global bin is unnecessary since the local Nx handles everything

Expected Behavior

When the global bin is about to hand off to a local Nx installation, it should not initialize analytics or open any DB connections. The local Nx will handle analytics initialization correctly with its own version and native bindings.

Analytics and DB connections should only be initialized in the two branches where the global bin actually executes commands itself:

  • isNxCloudCommand — needs analytics because it runs commands directly
  • isLocalInstall — needs analytics because initLocal() doesn't re-enter bin/nx.ts

Related Issue(s)

@FrozenPandaz FrozenPandaz requested a review from a team as a code owner March 18, 2026 20:18
@FrozenPandaz FrozenPandaz requested a review from AgentEnder March 18, 2026 20:18
@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 18, 2026

Deploy Preview for nx-docs ready!

Name Link
🔨 Latest commit 3247137
🔍 Latest deploy log https://app.netlify.com/projects/nx-docs/deploys/69bb5db3528c010008c7636e
😎 Deploy Preview https://deploy-preview-34914--nx-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 18, 2026

Deploy Preview for nx-dev ready!

Name Link
🔨 Latest commit 3247137
🔍 Latest deploy log https://app.netlify.com/projects/nx-dev/deploys/69bb5db3067afe0008195ccc
😎 Deploy Preview https://deploy-preview-34914--nx-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@nx-cloud
Copy link
Copy Markdown
Contributor

nx-cloud Bot commented Mar 18, 2026

View your CI Pipeline Execution ↗ for commit 3247137

Command Status Duration Result
nx affected --targets=lint,test,build,e2e,e2e-c... ✅ Succeeded 14m 47s View ↗
nx run-many -t check-imports check-lock-files c... ✅ Succeeded 9s View ↗
nx-cloud record -- nx-cloud conformance:check ✅ Succeeded 8s View ↗
nx-cloud record -- nx format:check ✅ Succeeded 1s View ↗
nx-cloud record -- nx sync:check ✅ Succeeded <1s View ↗

☁️ Nx Cloud last updated this comment at 2026-03-19 02:41:46 UTC

@FrozenPandaz FrozenPandaz enabled auto-merge (squash) March 18, 2026 20:24
@FrozenPandaz FrozenPandaz force-pushed the fix-analytics-global branch 2 times, most recently from aa31c3e to 6a283ed Compare March 18, 2026 21:18
… to local

When the global Nx binary detects a different local installation, it
hands off execution via require(localNx). Previously, it would initialize
analytics and open a DB connection before the handoff — using the wrong
NX_VERSION and native bindings. The local Nx then re-initializes
analytics correctly when it runs its own bin/nx.ts.

Move analytics initialization into only the branches that actually
execute locally (isLocalInstall and isNxCloudCommand), so the handoff
path stays lightweight and avoids unnecessary DB connections.
@FrozenPandaz FrozenPandaz force-pushed the fix-analytics-global branch from 6a283ed to b5d47b2 Compare March 18, 2026 21:37
@FrozenPandaz FrozenPandaz disabled auto-merge March 19, 2026 01:10
nx-cloud[bot]

This comment was marked as outdated.

Copy link
Copy Markdown
Contributor

@nx-cloud nx-cloud Bot left a comment

Choose a reason for hiding this comment

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

Nx Cloud has identified a flaky task in your failed CI:

🔂 Since the failure was identified as flaky, we triggered a CI rerun by adding an empty commit to this branch.

Nx Cloud View detailed reasoning in Nx Cloud ↗

🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.


🎓 Learn more about Self-Healing CI on nx.dev

@FrozenPandaz FrozenPandaz merged commit 259b823 into master Mar 19, 2026
22 checks passed
@FrozenPandaz FrozenPandaz deleted the fix-analytics-global branch March 19, 2026 03:31
FrozenPandaz added a commit that referenced this pull request Mar 20, 2026
… to local (#34914)

## Current Behavior

When the globally installed Nx binary (`bin/nx.ts`) runs, it goes
through the following flow before determining whether to hand off to a
local Nx installation:

1. `ensureAnalyticsPreferenceSet()` — prompts user if analytics
preference not set
2. `startAnalytics()` — which calls `getDbConnection()` →
`connectToNxDb(directory, NX_VERSION)` using the **global** Nx version
and native bindings, then calls `initializeTelemetry(dbConnection, ...)`
to initialize telemetry
3. Sets `NX_ANALYTICS_SESSION_ID` env var

Then it checks which execution path to take:
- **`isNxCloudCommand`** — executes commands directly (no handoff)
- **`isLocalInstall`** — the global IS the local, calls `initLocal()`
(no handoff)
- **`localNx` exists** — hands off to the local Nx via
`require(localNx)`

In the handoff case, the local `bin/nx.ts` runs `main()` from scratch,
which calls `startAnalytics()` again. It sees `NX_ANALYTICS_SESSION_ID`
is already set and takes the "reuse session" shortcut — but telemetry
was already initialized with the wrong version's native bindings and DB
connection.

**Problems:**
- The global bin opens a DB connection with the **wrong `NX_VERSION`**
(global version, not local version) and **wrong native bindings**
- Analytics is initialized twice — once from global (incorrect), once
from local (correct but using session reuse path)
- The DB connection opened by the global bin is unnecessary since the
local Nx handles everything

## Expected Behavior

When the global bin is about to hand off to a local Nx installation, it
should **not** initialize analytics or open any DB connections. The
local Nx will handle analytics initialization correctly with its own
version and native bindings.

Analytics and DB connections should only be initialized in the two
branches where the global bin actually executes commands itself:
- `isNxCloudCommand` — needs analytics because it runs commands directly
- `isLocalInstall` — needs analytics because `initLocal()` doesn't
re-enter `bin/nx.ts`

## Related Issue(s)

<!-- Internal discovery during analytics work -->

---------

Co-authored-by: nx-cloud[bot] <71083854+nx-cloud[bot]@users.noreply.github.com>
FrozenPandaz added a commit that referenced this pull request Mar 23, 2026
…local (#34953)

## Current Behavior

When the globally installed Nx binary (`bin/nx.ts`) runs, it calls
`setupWorkspaceContext()` **before** determining whether it should hand
off to a local Nx installation. If the global Nx version differs from
the local version (e.g. global `22.7.0-beta.2` vs local
`22.7.0-beta.1`), `isNxVersionMismatch()` returns true,
`daemonClient.enabled()` returns false, and `setupWorkspaceContext()`
creates an in-process `WorkspaceContext` that locks the workspace data
directory.

When the local Nx then tries to start the daemon, it conflicts with this
lock and the daemon fails to start. This means running `nx reset`
followed by any command (e.g. `nx show projects`) results in the daemon
never auto-starting, falling back to in-process graph construction, or
erroring out entirely.

## Expected Behavior

The global bin should not set up a workspace context when it's about to
hand off to a local Nx installation. The local Nx will handle workspace
context setup itself.

`setupWorkspaceContext()` is now only called in the `isLocalInstall` and
`isNxCloudCommand` branches — skipped in the handoff path. This matches
the existing pattern established in #34914 for analytics and DB
connections.

## Related Issue(s)

<!-- No specific issue filed — discovered during development in the nx
repo -->
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has already been merged/closed. If you experience issues related to these changes, please open a new issue referencing this pull request.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Mar 25, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants