fix(core): skip analytics and DB connection when global bin hands off to local#34914
Conversation
✅ Deploy Preview for nx-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Deploy Preview for nx-dev ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
View your CI Pipeline Execution ↗ for commit 3247137
☁️ Nx Cloud last updated this comment at |
aa31c3e to
6a283ed
Compare
… 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.
6a283ed to
b5d47b2
Compare
There was a problem hiding this comment.
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.
🔔 Heads up, your workspace has pending recommendations ↗ to auto-apply fixes for similar failures.
🎓 Learn more about Self-Healing CI on nx.dev
… 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>
…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 -->
|
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. |
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:ensureAnalyticsPreferenceSet()— prompts user if analytics preference not setstartAnalytics()— which callsgetDbConnection()→connectToNxDb(directory, NX_VERSION)using the global Nx version and native bindings, then callsinitializeTelemetry(dbConnection, ...)to initialize telemetryNX_ANALYTICS_SESSION_IDenv varThen it checks which execution path to take:
isNxCloudCommand— executes commands directly (no handoff)isLocalInstall— the global IS the local, callsinitLocal()(no handoff)localNxexists — hands off to the local Nx viarequire(localNx)In the handoff case, the local
bin/nx.tsrunsmain()from scratch, which callsstartAnalytics()again. It seesNX_ANALYTICS_SESSION_IDis already set and takes the "reuse session" shortcut — but telemetry was already initialized with the wrong version's native bindings and DB connection.Problems:
NX_VERSION(global version, not local version) and wrong native bindingsExpected 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 directlyisLocalInstall— needs analytics becauseinitLocal()doesn't re-enterbin/nx.tsRelated Issue(s)