Skip to content

Commit 6943d0c

Browse files
committed
Wipe the env when compiling the user function
1 parent 89df8aa commit 6943d0c

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

plrust-tests/src/trusted.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ mod tests {
190190

191191
#[pg_test]
192192
#[search_path(@extschema@)]
193-
#[should_panic = "error: the `env` and `option_env` macros are forbidden"]
193+
#[should_panic = "environment variable `PATH` not defined at compile time."]
194194
#[cfg(feature = "trusted")]
195195
fn plrust_block_env() -> spi::Result<()> {
196196
let definition = r#"
@@ -204,15 +204,14 @@ mod tests {
204204

205205
#[pg_test]
206206
#[search_path(@extschema@)]
207-
#[should_panic = "error: the `env` and `option_env` macros are forbidden"]
207+
#[should_panic = "the `option_env` macro always returns `None` in the PL/Rust user function"]
208208
#[cfg(feature = "trusted")]
209209
fn plrust_block_option_env() -> spi::Result<()> {
210210
let definition = r#"
211211
CREATE FUNCTION try_get_path() RETURNS text AS $$
212-
match option_env!("PATH") {
213-
None => Ok(None),
214-
Some(s) => Ok(Some(s.to_string()))
215-
}
212+
let v = option_env!("PATH")
213+
.expect("the `option_env` macro always returns `None` in the PL/Rust user function");
214+
Ok(Some(v.to_string()))
216215
$$ LANGUAGE plrust;
217216
"#;
218217
Spi::run(definition)

plrustc/plrustc/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ impl Callbacks for PlrustcCallbacks {
4949
}
5050
}
5151
}
52+
fn clear_env() {
53+
let all_var_names = std::env::vars_os()
54+
.map(|(name, _)| name)
55+
.collect::<Vec<_>>();
56+
for name in all_var_names {
57+
std::env::remove_var(name);
58+
}
59+
}
5260

5361
fn main() {
5462
rustc_driver::install_ice_hook("https://github.com/tcdi/plrust/issues/new", |_| ());
@@ -58,6 +66,9 @@ fn main() {
5866
let args =
5967
rustc_driver::args::arg_expand_all(handler, &std::env::args().collect::<Vec<_>>());
6068
let config = PlrustcConfig::from_env_and_args(&args);
69+
if config.compiling_user_crate() {
70+
clear_env();
71+
}
6172
run_compiler(
6273
args,
6374
&mut PlrustcCallbacks {

0 commit comments

Comments
 (0)