Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crates/runner-shared/src/runtime_env/go.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const GODEBUG: &str = "GODEBUG";
/// Starts the global `math/rand` source from `Seed(1)` instead of a random seed.
const RANDAUTOSEED_OFF: &str = "randautoseed=0";

/// Go env:
/// - `GODEBUG=randautoseed=0`: deterministic global `math/rand` in every mode.
pub(super) fn env() -> Vec<(&'static str, String)> {
let user_godebug = std::env::var(GODEBUG).ok();
vec![(GODEBUG, godebug(user_godebug.as_deref()))]
}

/// Prepends our settings to the user's `GODEBUG`. Go uses the last occurrence
/// of a setting, so an explicit `randautoseed` from the user still wins.
fn godebug(user_godebug: Option<&str>) -> String {
match user_godebug {
Some(user) if !user.is_empty() => format!("{RANDAUTOSEED_OFF},{user}"),
_ => RANDAUTOSEED_OFF.to_owned(),
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn keeps_user_settings_after_ours() {
assert_eq!(godebug(None), "randautoseed=0");
assert_eq!(godebug(Some("")), "randautoseed=0");
assert_eq!(
godebug(Some("gctrace=1,randautoseed=1")),
"randautoseed=0,gctrace=1,randautoseed=1"
);
}
}
2 changes: 2 additions & 0 deletions crates/runner-shared/src/runtime_env/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::measurement_mode::MeasurementMode;

mod go;
mod java;
mod python;

Expand All @@ -10,5 +11,6 @@ pub fn env(mode: MeasurementMode) -> Vec<(&'static str, String)> {
let mut env = vec![(RUNNER_MODE_ENV, mode.runner_mode_env_value().to_owned())];
env.extend(python::env(mode));
env.extend(java::env(mode));
env.extend(go::env());
env
}
Loading