diff --git a/plrust-tests/src/trusted.rs b/plrust-tests/src/trusted.rs index 33b373a6..04986420 100644 --- a/plrust-tests/src/trusted.rs +++ b/plrust-tests/src/trusted.rs @@ -190,31 +190,32 @@ mod tests { #[pg_test] #[search_path(@extschema@)] - #[should_panic = "error: the `env` and `option_env` macros are forbidden"] + #[should_panic = "environment variable `FOOBAR` not defined at compile time"] #[cfg(feature = "trusted")] fn plrust_block_env() -> spi::Result<()> { let definition = r#" - CREATE FUNCTION get_path() RETURNS text AS $$ - let path = env!("PATH"); - Ok(Some(path.to_string())) + CREATE FUNCTION get_foobar() RETURNS text AS $$ + let foo = env!("FOOBAR"); + Ok(Some(foo.to_string())) $$ LANGUAGE plrust; "#; + std::env::set_var("FOOBAR", "1"); Spi::run(definition) } #[pg_test] #[search_path(@extschema@)] - #[should_panic = "error: the `env` and `option_env` macros are forbidden"] + #[should_panic = "the `option_env` macro always returns `None` in the PL/Rust user function"] #[cfg(feature = "trusted")] fn plrust_block_option_env() -> spi::Result<()> { let definition = r#" - CREATE FUNCTION try_get_path() RETURNS text AS $$ - match option_env!("PATH") { - None => Ok(None), - Some(s) => Ok(Some(s.to_string())) - } + CREATE FUNCTION try_get_foobar2() RETURNS text AS $$ + let v = option_env!("FOOBAR2") + .expect("the `option_env` macro always returns `None` in the PL/Rust user function"); + Ok(Some(v.to_string())) $$ LANGUAGE plrust; "#; + std::env::set_var("FOOBAR2", "1"); Spi::run(definition) } diff --git a/plrustc/plrustc/src/main.rs b/plrustc/plrustc/src/main.rs index 2d712d6e..47ac19bb 100644 --- a/plrustc/plrustc/src/main.rs +++ b/plrustc/plrustc/src/main.rs @@ -50,6 +50,16 @@ impl Callbacks for PlrustcCallbacks { } } +fn clear_env() { + for (name, _) in std::env::vars_os() { + // Can't remove `PATH`, need it to locate linker and such. + if name == "PATH" { + continue; + } + std::env::remove_var(name); + } +} + fn main() { rustc_driver::install_ice_hook("https://github.com/tcdi/plrust/issues/new", |_| ()); let handler = &EarlyErrorHandler::new(ErrorOutputType::default()); @@ -58,6 +68,9 @@ fn main() { let args = rustc_driver::args::arg_expand_all(handler, &std::env::args().collect::>()); let config = PlrustcConfig::from_env_and_args(&args); + if config.compiling_user_crate() { + clear_env(); + } run_compiler( args, &mut PlrustcCallbacks {