diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 9b06fc2cf80..34ce25b563c 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -81,17 +81,6 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { config.reload_rooted_at(config.home().clone().into_path_unlocked())?; } - let workspace = args.workspace(config).ok(); - let mut compile_opts = args.compile_options( - config, - CompileMode::Build, - workspace.as_ref(), - ProfileChecking::Checked, - )?; - - compile_opts.build_config.requested_profile = - args.get_profile_name(config, "release", ProfileChecking::Checked)?; - let krates = args .values_of("crate") .unwrap_or_default() @@ -127,6 +116,26 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult { let version = args.value_of("version"); let root = args.value_of("root"); + // We only provide worksapce information for local crate installation from + // one of the following sources: + // - From current working directory (only work for edition 2015). + // - From a specific local file path. + let workspace = if from_cwd || args.is_present("path") { + args.workspace(config).ok() + } else { + None + }; + + let mut compile_opts = args.compile_options( + config, + CompileMode::Build, + workspace.as_ref(), + ProfileChecking::Checked, + )?; + + compile_opts.build_config.requested_profile = + args.get_profile_name(config, "release", ProfileChecking::Checked)?; + if args.is_present("list") { ops::install_list(root, config)?; } else { diff --git a/tests/testsuite/install.rs b/tests/testsuite/install.rs index 875a7a636cf..8e8ba337f5b 100644 --- a/tests/testsuite/install.rs +++ b/tests/testsuite/install.rs @@ -1587,3 +1587,68 @@ fn install_yanked_cargo_package() { ) .run(); } + +#[cargo_test] +fn install_cargo_package_in_a_patched_workspace() { + pkg("foo", "0.1.0"); + pkg("fizz", "1.0.0"); + + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "bar" + version = "0.1.0" + authors = [] + + [workspace] + members = ["baz"] + "#, + ) + .file("src/main.rs", "fn main() {}") + .file( + "baz/Cargo.toml", + r#" + [package] + name = "baz" + version = "0.1.0" + authors = [] + + [dependencies] + fizz = "1" + + [patch.crates-io] + fizz = { version = "=1.0.0" } + "#, + ) + .file("baz/src/lib.rs", "") + .build(); + + let stderr = "\ +[WARNING] patch for the non root package will be ignored, specify patch at the workspace root: +package: [..]/foo/baz/Cargo.toml +workspace: [..]/foo/Cargo.toml +"; + p.cargo("check").with_stderr_contains(&stderr).run(); + + // A crate installation must not emit any message from a workspace under + // current working directory. + // See https://github.com/rust-lang/cargo/issues/8619 + p.cargo("install foo") + .with_stderr( + "\ +[UPDATING] `[..]` index +[DOWNLOADING] crates ... +[DOWNLOADED] foo v0.1.0 (registry [..]) +[INSTALLING] foo v0.1.0 +[COMPILING] foo v0.1.0 +[FINISHED] release [optimized] target(s) in [..] +[INSTALLING] [..]foo[EXE] +[INSTALLED] package `foo v0.1.0` (executable `foo[EXE]`) +[WARNING] be sure to add `[..]` to your PATH to be able to run the installed binaries +", + ) + .run(); + assert_has_installed_exe(cargo_home(), "foo"); +}