Skip to content

Commit b958a5b

Browse files
committed
refactor(runner): pass extra env via config instead of unsafe set_var
Replace the `unsafe { std::env::set_var }` call for CODSPEED_MEMTRACK_BINARIES with a generic `extra_env` HashMap on OrchestratorConfig/ExecutorConfig that gets forwarded to executor subprocesses through the existing env file mechanism.
1 parent 106e731 commit b958a5b

6 files changed

Lines changed: 24 additions & 11 deletions

File tree

src/cli/exec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::project_config::ProjectConfig;
99
use crate::project_config::merger::ConfigMerger;
1010
use crate::upload::poll_results::PollResultsOptions;
1111
use clap::Args;
12+
use std::collections::HashMap;
1213
use std::path::Path;
1314
use url::Url;
1415

@@ -90,6 +91,7 @@ fn build_orchestrator_config(
9091
go_runner_version: args.shared.go_runner_version,
9192
show_full_output: args.shared.show_full_output,
9293
poll_results_options: PollResultsOptions::for_exec(),
94+
extra_env: HashMap::new(),
9395
})
9496
}
9597

src/cli/run/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::project_config::merger::ConfigMerger;
1010
use crate::run_environment::interfaces::RepositoryProvider;
1111
use crate::upload::poll_results::PollResultsOptions;
1212
use clap::{Args, ValueEnum};
13+
use std::collections::HashMap;
1314
use std::path::Path;
1415
use url::Url;
1516

@@ -130,6 +131,7 @@ fn build_orchestrator_config(
130131
go_runner_version: args.shared.go_runner_version,
131132
show_full_output: args.shared.show_full_output,
132133
poll_results_options,
134+
extra_env: HashMap::new(),
133135
})
134136
}
135137

src/executor/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::runner_mode::RunnerMode;
66
use crate::upload::poll_results::PollResultsOptions;
77
use clap::ValueEnum;
88
use semver::Version;
9+
use std::collections::HashMap;
910
use std::path::PathBuf;
1011
use url::Url;
1112

@@ -76,6 +77,8 @@ pub struct OrchestratorConfig {
7677
pub show_full_output: bool,
7778
/// Options controlling post-upload result polling and display
7879
pub poll_results_options: PollResultsOptions,
80+
/// Additional environment variables forwarded to executor subprocesses.
81+
pub extra_env: HashMap<String, String>,
7982
}
8083

8184
/// Per-execution configuration passed to executors.
@@ -103,6 +106,8 @@ pub struct ExecutorConfig {
103106
pub allow_empty: bool,
104107
/// The version of go-runner to install (if None, installs latest)
105108
pub go_runner_version: Option<Version>,
109+
/// Additional environment variables forwarded to executor subprocesses.
110+
pub extra_env: HashMap<String, String>,
106111
}
107112

108113
#[derive(Debug, Clone, PartialEq)]
@@ -169,6 +174,7 @@ impl OrchestratorConfig {
169174
skip_setup: self.skip_setup,
170175
allow_empty: self.allow_empty,
171176
go_runner_version: self.go_runner_version.clone(),
177+
extra_env: self.extra_env.clone(),
172178
}
173179
}
174180
}
@@ -205,6 +211,7 @@ impl OrchestratorConfig {
205211
go_runner_version: None,
206212
show_full_output: false,
207213
poll_results_options: PollResultsOptions::for_exec(),
214+
extra_env: HashMap::new(),
208215
}
209216
}
210217
}

src/executor/helpers/env.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub fn get_base_injected_env(
77
mode: RunnerMode,
88
profile_folder: &Path,
99
config: &ExecutorConfig,
10-
) -> HashMap<&'static str, String> {
10+
) -> HashMap<String, String> {
1111
let runner_mode_internal_env_value = match mode {
1212
// While the runner now deprecates the usage of instrumentation with a message, we
1313
// internally still use instrumentation temporarily to give time to users to upgrade their
@@ -20,31 +20,33 @@ pub fn get_base_injected_env(
2020
RunnerMode::Memory => "memory",
2121
};
2222
let mut env = HashMap::from([
23-
("PYTHONHASHSEED", "0".into()),
23+
("PYTHONHASHSEED".into(), "0".into()),
2424
(
25-
"PYTHON_PERF_JIT_SUPPORT",
25+
"PYTHON_PERF_JIT_SUPPORT".into(),
2626
if mode == RunnerMode::Walltime {
2727
"1".into()
2828
} else {
2929
"0".into()
3030
},
3131
),
32-
("ARCH", ARCH.into()),
33-
("CODSPEED_ENV", "runner".into()),
32+
("ARCH".into(), ARCH.into()),
33+
("CODSPEED_ENV".into(), "runner".into()),
3434
(
35-
"CODSPEED_RUNNER_MODE",
35+
"CODSPEED_RUNNER_MODE".into(),
3636
runner_mode_internal_env_value.into(),
3737
),
3838
(
39-
"CODSPEED_PROFILE_FOLDER",
39+
"CODSPEED_PROFILE_FOLDER".into(),
4040
profile_folder.to_string_lossy().to_string(),
4141
),
4242
]);
4343

4444
if let Some(version) = &config.go_runner_version {
45-
env.insert("CODSPEED_GO_RUNNER_VERSION", version.to_string());
45+
env.insert("CODSPEED_GO_RUNNER_VERSION".into(), version.to_string());
4646
}
4747

48+
env.extend(config.extra_env.clone());
49+
4850
env
4951
}
5052

src/executor/helpers/run_with_env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn get_exported_system_env() -> Result<String> {
3434
/// - NamedTempFile is the environment file that must be kept alive until command execution
3535
pub fn wrap_with_env(
3636
mut cmd_builder: CommandBuilder,
37-
extra_env: &HashMap<&'static str, String>,
37+
extra_env: &HashMap<String, String>,
3838
) -> Result<(CommandBuilder, NamedTempFile)> {
3939
let env_file = create_env_file(extra_env)?;
4040

@@ -50,7 +50,7 @@ pub fn wrap_with_env(
5050
Ok((cmd_builder, env_file))
5151
}
5252

53-
fn create_env_file(extra_env: &HashMap<&'static str, String>) -> Result<NamedTempFile> {
53+
fn create_env_file(extra_env: &HashMap<String, String>) -> Result<NamedTempFile> {
5454
let system_env = get_exported_system_env()?;
5555
let base_injected_env = extra_env
5656
.iter()

src/executor/wall_time/executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ impl WallTimeExecutor {
104104
&execution_context.profile_folder,
105105
&execution_context.config,
106106
);
107-
extra_env.insert("PATH", path_value);
107+
extra_env.insert("PATH".into(), path_value);
108108

109109
// We have to write the benchmark command to a script, to ensure proper formatting
110110
// and to not have to manually escape everything.

0 commit comments

Comments
 (0)