diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index a9a8ea6b494..55b5f116b65 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -156,6 +156,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { ops::install_list(root, config)?; } else { ops::install( + config, root, krates, source, diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index e698f3a3a25..82d191fb263 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -46,8 +46,7 @@ use crate::util::{closest_msg, profile, CargoResult}; /// Contains information about how a package should be compiled. #[derive(Debug)] -pub struct CompileOptions<'a> { - pub config: &'a Config, +pub struct CompileOptions { /// Configuration information for a rustc build pub build_config: BuildConfig, /// Extra features to build for the root package @@ -79,10 +78,9 @@ pub struct CompileOptions<'a> { pub export_dir: Option, } -impl<'a> CompileOptions<'a> { - pub fn new(config: &'a Config, mode: CompileMode) -> CargoResult> { +impl<'a> CompileOptions { + pub fn new(config: &Config, mode: CompileMode) -> CargoResult { Ok(CompileOptions { - config, build_config: BuildConfig::new(config, None, &None, mode)?, features: Vec::new(), all_features: false, @@ -242,10 +240,7 @@ pub enum CompileFilter { }, } -pub fn compile<'a>( - ws: &Workspace<'a>, - options: &CompileOptions<'a>, -) -> CargoResult> { +pub fn compile<'a>(ws: &Workspace<'a>, options: &CompileOptions) -> CargoResult> { let exec: Arc = Arc::new(DefaultExecutor); compile_with_exec(ws, options, &exec) } @@ -254,7 +249,7 @@ pub fn compile<'a>( /// calls and add custom logic. `compile` uses `DefaultExecutor` which just passes calls through. pub fn compile_with_exec<'a>( ws: &Workspace<'a>, - options: &CompileOptions<'a>, + options: &CompileOptions, exec: &Arc, ) -> CargoResult> { ws.emit_warnings()?; @@ -263,11 +258,10 @@ pub fn compile_with_exec<'a>( pub fn compile_ws<'a>( ws: &Workspace<'a>, - options: &CompileOptions<'a>, + options: &CompileOptions, exec: &Arc, ) -> CargoResult> { let CompileOptions { - config, ref build_config, ref spec, ref features, @@ -280,6 +274,7 @@ pub fn compile_ws<'a>( rustdoc_document_private_items, ref export_dir, } = *options; + let config = ws.config(); match build_config.mode { CompileMode::Test diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 466c1333258..07b3eabdf6d 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -9,15 +9,15 @@ use std::process::Command; /// Strongly typed options for the `cargo doc` command. #[derive(Debug)] -pub struct DocOptions<'a> { +pub struct DocOptions { /// Whether to attempt to open the browser after compiling the docs pub open_result: bool, /// Options to pass through to the compiler - pub compile_opts: ops::CompileOptions<'a>, + pub compile_opts: ops::CompileOptions, } /// Main method for `cargo doc`. -pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> { +pub fn doc(ws: &Workspace<'_>, options: &DocOptions) -> CargoResult<()> { let specs = options.compile_opts.spec.to_package_id_specs(ws)?; let opts = ResolveOpts::new( /*dev_deps*/ true, @@ -85,7 +85,7 @@ pub fn doc(ws: &Workspace<'_>, options: &DocOptions<'_>) -> CargoResult<()> { .join(&name) .join("index.html"); if path.exists() { - let mut shell = options.compile_opts.config.shell(); + let mut shell = ws.config().shell(); shell.status("Opening", path.display())?; open_docs(&path, &mut shell)?; } diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index 0ff2edbc6b2..ba8ab12faa0 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -35,20 +35,22 @@ impl Drop for Transaction { } pub fn install( + config: &Config, root: Option<&str>, krates: Vec<&str>, source_id: SourceId, from_cwd: bool, vers: Option<&str>, - opts: &ops::CompileOptions<'_>, + opts: &ops::CompileOptions, force: bool, no_track: bool, ) -> CargoResult<()> { - let root = resolve_root(root, opts.config)?; - let map = SourceConfigMap::new(opts.config)?; + let root = resolve_root(root, config)?; + let map = SourceConfigMap::new(config)?; let (installed_anything, scheduled_error) = if krates.len() <= 1 { install_one( + config, &root, &map, krates.into_iter().next(), @@ -69,6 +71,7 @@ pub fn install( let root = root.clone(); let map = map.clone(); match install_one( + config, &root, &map, Some(krate), @@ -82,7 +85,7 @@ pub fn install( ) { Ok(()) => succeeded.push(krate), Err(e) => { - crate::display_error(&e, &mut opts.config.shell()); + crate::display_error(&e, &mut config.shell()); failed.push(krate) } } @@ -100,7 +103,7 @@ pub fn install( )); } if !succeeded.is_empty() || !failed.is_empty() { - opts.config.shell().status("Summary", summary.join(" "))?; + config.shell().status("Summary", summary.join(" "))?; } (!succeeded.is_empty(), !failed.is_empty()) @@ -117,7 +120,7 @@ pub fn install( } } - opts.config.shell().warn(&format!( + config.shell().warn(&format!( "be sure to add `{}` to your PATH to be \ able to run the installed binaries", dst.display() @@ -132,19 +135,18 @@ pub fn install( } fn install_one( + config: &Config, root: &Filesystem, map: &SourceConfigMap<'_>, krate: Option<&str>, source_id: SourceId, from_cwd: bool, vers: Option<&str>, - opts: &ops::CompileOptions<'_>, + opts: &ops::CompileOptions, force: bool, no_track: bool, is_first_install: bool, ) -> CargoResult<()> { - let config = opts.config; - let pkg = if source_id.is_git() { select_pkg( GitSource::new(source_id, config)?, diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index c4c2a0f9c66..cda35913aa9 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -704,7 +704,6 @@ fn run_verify(ws: &Workspace<'_>, tar: &FileLock, opts: &PackageOpts<'_>) -> Car ops::compile_with_exec( &ws, &ops::CompileOptions { - config, build_config: BuildConfig::new(config, opts.jobs, &opts.target, CompileMode::Build)?, features: opts.features.clone(), no_default_features: opts.no_default_features, diff --git a/src/cargo/ops/cargo_run.rs b/src/cargo/ops/cargo_run.rs index 88361bc36fb..0c207482117 100644 --- a/src/cargo/ops/cargo_run.rs +++ b/src/cargo/ops/cargo_run.rs @@ -8,7 +8,7 @@ use crate::util::{CargoResult, ProcessError}; pub fn run( ws: &Workspace<'_>, - options: &ops::CompileOptions<'_>, + options: &ops::CompileOptions, args: &[OsString], ) -> CargoResult> { let config = ws.config(); diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index 4eb4b715faa..e142d831fe4 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -5,17 +5,17 @@ use crate::core::shell::Verbosity; use crate::core::Workspace; use crate::ops; use crate::util::errors::CargoResult; -use crate::util::{CargoTestError, ProcessError, Test}; +use crate::util::{CargoTestError, Config, ProcessError, Test}; -pub struct TestOptions<'a> { - pub compile_opts: ops::CompileOptions<'a>, +pub struct TestOptions { + pub compile_opts: ops::CompileOptions, pub no_run: bool, pub no_fail_fast: bool, } pub fn run_tests( ws: &Workspace<'_>, - options: &TestOptions<'_>, + options: &TestOptions, test_args: &[&str], ) -> CargoResult> { let compilation = compile_tests(ws, options)?; @@ -23,14 +23,14 @@ pub fn run_tests( if options.no_run { return Ok(None); } - let (test, mut errors) = run_unit_tests(options, test_args, &compilation)?; + let (test, mut errors) = run_unit_tests(ws.config(), options, test_args, &compilation)?; // If we have an error and want to fail fast, then return. if !errors.is_empty() && !options.no_fail_fast { return Ok(Some(CargoTestError::new(test, errors))); } - let (doctest, docerrors) = run_doc_tests(options, test_args, &compilation)?; + let (doctest, docerrors) = run_doc_tests(ws.config(), options, test_args, &compilation)?; let test = if docerrors.is_empty() { test } else { doctest }; errors.extend(docerrors); if errors.is_empty() { @@ -42,7 +42,7 @@ pub fn run_tests( pub fn run_benches( ws: &Workspace<'_>, - options: &TestOptions<'_>, + options: &TestOptions, args: &[&str], ) -> CargoResult> { let compilation = compile_tests(ws, options)?; @@ -54,7 +54,7 @@ pub fn run_benches( let mut args = args.to_vec(); args.push("--bench"); - let (test, errors) = run_unit_tests(options, &args, &compilation)?; + let (test, errors) = run_unit_tests(ws.config(), options, &args, &compilation)?; match errors.len() { 0 => Ok(None), @@ -62,10 +62,7 @@ pub fn run_benches( } } -fn compile_tests<'a>( - ws: &Workspace<'a>, - options: &TestOptions<'a>, -) -> CargoResult> { +fn compile_tests<'a>(ws: &Workspace<'a>, options: &TestOptions) -> CargoResult> { let mut compilation = ops::compile(ws, &options.compile_opts)?; compilation .tests @@ -75,12 +72,12 @@ fn compile_tests<'a>( /// Runs the unit and integration tests of a package. fn run_unit_tests( - options: &TestOptions<'_>, + config: &Config, + options: &TestOptions, test_args: &[&str], compilation: &Compilation<'_>, ) -> CargoResult<(Test, Vec)> { - let config = options.compile_opts.config; - let cwd = options.compile_opts.config.cwd(); + let cwd = config.cwd(); let mut errors = Vec::new(); @@ -133,12 +130,12 @@ fn run_unit_tests( } fn run_doc_tests( - options: &TestOptions<'_>, + config: &Config, + options: &TestOptions, test_args: &[&str], compilation: &Compilation<'_>, ) -> CargoResult<(Test, Vec)> { let mut errors = Vec::new(); - let config = options.compile_opts.config; // The unstable doctest-xcompile feature enables both per-target-ignores and // cross-compiling doctests. As a side effect, this feature also gates running diff --git a/src/cargo/ops/common_for_install_and_uninstall.rs b/src/cargo/ops/common_for_install_and_uninstall.rs index aa5c4ee18a3..221e1afc38e 100644 --- a/src/cargo/ops/common_for_install_and_uninstall.rs +++ b/src/cargo/ops/common_for_install_and_uninstall.rs @@ -159,7 +159,7 @@ impl InstallTracker { dst: &Path, pkg: &Package, force: bool, - opts: &CompileOptions<'_>, + opts: &CompileOptions, target: &str, _rustc: &str, ) -> CargoResult<(Freshness, BTreeMap>)> { @@ -267,7 +267,7 @@ impl InstallTracker { package: &Package, bins: &BTreeSet, version_req: Option, - opts: &CompileOptions<'_>, + opts: &CompileOptions, target: &str, rustc: &str, ) { @@ -401,7 +401,7 @@ impl CrateListingV2 { pkg: &Package, bins: &BTreeSet, version_req: Option, - opts: &CompileOptions<'_>, + opts: &CompileOptions, target: &str, rustc: &str, ) { @@ -490,12 +490,7 @@ impl InstallInfo { /// Determine if this installation is "up to date", or if it needs to be reinstalled. /// /// This does not do Package/Source/Version checking. - fn is_up_to_date( - &self, - opts: &CompileOptions<'_>, - target: &str, - exes: &BTreeSet, - ) -> bool { + fn is_up_to_date(&self, opts: &CompileOptions, target: &str, exes: &BTreeSet) -> bool { self.features == feature_set(&opts.features) && self.all_features == opts.all_features && self.no_default_features == opts.no_default_features diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 4900df09543..1983e9387a8 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -53,10 +53,9 @@ use rustfix::{self, CodeFix}; use crate::core::Workspace; use crate::ops::{self, CompileOptions}; -use crate::util; use crate::util::diagnostic_server::{Message, RustfixDiagnosticServer}; use crate::util::errors::CargoResult; -use crate::util::ProcessBuilder; +use crate::util::{self, Config, ProcessBuilder}; use crate::util::{existing_vcs_repo, LockServer, LockServerClient}; const FIX_ENV: &str = "__CARGO_FIX_PLZ"; @@ -69,7 +68,7 @@ pub struct FixOptions<'a> { pub edition: bool, pub prepare_for: Option<&'a str>, pub idioms: bool, - pub compile_opts: CompileOptions<'a>, + pub compile_opts: CompileOptions, pub allow_dirty: bool, pub allow_no_vcs: bool, pub allow_staged: bool, @@ -77,7 +76,7 @@ pub struct FixOptions<'a> { } pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> { - check_version_control(opts)?; + check_version_control(ws.config(), opts)?; // Spin up our lock server, which our subprocesses will use to synchronize fixes. let lock_server = LockServer::new()?; @@ -116,7 +115,7 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> { server.configure(&mut wrapper); } - let rustc = opts.compile_opts.config.load_global_rustc(Some(ws))?; + let rustc = ws.config().load_global_rustc(Some(ws))?; wrapper.arg(&rustc.path); // primary crates are compiled using a cargo subprocess to do extra work of applying fixes and @@ -127,11 +126,10 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> { Ok(()) } -fn check_version_control(opts: &FixOptions<'_>) -> CargoResult<()> { +fn check_version_control(config: &Config, opts: &FixOptions<'_>) -> CargoResult<()> { if opts.allow_no_vcs { return Ok(()); } - let config = opts.compile_opts.config; if !existing_vcs_repo(config.cwd(), config.cwd()) { anyhow::bail!( "no VCS found for this package and `cargo fix` can potentially \ diff --git a/src/cargo/util/command_prelude.rs b/src/cargo/util/command_prelude.rs index b3b3a151125..48df8b95061 100644 --- a/src/cargo/util/command_prelude.rs +++ b/src/cargo/util/command_prelude.rs @@ -362,13 +362,13 @@ pub trait ArgMatchesExt { } } - fn compile_options<'a>( + fn compile_options( &self, - config: &'a Config, + config: &Config, mode: CompileMode, - workspace: Option<&Workspace<'a>>, + workspace: Option<&Workspace<'_>>, profile_checking: ProfileChecking, - ) -> CargoResult> { + ) -> CargoResult { let spec = Packages::from_flags( // TODO Integrate into 'workspace' self._is_present("workspace") || self._is_present("all"), @@ -455,7 +455,6 @@ pub trait ArgMatchesExt { } let opts = CompileOptions { - config, build_config, features: self._values_of("features"), all_features: self._is_present("all-features"), @@ -487,13 +486,13 @@ pub trait ArgMatchesExt { Ok(opts) } - fn compile_options_for_single_package<'a>( + fn compile_options_for_single_package( &self, - config: &'a Config, + config: &Config, mode: CompileMode, - workspace: Option<&Workspace<'a>>, + workspace: Option<&Workspace<'_>>, profile_checking: ProfileChecking, - ) -> CargoResult> { + ) -> CargoResult { let mut compile_opts = self.compile_options(config, mode, workspace, profile_checking)?; compile_opts.spec = Packages::Packages(self._values_of("package")); Ok(compile_opts) @@ -566,7 +565,7 @@ about this warning."; fn check_optional_opts( &self, workspace: &Workspace<'_>, - compile_opts: &CompileOptions<'_>, + compile_opts: &CompileOptions, ) -> CargoResult<()> { if self.is_present_with_zero_values("example") { print_available_examples(workspace, compile_opts)?; diff --git a/src/cargo/util/errors.rs b/src/cargo/util/errors.rs index a65a3d37b84..cfa13afa7e4 100644 --- a/src/cargo/util/errors.rs +++ b/src/cargo/util/errors.rs @@ -259,7 +259,7 @@ impl CargoTestError { } } - pub fn hint(&self, ws: &Workspace<'_>, opts: &CompileOptions<'_>) -> String { + pub fn hint(&self, ws: &Workspace<'_>, opts: &CompileOptions) -> String { match self.test { Test::UnitTest { ref kind, diff --git a/src/cargo/util/workspace.rs b/src/cargo/util/workspace.rs index b3c4966cf33..fbfe11d8433 100644 --- a/src/cargo/util/workspace.rs +++ b/src/cargo/util/workspace.rs @@ -7,7 +7,7 @@ use std::fmt::Write; fn get_available_targets<'a>( filter_fn: fn(&Target) -> bool, ws: &'a Workspace<'_>, - options: &'a CompileOptions<'_>, + options: &'a CompileOptions, ) -> CargoResult> { let packages = options.spec.get_packages(ws)?; @@ -29,7 +29,7 @@ fn get_available_targets<'a>( fn print_available( filter_fn: fn(&Target) -> bool, ws: &Workspace<'_>, - options: &CompileOptions<'_>, + options: &CompileOptions, option_name: &str, plural_name: &str, ) -> CargoResult<()> { @@ -49,27 +49,18 @@ fn print_available( bail!("{}", output) } -pub fn print_available_examples( - ws: &Workspace<'_>, - options: &CompileOptions<'_>, -) -> CargoResult<()> { +pub fn print_available_examples(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available(Target::is_example, ws, options, "--example", "examples") } -pub fn print_available_binaries( - ws: &Workspace<'_>, - options: &CompileOptions<'_>, -) -> CargoResult<()> { +pub fn print_available_binaries(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available(Target::is_bin, ws, options, "--bin", "binaries") } -pub fn print_available_benches( - ws: &Workspace<'_>, - options: &CompileOptions<'_>, -) -> CargoResult<()> { +pub fn print_available_benches(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available(Target::is_bench, ws, options, "--bench", "benches") } -pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions<'_>) -> CargoResult<()> { +pub fn print_available_tests(ws: &Workspace<'_>, options: &CompileOptions) -> CargoResult<()> { print_available(Target::is_test, ws, options, "--test", "tests") }