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
40 // Already-set values win; this hydration is a fallback for the
41 // "user runs bare binary" path.
42 if std::env::var_os(Key).is_some() {
43 continue;
44 }
45
46 // SAFETY: set_var on a single-threaded boot path before any
47 // other thread spawns. Mountain calls this from the early boot
48 // section of Binary::Main::Entry::Fn before tokio / scheduler
49 // init.
50 unsafe { std::env::set_var(Key, Value) };
51 }
52
53 dev_log!(
54 "lifecycle",
55 "[PostHog] Hydrated runtime env from baked Constants (Authorize={}, Beam={}, Capture={}, OTLPEnabled={})",
56 if Constants::POSTHOG_API_KEY.is_empty() { "<unset>" } else { "<set>" },
57 Constants::POSTHOG_HOST,
58 Constants::TELEMETRY_CAPTURE,
59 Constants::OTLP_ENABLED,
60 );
61}