Skip to main content

Mountain/Binary/Build/PostHogPlugin/
HydrateRuntimeEnvironment.rs

1
2//! Hydrate the running process's environment from the compile-baked
3//! `Constants` so child processes spawned later (Cocoon Node, Sky
4//! webview) see the same telemetry config Mountain itself was built
5//! with - even when the user runs the bare binary without sourcing
6//! `.env.Land.PostHog`.
7//!
8//! Idempotent: skips any var that's already set so a CI / dev-shell
9//! override beats the build-time default. Mountain's
10//! `ProcessManagement::CocoonManagement::LandEnvAllowList` then
11//! forwards each value into Cocoon via `Command.envs()`. Sky reads
12//! the same values via `import.meta.env` substitution at Vite/Astro
13//! build time.
14//!
15//! Release builds skip the hydration: `cfg!(debug_assertions)` is
16//! `false`, so the body short-circuits and no telemetry env leaks
17//! into a packaged production binary.
18
19use crate::{Binary::Build::PostHogPlugin::Constants, dev_log};
20
21pub fn Fn() {
22	if !cfg!(debug_assertions) {
23		return;
24	}
25
26	for (Key, Value) in [
27		("Authorize", Constants::POSTHOG_API_KEY),
28		("Beam", Constants::POSTHOG_HOST),
29		("Report", Constants::POSTHOG_ENABLED),
30		("Brand", Constants::POSTHOG_DISTINCT_ID_SEED),
31		("OTLPEndpoint", Constants::OTLP_ENDPOINT),
32		("OTLPEnabled", Constants::OTLP_ENABLED),
33		("Capture", Constants::TELEMETRY_CAPTURE),
34	] {
35		if Value.is_empty() {
36			continue;
37		}
38
39		// Already-set values win; this hydration is a fallback for the
40		// "user runs bare binary" path.
41		if std::env::var_os(Key).is_some() {
42			continue;
43		}
44
45		// SAFETY: set_var on a single-threaded boot path before any
46		// other thread spawns. Mountain calls this from the early boot
47		// section of Binary::Main::Entry::Fn before tokio / scheduler
48		// init.
49		unsafe { std::env::set_var(Key, Value) };
50	}
51
52	dev_log!(
53		"lifecycle",
54		"[PostHog] Hydrated runtime env from baked Constants (Authorize={}, Beam={}, Capture={}, OTLPEnabled={})",
55		if Constants::POSTHOG_API_KEY.is_empty() { "<unset>" } else { "<set>" },
56		Constants::POSTHOG_HOST,
57		Constants::TELEMETRY_CAPTURE,
58		Constants::OTLP_ENABLED,
59	);
60}