Skip to content

Commit

Permalink
cargo-pgrx: reduce trivial dep usages (#1499)
Browse files Browse the repository at this point in the history
I noticed #1468 would allow us to also remove a dependency or two from
cargo-pgrx, so I started work by removing once_cell from it and a few
other spots in our tree, although we use it so pervasively it's not
realistic to remove it entirely. It was actually secretly in our public
API, behind a `#[doc(hidden)]`, for some reason.

I then noticed a bunch of other easy removals from cargo-pgrx:
- serde_derive should not be used directly
- flate2, prettyplease, syn, and unescape are not directly used anymore
- nix crate maintenance has gotten better, but it is a big dep for 1 bool
  • Loading branch information
workingjubilee authored Feb 7, 2024
1 parent 03e97ee commit e2c4ff9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 85 deletions.
31 changes: 1 addition & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions cargo-pgrx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,38 @@ pgrx-pg-config.workspace = true
pgrx-sql-entity-graph.workspace = true

cargo_toml.workspace = true
eyre.workspace = true
owo-colors.workspace = true
libc.workspace = true
regex.workspace = true
unescape.workspace = true
url.workspace = true

cargo_metadata = "0.17.0"
clap = { version = "4.4.2", features = [ "env", "suggestions", "cargo", "derive", "wrap_help" ] }
clap-cargo = { version = "0.11.0", features = [ "cargo_metadata" ] }
semver = "1.0.20"
env_proxy = "0.4.1"
prettyplease = "0.2.15"
jobslot = "0.2.12" # as seen in gmake -j{N}
semver = "1.0.20" # checking pgrx versions match
tempfile = "3.8.0"
toml = "0.8.2" # our config files

# SQL schema generation
object = "0.32.1"
proc-macro2 = { version = "1.0.69", features = [ "span-locations" ] }
quote = "1.0.33"
ureq = { version = "2.8.0", default-features = false, features = [ "gzip" ] }
serde = { version = "1.0", features = [ "derive" ] }
serde_derive = "1.0"
serde-xml-rs = "0.6.0"
syn = { version = "2.0.18", features = [ "extra-traits", "full", "fold", "parsing" ] }
object = "0.32.1"
once_cell = "1.18.0"

# emit better diagnostics
color-eyre = "0.6.2"
eyre.workspace = true
owo-colors.workspace = true
tracing = "0.1"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.17", features = [ "env-filter" ] }
flate2 = { version = "1.0.27", default-features = false, features = ["rust_backend"] }
tempfile = "3.8.0"
nix = { version = "0.27", default-features = false, features = ["user"] }
toml = "0.8.2"
jobslot = "0.2.12"

# for downloading/unpacking things from the Postgres file servers
bzip2 = "0.4.4"
env_proxy = "0.4.1"
serde = { version = "1.0", features = [ "derive" ] }
serde-xml-rs = "0.6.0"
tar = "0.4.40"
ureq = { version = "2.8.0", default-features = false, features = [ "gzip" ] }
url.workspace = true

[features]
default = ["rustls"]
Expand Down
5 changes: 3 additions & 2 deletions cargo-pgrx/src/command/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ fn get_pg_installdir(pgdir: &PathBuf) -> PathBuf {

#[cfg(unix)]
fn is_root_user() -> bool {
use nix::unistd::Uid;
Uid::effective().is_root()
// SAFETY: No, the `nix` crate does not do anything more clever:
// check if effective user ID is 0, AKA "root"
unsafe { libc::geteuid() == 0 }
}

/// Incorrectly answers false, reverting pgrx to pre-root-aware behavior,
Expand Down
21 changes: 13 additions & 8 deletions cargo-pgrx/src/command/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ use crate::profile::CargoProfile;
use crate::CommandExecute;
use cargo_toml::Manifest;
use eyre::{eyre, WrapErr};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use pgrx_pg_config::{cargo::PgrxManifestExt, get_target_dir, PgConfig, Pgrx};
use std::collections::HashMap;
use std::fs;
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, OnceLock};

/// Type used for memoizing expensive to calculate values.
/// `Arc<Mutex>` is needed to get around compiler safety checks.
Expand Down Expand Up @@ -453,12 +452,14 @@ pub(crate) fn find_library_file(
Ok(library_file_path)
}

static CARGO_VERSION: Lazy<MemoizeKeyValue> = Lazy::new(|| Arc::new(Mutex::new(HashMap::new())));
static CARGO_VERSION: OnceLock<MemoizeKeyValue> = OnceLock::new();

pub(crate) fn get_version(manifest_path: impl AsRef<Path>) -> eyre::Result<String> {
let path_string = manifest_path.as_ref().to_owned();

if let Some(version) = CARGO_VERSION.lock().unwrap().get(&path_string) {
if let Some(version) =
CARGO_VERSION.get_or_init(Default::default).lock().unwrap().get(&path_string)
{
return Ok(version.clone());
}

Expand All @@ -481,16 +482,20 @@ pub(crate) fn get_version(manifest_path: impl AsRef<Path>) -> eyre::Result<Strin
None => return Err(eyre!("cannot determine extension version number. Is the `default_version` property declared in the control file?")),
};

CARGO_VERSION.lock().unwrap().insert(path_string, version.clone());
CARGO_VERSION
.get_or_init(Default::default)
.lock()
.unwrap()
.insert(path_string, version.clone());
Ok(version)
}

static GIT_HASH: Lazy<MemoizeKeyValue> = Lazy::new(|| Arc::new(Mutex::new(HashMap::new())));
static GIT_HASH: OnceLock<MemoizeKeyValue> = OnceLock::new();

fn get_git_hash(manifest_path: impl AsRef<Path>) -> eyre::Result<String> {
let path_string = manifest_path.as_ref().to_owned();

if let Some(hash) = GIT_HASH.lock().unwrap().get(&path_string) {
if let Some(hash) = GIT_HASH.get_or_init(Default::default).lock().unwrap().get(&path_string) {
Ok(hash.clone())
} else {
let hash = match get_property(manifest_path, "git_hash")? {
Expand All @@ -500,7 +505,7 @@ fn get_git_hash(manifest_path: impl AsRef<Path>) -> eyre::Result<String> {
)),
};

GIT_HASH.lock().unwrap().insert(path_string, hash.clone());
GIT_HASH.get_or_init(Default::default).lock().unwrap().insert(path_string, hash.clone());

Ok(hash)
}
Expand Down
2 changes: 1 addition & 1 deletion cargo-pgrx/src/command/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ mod rss {
use eyre::WrapErr;
use owo_colors::OwoColorize;
use pgrx_pg_config::{PgMinorVersion, PgVersion};
use serde_derive::Deserialize;
use serde::Deserialize;
use std::collections::BTreeMap;
use url::Url;

Expand Down
1 change: 0 additions & 1 deletion pgrx-pg-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,3 @@ proc-macro2 = "1.0.69"
quote = "1.0.33"
syn = { version = "1.0.109", features = [ "extra-traits", "full", "fold", "parsing" ] }
shlex = "1.3" # shell lexing, also used by many of our deps
once_cell = "1.18.0"
9 changes: 5 additions & 4 deletions pgrx-pg-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
use bindgen::callbacks::{DeriveTrait, ImplementsTrait, MacroParsingBehavior};
use eyre::{eyre, WrapErr};
use once_cell::sync::Lazy;
use pgrx_pg_config::{
is_supported_major_version, prefix_path, PgConfig, PgConfigSelector, Pgrx, SUPPORTED_VERSIONS,
};
Expand All @@ -19,6 +18,7 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fs;
use std::path::{self, PathBuf}; // disambiguate path::Path and syn::Type::Path
use std::process::{Command, Output};
use std::sync::OnceLock;
use syn::{ForeignItem, Item, ItemConst};

const BLOCKLISTED_TYPES: [&str; 3] = ["Datum", "NullableDatum", "Oid"];
Expand Down Expand Up @@ -1026,8 +1026,7 @@ fn run_command(mut command: &mut Command, version: &str) -> eyre::Result<Output>

// Plausibly it would be better to generate a regex to pass to bindgen for this,
// but this is less error-prone for now.
static BLOCKLISTED: Lazy<BTreeSet<&'static str>> =
Lazy::new(|| build::sym_blocklist::SYMBOLS.iter().copied().collect::<BTreeSet<&str>>());
static BLOCKLISTED: OnceLock<BTreeSet<&'static str>> = OnceLock::new();
fn is_blocklisted_item(item: &ForeignItem) -> bool {
let sym_name = match item {
ForeignItem::Fn(f) => &f.sig.ident,
Expand All @@ -1036,7 +1035,9 @@ fn is_blocklisted_item(item: &ForeignItem) -> bool {
ForeignItem::Static(s) => &s.ident,
_ => return false,
};
BLOCKLISTED.contains(sym_name.to_string().as_str())
BLOCKLISTED
.get_or_init(|| build::sym_blocklist::SYMBOLS.iter().copied().collect::<BTreeSet<&str>>())
.contains(sym_name.to_string().as_str())
}

fn apply_pg_guard(items: &Vec<syn::Item>) -> eyre::Result<proc_macro2::TokenStream> {
Expand Down
2 changes: 1 addition & 1 deletion pgrx-sql-entity-graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ no-schema-generation = []

[dependencies]
eyre.workspace = true
unescape.workspace = true
thiserror.workspace = true

convert_case = "0.6.0"
petgraph = "0.6.4"
proc-macro2 = { version = "1.0.69", features = [ "span-locations" ] }
quote = "1.0.33"
syn = { version = "1.0.109", features = [ "extra-traits", "full", "fold", "parsing" ] }
unescape = "0.1.0" # for escaped-character-handling

# colorized sql output
owo-colors = { optional = true, workspace = true }
Expand Down
1 change: 0 additions & 1 deletion pgrx-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ thiserror.workspace = true
owo-colors.workspace = true

clap-cargo = "0.11.0"
once_cell = "1.18.0"
postgres = "0.19.7"
proptest = { version = "1", optional = true }
serde = "1.0"
Expand Down
32 changes: 17 additions & 15 deletions pgrx-tests/src/framework.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use std::collections::HashSet;
use std::process::{Command, Stdio};

use eyre::{eyre, WrapErr};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use pgrx::prelude::*;
use pgrx_pg_config::{
Expand All @@ -21,7 +20,7 @@ use postgres::error::DbError;
use std::collections::HashMap;
use std::io::{BufRead, BufReader, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, OnceLock};
use std::time::Duration;
use sysinfo::{Pid, ProcessExt, System, SystemExt};

Expand All @@ -36,13 +35,7 @@ struct SetupState {
system_session_id: String,
}

static TEST_MUTEX: Lazy<Mutex<SetupState>> = Lazy::new(|| {
Mutex::new(SetupState {
installed: false,
loglines: Arc::new(Mutex::new(HashMap::new())),
system_session_id: "NONE".to_string(),
})
});
static TEST_MUTEX: OnceLock<Mutex<SetupState>> = OnceLock::new();

// The goal of this closure is to allow "wrapping" of anything that might issue
// an SQL simple_query or query using either a postgres::Client or
Expand Down Expand Up @@ -198,14 +191,23 @@ fn format_loglines(session_id: &str, loglines: &LogLines) -> String {
fn initialize_test_framework(
postgresql_conf: Vec<&'static str>,
) -> eyre::Result<(LogLines, String)> {
let mut state = TEST_MUTEX.lock().unwrap_or_else(|_| {
// This used to immediately throw an std::process::exit(1), but it
// would consume both stdout and stderr, resulting in error messages
// not being displayed unless you were running tests with --nocapture.
panic!(
let mut state = TEST_MUTEX
.get_or_init(|| {
Mutex::new(SetupState {
installed: false,
loglines: Arc::new(Mutex::new(HashMap::new())),
system_session_id: "NONE".to_string(),
})
})
.lock()
.unwrap_or_else(|_| {
// This used to immediately throw an std::process::exit(1), but it
// would consume both stdout and stderr, resulting in error messages
// not being displayed unless you were running tests with --nocapture.
panic!(
"Could not obtain test mutex. A previous test may have hard-aborted while holding it."
);
});
});

if !state.installed {
shutdown::register_shutdown_hook();
Expand Down
3 changes: 0 additions & 3 deletions pgrx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ pub mod varlena;
pub mod wrappers;
pub mod xid;

#[doc(hidden)]
pub use once_cell;

/// Not ready for public exposure.
mod layout;
mod ptr;
Expand Down

0 comments on commit e2c4ff9

Please sign in to comment.