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