From 8cb220f5eb001900913c4c096e59c19c86f2abf7 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 1 Jun 2023 20:45:50 +0100 Subject: [PATCH 1/4] refactor: use `IsTerminal` from std instead of `is-terminal` crate --- Cargo.lock | 2 -- Cargo.toml | 2 -- crates/resolver-tests/Cargo.toml | 1 - crates/resolver-tests/tests/resolve.rs | 4 +++- src/cargo/core/shell.rs | 2 +- 5 files changed, 4 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6acc7c682..4e8ddfbb7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -274,7 +274,6 @@ dependencies = [ "ignore", "im-rc", "indexmap", - "is-terminal", "itertools", "jobserver", "lazy_static", @@ -2658,7 +2657,6 @@ version = "0.0.0" dependencies = [ "cargo", "cargo-util", - "is-terminal", "lazy_static", "proptest", "varisat", diff --git a/Cargo.toml b/Cargo.toml index 93cb041b1..01262c1db 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,7 +45,6 @@ humantime = "2.0.0" ignore = "0.4.7" im-rc = "15.0.0" indexmap = "1" -is-terminal = "0.4.4" itertools = "0.10.0" jobserver = "0.1.26" lazy_static = "1.3.0" @@ -136,7 +135,6 @@ humantime.workspace = true ignore.workspace = true im-rc.workspace = true indexmap.workspace = true -is-terminal.workspace = true itertools.workspace = true jobserver.workspace = true lazy_static.workspace = true diff --git a/crates/resolver-tests/Cargo.toml b/crates/resolver-tests/Cargo.toml index 8a7cab113..e0efb9b6d 100644 --- a/crates/resolver-tests/Cargo.toml +++ b/crates/resolver-tests/Cargo.toml @@ -7,7 +7,6 @@ publish = false [dependencies] cargo.workspace = true cargo-util.workspace = true -is-terminal.workspace = true lazy_static.workspace = true proptest.workspace = true varisat.workspace = true diff --git a/crates/resolver-tests/tests/resolve.rs b/crates/resolver-tests/tests/resolve.rs index df74826f0..02486bfb5 100644 --- a/crates/resolver-tests/tests/resolve.rs +++ b/crates/resolver-tests/tests/resolve.rs @@ -1,3 +1,5 @@ +use std::io::IsTerminal; + use cargo::core::dependency::DepKind; use cargo::core::Dependency; use cargo::util::Config; @@ -21,7 +23,7 @@ use proptest::prelude::*; proptest! { #![proptest_config(ProptestConfig { max_shrink_iters: - if is_ci() || !is_terminal::IsTerminal::is_terminal(&std::io::stderr()){ + if is_ci() || !std::io::stderr().is_terminal() { // This attempts to make sure that CI will fail fast, 0 } else { diff --git a/src/cargo/core/shell.rs b/src/cargo/core/shell.rs index f74bde257..4d45e6098 100644 --- a/src/cargo/core/shell.rs +++ b/src/cargo/core/shell.rs @@ -1,7 +1,7 @@ use std::fmt; use std::io::prelude::*; +use std::io::IsTerminal; -use is_terminal::IsTerminal; use termcolor::Color::{Cyan, Green, Red, Yellow}; use termcolor::{self, Color, ColorSpec, StandardStream, WriteColor}; From 1f581e79cf31be0d027b3fe0200e8a611c723b71 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 1 Jun 2023 20:47:33 +0100 Subject: [PATCH 2/4] refactor: rustdoc `--test-run-directory` is stabilized --- src/cargo/ops/cargo_test.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index b7e61982d..4a86349ae 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -217,8 +217,6 @@ fn run_doc_tests( if doctest_in_workspace { add_path_args(ws, unit, &mut p); - // FIXME(swatinem): remove the `unstable-options` once rustdoc stabilizes the `test-run-directory` option - p.arg("-Z").arg("unstable-options"); p.arg("--test-run-directory") .arg(unit.pkg.root().to_path_buf()); } else { From fa7cc198aa320d63c58053c92775425c6f7b32e5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 1 Jun 2023 21:49:26 +0100 Subject: [PATCH 3/4] refactor: replace some usages of `lazy_static` with `OnceLock` * Some usages still wait for `LazyCell`. * `TEST_ROOTS` is no longer used. Removed. --- Cargo.lock | 1 - Cargo.toml | 1 - crates/cargo-test-support/src/paths.rs | 21 ++++++++++--------- crates/cargo-test-support/src/tools.rs | 15 ++++++------- src/bin/cargo/cli.rs | 29 ++++++++++++++------------ src/cargo/core/package_id.rs | 11 +++++----- src/cargo/core/source/source_id.rs | 10 +++++---- src/cargo/util/interning.rs | 10 +++++---- 8 files changed, 53 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4e8ddfbb7..d82928705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -276,7 +276,6 @@ dependencies = [ "indexmap", "itertools", "jobserver", - "lazy_static", "lazycell", "libc", "libgit2-sys", diff --git a/Cargo.toml b/Cargo.toml index 01262c1db..c95ff612c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -137,7 +137,6 @@ im-rc.workspace = true indexmap.workspace = true itertools.workspace = true jobserver.workspace = true -lazy_static.workspace = true lazycell.workspace = true libc.workspace = true libgit2-sys.workspace = true diff --git a/crates/cargo-test-support/src/paths.rs b/crates/cargo-test-support/src/paths.rs index ef1fddb70..50040e1d4 100644 --- a/crates/cargo-test-support/src/paths.rs +++ b/crates/cargo-test-support/src/paths.rs @@ -1,7 +1,6 @@ use filetime::{self, FileTime}; -use lazy_static::lazy_static; + use std::cell::RefCell; -use std::collections::HashMap; use std::env; use std::fs; use std::io::{self, ErrorKind}; @@ -9,15 +8,11 @@ use std::path::{Path, PathBuf}; use std::process::Command; use std::sync::atomic::{AtomicUsize, Ordering}; use std::sync::Mutex; +use std::sync::OnceLock; static CARGO_INTEGRATION_TEST_DIR: &str = "cit"; -lazy_static! { - // TODO: Use `SyncOnceCell` when stable - static ref GLOBAL_ROOT: Mutex> = Mutex::new(None); - - static ref TEST_ROOTS: Mutex> = Default::default(); -} +static GLOBAL_ROOT: OnceLock>> = OnceLock::new(); /// This is used when running cargo is pre-CARGO_TARGET_TMPDIR /// TODO: Remove when CARGO_TARGET_TMPDIR grows old enough. @@ -31,7 +26,10 @@ fn global_root_legacy() -> PathBuf { } fn set_global_root(tmp_dir: Option<&'static str>) { - let mut lock = GLOBAL_ROOT.lock().unwrap(); + let mut lock = GLOBAL_ROOT + .get_or_init(|| Default::default()) + .lock() + .unwrap(); if lock.is_none() { let mut root = match tmp_dir { Some(tmp_dir) => PathBuf::from(tmp_dir), @@ -44,7 +42,10 @@ fn set_global_root(tmp_dir: Option<&'static str>) { } pub fn global_root() -> PathBuf { - let lock = GLOBAL_ROOT.lock().unwrap(); + let lock = GLOBAL_ROOT + .get_or_init(|| Default::default()) + .lock() + .unwrap(); match lock.as_ref() { Some(p) => p.clone(), None => unreachable!("GLOBAL_ROOT not set yet"), diff --git a/crates/cargo-test-support/src/tools.rs b/crates/cargo-test-support/src/tools.rs index 7c056b6fa..2ce2849ae 100644 --- a/crates/cargo-test-support/src/tools.rs +++ b/crates/cargo-test-support/src/tools.rs @@ -1,20 +1,21 @@ //! Common executables that can be reused by various tests. use crate::{basic_manifest, paths, project, Project}; -use lazy_static::lazy_static; use std::path::{Path, PathBuf}; use std::sync::Mutex; +use std::sync::OnceLock; -lazy_static! { - static ref ECHO_WRAPPER: Mutex> = Mutex::new(None); - static ref ECHO: Mutex> = Mutex::new(None); -} +static ECHO_WRAPPER: OnceLock>> = OnceLock::new(); +static ECHO: OnceLock>> = OnceLock::new(); /// Returns the path to an executable that works as a wrapper around rustc. /// /// The wrapper will echo the command line it was called with to stderr. pub fn echo_wrapper() -> PathBuf { - let mut lock = ECHO_WRAPPER.lock().unwrap(); + let mut lock = ECHO_WRAPPER + .get_or_init(|| Default::default()) + .lock() + .unwrap(); if let Some(path) = &*lock { return path.clone(); } @@ -53,7 +54,7 @@ pub fn echo_wrapper() -> PathBuf { /// /// Do not expect this to be anything fancy. pub fn echo() -> PathBuf { - let mut lock = ECHO.lock().unwrap(); + let mut lock = ECHO.get_or_init(|| Default::default()).lock().unwrap(); if let Some(path) = &*lock { return path.clone(); } diff --git a/src/bin/cargo/cli.rs b/src/bin/cargo/cli.rs index 946816571..f46a499e4 100644 --- a/src/bin/cargo/cli.rs +++ b/src/bin/cargo/cli.rs @@ -14,16 +14,6 @@ use super::list_commands; use crate::command_prelude::*; use cargo::core::features::HIDDEN; -lazy_static::lazy_static! { - // Maps from commonly known external commands (not builtin to cargo) to their - // description, for the help page. Reserved for external subcommands that are - // core within the rust ecosystem (esp ones that might become internal in the future). - static ref KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS: HashMap<&'static str, &'static str> = HashMap::from([ - ("clippy", "Checks a package to catch common mistakes and improve your Rust code."), - ("fmt", "Formats all bin and lib files of the current crate using rustfmt."), - ]); -} - pub fn main(config: &mut LazyConfig) -> CliResult { let args = cli().try_get_matches()?; @@ -128,15 +118,28 @@ Run with 'cargo -Z [FLAG] [COMMAND]'", } if expanded_args.flag("list") { + // Maps from commonly known external commands (not builtin to cargo) + // to their description, for the help page. Reserved for external + // subcommands that are core within the rust ecosystem (esp ones that + // might become internal in the future). + let known_external_command_descriptions = HashMap::from([ + ( + "clippy", + "Checks a package to catch common mistakes and improve your Rust code.", + ), + ( + "fmt", + "Formats all bin and lib files of the current crate using rustfmt.", + ), + ]); drop_println!(config, "Installed Commands:"); for (name, command) in list_commands(config) { - let known_external_desc = KNOWN_EXTERNAL_COMMAND_DESCRIPTIONS.get(name.as_str()); + let known_external_desc = known_external_command_descriptions.get(name.as_str()); match command { CommandInfo::BuiltIn { about } => { assert!( known_external_desc.is_none(), - "KNOWN_EXTERNAL_COMMANDS shouldn't contain builtin \"{}\"", - name + "known_external_commands shouldn't contain builtin `{name}`", ); let summary = about.unwrap_or_default(); let summary = summary.lines().next().unwrap_or(&summary); // display only the first line diff --git a/src/cargo/core/package_id.rs b/src/cargo/core/package_id.rs index ee31e9c48..7abd545d7 100644 --- a/src/cargo/core/package_id.rs +++ b/src/cargo/core/package_id.rs @@ -5,6 +5,7 @@ use std::hash::Hash; use std::path::Path; use std::ptr; use std::sync::Mutex; +use std::sync::OnceLock; use serde::de; use serde::ser; @@ -13,10 +14,7 @@ use crate::core::source::SourceId; use crate::util::interning::InternedString; use crate::util::{CargoResult, ToSemver}; -lazy_static::lazy_static! { - static ref PACKAGE_ID_CACHE: Mutex> = - Mutex::new(HashSet::new()); -} +static PACKAGE_ID_CACHE: OnceLock>> = OnceLock::new(); /// Identifier for a specific version of a package in a specific source. #[derive(Clone, Copy, Eq, PartialOrd, Ord)] @@ -147,7 +145,10 @@ impl PackageId { version, source_id, }; - let mut cache = PACKAGE_ID_CACHE.lock().unwrap(); + let mut cache = PACKAGE_ID_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let inner = cache.get(&inner).cloned().unwrap_or_else(|| { let inner = Box::leak(Box::new(inner)); cache.insert(inner); diff --git a/src/cargo/core/source/source_id.rs b/src/cargo/core/source/source_id.rs index c369dab16..4064364d5 100644 --- a/src/cargo/core/source/source_id.rs +++ b/src/cargo/core/source/source_id.rs @@ -13,11 +13,10 @@ use std::hash::{self, Hash}; use std::path::{Path, PathBuf}; use std::ptr; use std::sync::Mutex; +use std::sync::OnceLock; use url::Url; -lazy_static::lazy_static! { - static ref SOURCE_ID_CACHE: Mutex> = Default::default(); -} +static SOURCE_ID_CACHE: OnceLock>> = OnceLock::new(); /// Unique identifier for a source of packages. /// @@ -118,7 +117,10 @@ impl SourceId { /// Interns the value and returns the wrapped type. fn wrap(inner: SourceIdInner) -> SourceId { - let mut cache = SOURCE_ID_CACHE.lock().unwrap(); + let mut cache = SOURCE_ID_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let inner = cache.get(&inner).cloned().unwrap_or_else(|| { let inner = Box::leak(Box::new(inner)); cache.insert(inner); diff --git a/src/cargo/util/interning.rs b/src/cargo/util/interning.rs index bbec12942..584fdf623 100644 --- a/src/cargo/util/interning.rs +++ b/src/cargo/util/interning.rs @@ -10,14 +10,13 @@ use std::path::Path; use std::ptr; use std::str; use std::sync::Mutex; +use std::sync::OnceLock; fn leak(s: String) -> &'static str { Box::leak(s.into_boxed_str()) } -lazy_static::lazy_static! { - static ref STRING_CACHE: Mutex> = Mutex::new(HashSet::new()); -} +static STRING_CACHE: OnceLock>> = OnceLock::new(); #[derive(Clone, Copy)] pub struct InternedString { @@ -64,7 +63,10 @@ impl Eq for InternedString {} impl InternedString { pub fn new(str: &str) -> InternedString { - let mut cache = STRING_CACHE.lock().unwrap(); + let mut cache = STRING_CACHE + .get_or_init(|| Default::default()) + .lock() + .unwrap(); let s = cache.get(str).cloned().unwrap_or_else(|| { let s = leak(str.to_string()); cache.insert(s); From 423eb4e6f19cd44ff135a78b7af583f9894709e5 Mon Sep 17 00:00:00 2001 From: Weihang Lo Date: Thu, 1 Jun 2023 21:55:49 +0100 Subject: [PATCH 4/4] chore: sparse protocol is now on by default! --- .github/workflows/main.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index be7305570..282c50462 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,9 +12,6 @@ defaults: permissions: contents: read -env: - CARGO_REGISTRIES_CRATES_IO_PROTOCOL: sparse - jobs: # Check Code style quickly by running `rustfmt` over all code rustfmt: