Skip to content

Commit b26365c

Browse files
authored
fix(cli): read the telemetry token per call so tests don't depend on the publish job's env (#7822)
1 parent c3ccda5 commit b26365c

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

‎packages/sim-cli/src/commands/telemetry.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ let output: string[]
1212
beforeEach(() => {
1313
dir = mkdtempSync(join(tmpdir(), 'sim-telemetry-'))
1414
vi.stubEnv('SIM_CONFIG_DIR', dir)
15+
/** Pinned so the result never depends on the shell or CI job running the suite. */
16+
vi.stubEnv('SIM_CLI_TELEMETRY_KEY', '')
17+
vi.stubEnv('DO_NOT_TRACK', '')
18+
vi.stubEnv('SIM_TELEMETRY_DISABLED', '')
1519
output = []
1620
vi.spyOn(console, 'log').mockImplementation((line: string) => {
1721
output.push(line)
@@ -54,4 +58,12 @@ describe('sim telemetry', () => {
5458
expect(output[0]).toMatch(/off: this build has no reporting destination/)
5559
expect(output[1]).toContain('https://docs.sim.ai/cli/usage-data')
5660
})
61+
62+
it('reports on for a build with a destination', async () => {
63+
vi.stubEnv('SIM_CLI_TELEMETRY_KEY', 'phc_test')
64+
65+
await run('status')
66+
67+
expect(output[0]).toBe('Usage reporting is on.')
68+
})
5769
})

‎packages/sim-cli/src/telemetry/transport.ts‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
import { type ChildProcess, spawn } from 'node:child_process'
22
import { childProcessEnv, proxyExecArgv } from '../environment'
33

4-
/**
5-
* Where events go. The key is a PostHog project token — a public, write-only
6-
* value — baked into the published build by `bun build --env='SIM_CLI_TELEMETRY_*'`
7-
* the way the Supabase CLI injects its own at link time. A checkout built
8-
* without one has no destination and reports nothing, and a self-hosted
9-
* deployment can point its own build at its own project.
10-
*
11-
* These two reads must stay literal `process.env.<NAME>` expressions: that is
12-
* the only form the bundler substitutes.
13-
*/
14-
const BUILT_IN_KEY = process.env.SIM_CLI_TELEMETRY_KEY
15-
const BUILT_IN_HOST = process.env.SIM_CLI_TELEMETRY_HOST
16-
174
export const DEFAULT_INGEST_HOST = 'https://us.i.posthog.com'
185

196
/** The single-event capture endpoint, relative to the ingest host. */
@@ -33,10 +20,22 @@ export interface IngestTarget {
3320
host: string
3421
}
3522

36-
/** The destination this build was made with, if any. */
23+
/**
24+
* The destination this build was made with, if any. The key is a PostHog
25+
* project token — a public, write-only value — baked into the published build
26+
* by `bun build --env='SIM_CLI_TELEMETRY_*'` the way the Supabase CLI injects
27+
* its own at link time. A checkout built without one has no destination and
28+
* reports nothing, and a self-hosted deployment can point its own build at its
29+
* own project.
30+
*
31+
* Both reads must stay literal `process.env.<NAME>` expressions, the only form
32+
* the bundler substitutes. They are made on each call rather than at module
33+
* load so that, unbundled, the answer follows the environment a caller set.
34+
*/
3735
export function builtInIngestTarget(): IngestTarget | undefined {
38-
if (!BUILT_IN_KEY) return undefined
39-
return { key: BUILT_IN_KEY, host: BUILT_IN_HOST || DEFAULT_INGEST_HOST }
36+
const key = process.env.SIM_CLI_TELEMETRY_KEY
37+
if (!key) return undefined
38+
return { key, host: process.env.SIM_CLI_TELEMETRY_HOST || DEFAULT_INGEST_HOST }
4039
}
4140

4241
/** One event in the shape PostHog's capture endpoint accepts. */

0 commit comments

Comments
 (0)