restore theme dev analytics#7345
Open
EvilGenius13 wants to merge 1 commit intomainfrom
Open
Conversation
43acc70 to
1e43830
Compare
Contributor
There was a problem hiding this comment.
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
onClosecallback instead of callingprocess.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.
1e43830 to
0704dc7
Compare
0704dc7 to
3f56c8d
Compare
ddf74ac to
498adcd
Compare
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.
498adcd to
717c98f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
Pressing Ctrl+C in
shopify theme devwas dropping the analytics event for the session. The keypress handler calledprocess.exit()synchronously, so the post-run hook that firesreportAnalyticsEventnever ran, and thefinally { logAnalyticsData }block intheme-command.tswas 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 devalready does: emit the event inline, then hard-exit.WHAT is this pull request doing?
Fires
reportAnalyticsEventinline indev()right beforeprocess.exit(0).reportDevAnalytics()helper populates the shop hash metadata and emits the event.hasReportedAnalyticsEventflag prevents a double-emit if a late throw bubbles intoerrorHandler.process.exit()in the Ctrl+C keypress branch with anonClosecallback sodev()can await the background job, run the analytics call, and then exit.How to test your changes?
shopify theme devagainst a development store.Automated coverage in
services/dev.test.ts:{exitMode: 'ok'}, andprocess.exit(0)runs after the emission.reportDevAnalyticsis a no-op (double-emit guard).Checklist
pnpm changeset add