Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: housekeeping for 1.70.0 #12217

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions Cargo.lock

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

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -136,10 +135,8 @@ 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
lazycell.workspace = true
libc.workspace = true
libgit2-sys.workspace = true
Expand Down
21 changes: 11 additions & 10 deletions crates/cargo-test-support/src/paths.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
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};
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<Option<PathBuf>> = Mutex::new(None);

static ref TEST_ROOTS: Mutex<HashMap<String, PathBuf>> = Default::default();
weihanglo marked this conversation as resolved.
Show resolved Hide resolved
}
static GLOBAL_ROOT: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();

/// This is used when running cargo is pre-CARGO_TARGET_TMPDIR
/// TODO: Remove when CARGO_TARGET_TMPDIR grows old enough.
Expand All @@ -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),
Expand All @@ -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"),
Expand Down
15 changes: 8 additions & 7 deletions crates/cargo-test-support/src/tools.rs
Original file line number Diff line number Diff line change
@@ -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<Option<PathBuf>> = Mutex::new(None);
static ref ECHO: Mutex<Option<PathBuf>> = Mutex::new(None);
}
static ECHO_WRAPPER: OnceLock<Mutex<Option<PathBuf>>> = OnceLock::new();
static ECHO: OnceLock<Mutex<Option<PathBuf>>> = 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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
1 change: 0 additions & 1 deletion crates/resolver-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 3 additions & 1 deletion crates/resolver-tests/tests/resolve.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::io::IsTerminal;

use cargo::core::dependency::DepKind;
use cargo::core::Dependency;
use cargo::util::Config;
Expand All @@ -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 {
Expand Down
29 changes: 16 additions & 13 deletions src/bin/cargo/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;

Expand Down Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HashSet<&'static PackageIdInner>> =
Mutex::new(HashSet::new());
}
static PACKAGE_ID_CACHE: OnceLock<Mutex<HashSet<&'static PackageIdInner>>> = OnceLock::new();

/// Identifier for a specific version of a package in a specific source.
#[derive(Clone, Copy, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/core/shell.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
10 changes: 6 additions & 4 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashSet<&'static SourceIdInner>> = Default::default();
}
static SOURCE_ID_CACHE: OnceLock<Mutex<HashSet<&'static SourceIdInner>>> = OnceLock::new();

/// Unique identifier for a source of packages.
///
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/cargo/ops/cargo_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions src/cargo/util/interning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HashSet<&'static str>> = Mutex::new(HashSet::new());
}
static STRING_CACHE: OnceLock<Mutex<HashSet<&'static str>>> = OnceLock::new();

#[derive(Clone, Copy)]
pub struct InternedString {
Expand Down Expand Up @@ -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);
Expand Down