DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/Binary/Build/PostHogPlugin/HydrateRuntimeEnvironment.rs
1//! Hydrate the running process's environment from the compile-baked
2//! `Constants` so child processes spawned later (Cocoon Node, Sky
3//! webview) see the same telemetry config Mountain itself was built
4//! with - even when the user runs the bare binary without sourcing
5//! `.env.Land.PostHog`.
6//!
7//! Idempotent: skips any var that's already set so a CI / dev-shell
8//! override beats the build-time default. Mountain's
9//! `ProcessManagement::CocoonManagement::LandEnvAllowList` then
10//! forwards each value into Cocoon via `Command.envs()`. Sky reads
11//! the same values via `import.meta.env` substitution at Vite/Astro
12//! build time.
13//!
14//! Release builds skip the hydration: `cfg!(debug_assertions)` is
15//! `false`, so the body short-circuits and no telemetry env leaks
16//! into a packaged production binary.
17
18use crate::{Binary::Build::PostHogPlugin::Constants, dev_log};
19
20pub fn Fn() {
21 if !cfg!(debug_assertions) {
22 return;
23 }
24
25 for (Key, Value) in [
26 ("Authorize", Constants::POSTHOG_API_KEY),
27 ("Beam", Constants::POSTHOG_HOST),
28 ("Report", Constants::POSTHOG_ENABLED),
29 ("Brand", Constants::POSTHOG_DISTINCT_ID_SEED),
30 ("Pipe", Constants::OTLP_ENDPOINT),
31 ("Emit", Constants::OTLP_ENABLED),
32 ("Capture", Constants::TELEMETRY_CAPTURE),
33 ] {
34 if Value.is_empty() {
35 continue;
36 }
37
38 // Already-set values win; this hydration is a fallback for the
39 // "user runs bare binary" path.
40 if std::env::var_os(Key).is_some() {
41 continue;
42 }
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={}, Emit={})",
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}