From 383bf7d31955e535476e95b85052a852141f7267 Mon Sep 17 00:00:00 2001 From: Eric Ridge Date: Wed, 25 Jan 2023 09:41:54 -0500 Subject: [PATCH] If there's a "pg_config" described in the environment, use it. (#1020) --- pgx-pg-config/src/lib.rs | 13 ++++++------- pgx-pg-sys/build.rs | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/pgx-pg-config/src/lib.rs b/pgx-pg-config/src/lib.rs index b71c6b8d2..d8ca9d1da 100644 --- a/pgx-pg-config/src/lib.rs +++ b/pgx-pg-config/src/lib.rs @@ -138,9 +138,9 @@ impl PgConfig { /// /// It also requires that the `PGX_PG_CONFIG_AS_ENV` variable be set to some value that isn't /// the string `"false"`. - pub fn from_env() -> Option { + pub fn from_env() -> eyre::Result { if !Self::is_in_environment() { - None + Err(eyre::eyre!("`PgConfig` not described in the environment")) } else { const PREFIX: &str = "PGX_PG_CONFIG_"; @@ -153,7 +153,7 @@ impl PgConfig { } } - Some(Self { + Ok(Self { version: None, pg_config: None, known_props: Some(known_props), @@ -466,8 +466,7 @@ impl Pgx { ) -> impl std::iter::Iterator> { match (which, PgConfig::is_in_environment()) { (PgConfigSelector::All, true) | (PgConfigSelector::Environment, _) => { - vec![PgConfig::from_env().ok_or(eyre!("PGX_PG_CONFIG_AS_ENV not found"))] - .into_iter() + vec![PgConfig::from_env()].into_iter() } (PgConfigSelector::All, _) => { @@ -677,7 +676,7 @@ fn parse_version() { fn from_empty_env() -> eyre::Result<()> { // without "PGX_PG_CONFIG_AS_ENV" we can't get one of these let pg_config = PgConfig::from_env(); - assert!(pg_config.is_none()); + assert!(pg_config.is_err()); // but now we can std::env::set_var("PGX_PG_CONFIG_AS_ENV", "true"); @@ -685,7 +684,7 @@ fn from_empty_env() -> eyre::Result<()> { std::env::set_var("PGX_PG_CONFIG_INCLUDEDIR-SERVER", "/path/to/server/headers"); std::env::set_var("PGX_PG_CONFIG_CPPFLAGS", "some cpp flags"); - let pg_config = PgConfig::from_env().expect("failed to make a PgConfig from the environment"); + let pg_config = PgConfig::from_env().unwrap(); assert_eq!(pg_config.major_version()?, 15, "Major version should match"); assert_eq!(pg_config.minor_version()?, 1, "Minor version should match"); assert_eq!( diff --git a/pgx-pg-sys/build.rs b/pgx-pg-sys/build.rs index 95059efc4..dca68af98 100644 --- a/pgx-pg-sys/build.rs +++ b/pgx-pg-sys/build.rs @@ -164,8 +164,18 @@ fn main() -> eyre::Result<()> { .join(", ") ) })?; - let specific = pgx.get(&found_feat)?; - vec![(found_ver, specific)] + + if let Ok(pg_config) = PgConfig::from_env() { + let major_version = pg_config.major_version()?; + + if major_version != found_ver { + panic!("Feature flag `pg{found_ver}` does not match version from the environment-described PgConfig (`{major_version}`)") + } + vec![(major_version, pg_config)] + } else { + let specific = pgx.get(&found_feat)?; + vec![(found_ver, specific)] + } }; std::thread::scope(|scope| { // This is pretty much either always 1 (normally) or 5 (for releases),