Skip to content

restore theme dev analytics#7345

Open
EvilGenius13 wants to merge 1 commit intomainfrom
jf-fix-theme-dev-analytics
Open

restore theme dev analytics#7345
EvilGenius13 wants to merge 1 commit intomainfrom
jf-fix-theme-dev-analytics

Conversation

@EvilGenius13
Copy link
Copy Markdown
Contributor

@EvilGenius13 EvilGenius13 commented Apr 17, 2026

WHY are these changes introduced?

Pressing Ctrl+C in shopify theme dev was dropping the analytics event for the session. The keypress handler called process.exit() synchronously, so the post-run hook that fires reportAnalyticsEvent never ran, and the finally { logAnalyticsData } block in theme-command.ts was skipped for the same reason.

Theme dev holds a handful of long-lived handles (HTTP server, chokidar watcher, polling loop, session interval, raw-mode stdin), so draining the event loop naturally would mean writing cleanup for each one. Easier to match what app dev already does: emit the event inline, then hard-exit.

WHAT is this pull request doing?

Fires reportAnalyticsEvent inline in dev() right before process.exit(0).

  • New reportDevAnalytics() helper populates the shop hash metadata and emits the event.
  • A module-level hasReportedAnalyticsEvent flag prevents a double-emit if a late throw bubbles into errorHandler.
  • Replaced the synchronous process.exit() in the Ctrl+C keypress branch with an onClose callback so dev() can await the background job, run the analytics call, and then exit.

How to test your changes?

  1. Build the branch and run shopify theme dev against a development store.
  2. Make sure you can still do your normal navigating, updating files etc.
  3. Press Ctrl+C. Process should exit immediately.
  4. Check for the data via query. (I can help you with this if needed).

Automated coverage in services/dev.test.ts:

  • Ctrl+C fires analytics exactly once with {exitMode: 'ok'}, and process.exit(0) runs after the emission.
  • Re-invoking reportDevAnalytics is a no-op (double-emit guard).

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes
  • I've considered analytics changes to measure impact
  • The change is user-facing, so I've added a changelog entry with pnpm changeset add

@EvilGenius13 EvilGenius13 changed the title fix(theme): restore theme dev analytics by resolving backgroundJobPromise on exit restore theme dev analytics Apr 17, 2026
@EvilGenius13 EvilGenius13 requested a review from karreiro April 20, 2026 15:54
@EvilGenius13 EvilGenius13 force-pushed the jf-fix-theme-dev-analytics branch from 43acc70 to 1e43830 Compare April 20, 2026 17:26
@EvilGenius13 EvilGenius13 marked this pull request as ready for review April 20, 2026 17:57
@EvilGenius13 EvilGenius13 requested review from a team as code owners April 20, 2026 17:57
Copilot AI review requested due to automatic review settings April 20, 2026 17:57
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 aims to restore/ensure analytics capture for shopify theme dev sessions when users exit via Ctrl+C by switching from an immediate process.exit() to a “graceful shutdown” signal that allows the command lifecycle to complete.

Changes:

  • Changed the dev server “background job” promise to be resolvable (Promise<void>) and exposed its resolver to callers.
  • Updated the Ctrl+C keypress handler to invoke an onClose callback instead of calling process.exit() directly.
  • Added a changeset entry and a unit test asserting Ctrl+C triggers onClose.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
packages/theme/src/cli/utilities/theme-environment/theme-environment.ts Makes the background-job promise resolvable and returns its resolver.
packages/theme/src/cli/services/dev.ts Wires Ctrl+C to resolve the background promise and adds an explicit process.exit(0) after the main await.
packages/theme/src/cli/services/dev.test.ts Updates handler construction and adds a Ctrl+C test asserting onClose is called.
.changeset/fix-theme-dev-analytics.md Declares a patch change and describes the user-visible behavior.

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

Comment thread packages/theme/src/cli/services/dev.ts
Comment thread packages/theme/src/cli/services/dev.ts
Comment thread .changeset/fix-theme-dev-analytics.md Outdated
@EvilGenius13 EvilGenius13 marked this pull request as draft April 20, 2026 20:21
@EvilGenius13 EvilGenius13 removed the request for review from karreiro April 20, 2026 20:22
@EvilGenius13 EvilGenius13 force-pushed the jf-fix-theme-dev-analytics branch from 1e43830 to 0704dc7 Compare April 22, 2026 17:48
@EvilGenius13 EvilGenius13 force-pushed the jf-fix-theme-dev-analytics branch from 0704dc7 to 3f56c8d Compare May 4, 2026 14:50
@github-actions github-actions Bot added the Area: @shopify/theme @shopify/theme package issues label May 4, 2026
@EvilGenius13 EvilGenius13 temporarily deployed to breaking-change-approval May 5, 2026 18:12 — with GitHub Actions Inactive
@EvilGenius13 EvilGenius13 force-pushed the jf-fix-theme-dev-analytics branch from ddf74ac to 498adcd Compare May 5, 2026 18:15
@EvilGenius13 EvilGenius13 marked this pull request as ready for review May 5, 2026 19:40
When users pressed Ctrl+C to exit `shopify theme dev`, the keypress handler
called `process.exit()` synchronously, bypassing the oclif post-run hook
where analytics normally fire. As a result, no event reached Monorail for
clean Ctrl+C exits.

Fix by emitting `reportAnalyticsEvent({exitMode: 'ok'})` inline inside
`dev()` before `process.exit(0)` and populating `store_fqdn_hash` metadata
at the same point, since the `finally { logAnalyticsData }` in
`theme-command.ts` is also skipped by the hard exit. A module-level
`hasReportedAnalyticsEvent` guard prevents double-emit if a later throw
bubbles to `errorHandler`.

Adds a unit test that mocks `reportAnalyticsEvent` and asserts it fires
exactly once with the expected payload, with correct call order vs
`process.exit(0)` verified via `invocationCallOrder`. The test also
covers the once-per-exit invariant and the double-emit guard inline.
@EvilGenius13 EvilGenius13 force-pushed the jf-fix-theme-dev-analytics branch from 498adcd to 717c98f Compare May 6, 2026 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: @shopify/theme @shopify/theme package issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants