Draft
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.
When users press Ctrl+C to exit `shopify theme dev`, the keypress handler
called `process.exit()` synchronously — skipping the oclif post-run hook
where analytics normally fire, so no event reached Monorail.
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 real 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.
1e43830 to
0704dc7
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 devdropped analytics for the session. The keypress handler calledprocess.exit()synchronously, terminating the process before oclif's post-run hook (wherereportAnalyticsEventfires) could run. Thefinally { logAnalyticsData }block intheme-command.tsis skipped by the same hard exit.Theme dev holds 5 long-lived handles (HTTP server, chokidar watcher, polling
while(true), sessionsetInterval, stdin raw mode). Letting the event loop drain naturally would require bespoke cleanup plumbing for each one. Matchingapp dev's established pattern — emit analytics inline, then hard-exit — is smaller, safer, and provably works.WHAT is this pull request doing?
Emits the analytics event inline inside
dev()beforeprocess.exit(0):reportAnalyticsEventcall (with shop-hash metadata populated at the same point) before the hard exit, so telemetry lands even though the oclif post-run hook andtheme-command.ts'sfinallyblock are both skipped.hasReportedAnalyticsEventguard prevents double-emit if a late throw bubbles toerrorHandler.cmd_all_exit = 'ok'row within p50 lag.Out of scope (follow-ups to file):
packages/cli/src/index.ts:65-70(separate exit path for non-raw-mode TTY, stillprocess.exit(1)s and skips postrun).packages/theme/src/cli/utilities/theme-environment/html.tsredirect-overflow hard-exit.process.exit(0)could eventually be removed entirely.How to test your changes?
shopify theme devagainst a development store.cmd_all_exit = 'ok'analytics event lands in monorail for the session.Automated coverage added:
services/dev.test.tsmocksreportAnalyticsEvent, simulates Ctrl+C viaresolveBackgroundJob, and asserts the event fires exactly once with{exitMode: 'ok'}and thatprocess.exit(0)runs AFTER the emission (viainvocationCallOrder).reportDevAnalyticsfires exactly once).services/dev.test.tsstill green. Fullpackages/themesuite: 566/566 pass.Checklist
pnpm changeset add