Skip to content

Commit b0d65af

Browse files
committed
Auto merge of #5333 - dwijnand:warn-install-2018, r=matklad
Warn/error when cargo installing the cwd in 2015/2018 Fixes #5327 submitted for early review. feedback very welcome, I'm happy to iterate (and learn).
2 parents f2d51b9 + d8f7828 commit b0d65af

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

src/bin/commands/install.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
7979
.unwrap_or_default()
8080
.collect::<Vec<_>>();
8181

82+
let mut from_cwd = false;
83+
8284
let source = if let Some(url) = args.value_of("git") {
8385
let url = url.to_url()?;
8486
let gitref = if let Some(branch) = args.value_of("branch") {
@@ -94,6 +96,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
9496
} else if let Some(path) = args.value_of_path("path", config) {
9597
SourceId::for_path(&path)?
9698
} else if krates.is_empty() {
99+
from_cwd = true;
97100
SourceId::for_path(config.cwd())?
98101
} else {
99102
SourceId::crates_io(config)?
@@ -109,6 +112,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
109112
root,
110113
krates,
111114
&source,
115+
from_cwd,
112116
version,
113117
&compile_opts,
114118
args.is_present("force"),

src/cargo/core/source/source_id.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ struct SourceIdInner {
3636
name: Option<String>,
3737
}
3838

39-
/// The possible kinds of code source. Along with a URL, this fully defines the
39+
/// The possible kinds of code source. Along with SourceIdInner this fully defines the
4040
/// source
4141
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
4242
enum Kind {

src/cargo/ops/cargo_install.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use semver::{Version, VersionReq};
1010
use tempfile::Builder as TempFileBuilder;
1111
use toml;
1212

13-
use core::{Dependency, Package, PackageIdSpec, Source, SourceId};
13+
use core::{Dependency, Edition, Package, PackageIdSpec, Source, SourceId};
1414
use core::{PackageId, Workspace};
1515
use ops::{self, CompileFilter, DefaultExecutor};
1616
use sources::{GitSource, PathSource, SourceConfigMap};
@@ -57,6 +57,7 @@ pub fn install(
5757
root: Option<&str>,
5858
krates: Vec<&str>,
5959
source_id: &SourceId,
60+
from_cwd: bool,
6061
vers: Option<&str>,
6162
opts: &ops::CompileOptions,
6263
force: bool,
@@ -70,6 +71,7 @@ pub fn install(
7071
&map,
7172
krates.into_iter().next(),
7273
source_id,
74+
from_cwd,
7375
vers,
7476
opts,
7577
force,
@@ -88,6 +90,7 @@ pub fn install(
8890
&map,
8991
Some(krate),
9092
source_id,
93+
from_cwd,
9194
vers,
9295
opts,
9396
force,
@@ -149,6 +152,7 @@ fn install_one(
149152
map: &SourceConfigMap,
150153
krate: Option<&str>,
151154
source_id: &SourceId,
155+
from_cwd: bool,
152156
vers: Option<&str>,
153157
opts: &ops::CompileOptions,
154158
force: bool,
@@ -229,6 +233,23 @@ fn install_one(
229233
};
230234
let pkg = ws.current()?;
231235

236+
if from_cwd {
237+
match pkg.manifest().edition() {
238+
Edition::Edition2015 =>
239+
config.shell().warn("To build the current package use `cargo build`, to install the current package run `cargo install --path .`")?
240+
,
241+
Edition::Edition2018 =>
242+
bail!(
243+
"To build the current package use `cargo build`, \
244+
to install the current package run `cargo install --path .`, \
245+
otherwise specify a crate to install from \
246+
crates.io, or use --path or --git to \
247+
specify alternate source"
248+
)
249+
,
250+
}
251+
};
252+
232253
config.shell().status("Installing", pkg)?;
233254

234255
// Preflight checks to check up front whether we'll overwrite something.

tests/testsuite/install.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::fs::{self, File, OpenOptions};
33
use std::io::prelude::*;
44

55
use cargo::util::ProcessBuilder;
6+
use cargotest::ChannelChanger;
67
use cargotest::install::{cargo_home, has_installed_exe};
78
use cargotest::support::git;
89
use cargotest::support::paths;
@@ -1008,11 +1009,50 @@ fn installs_from_cwd_by_default() {
10081009

10091010
assert_that(
10101011
cargo_process("install").cwd(p.root()),
1011-
execs().with_status(0),
1012+
execs().with_status(0).with_stderr_contains(
1013+
"\
1014+
warning: To build the current package use `cargo build`, to install the current package run `cargo install --path .`
1015+
",
1016+
),
10121017
);
10131018
assert_that(cargo_home(), has_installed_exe("foo"));
10141019
}
10151020

1021+
#[test]
1022+
fn installs_from_cwd_with_2018_warnings() {
1023+
if !cargotest::is_nightly() {
1024+
// Stable rust won't have the edition option. Remove this once it
1025+
// is stabilized.
1026+
return;
1027+
}
1028+
let p = project("foo")
1029+
.file(
1030+
"Cargo.toml",
1031+
r#"
1032+
cargo-features = ["edition"]
1033+
1034+
[package]
1035+
name = "foo"
1036+
version = "0.1.0"
1037+
authors = []
1038+
rust = "2018"
1039+
"#,
1040+
)
1041+
.file("src/main.rs", "fn main() {}")
1042+
.build();
1043+
1044+
assert_that(
1045+
cargo_process("install").cwd(p.root()).masquerade_as_nightly_cargo(),
1046+
execs().with_status(101).with_stderr_contains(
1047+
"error: To build the current package use `cargo build`, \
1048+
to install the current package run `cargo install --path .`, \
1049+
otherwise specify a crate to install from crates.io, \
1050+
or use --path or --git to specify alternate source\
1051+
"),
1052+
);
1053+
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
1054+
}
1055+
10161056
#[test]
10171057
fn do_not_rebuilds_on_local_install() {
10181058
let p = project("foo")

tests/testsuite/required_features.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -742,12 +742,12 @@ fn install_default_features() {
742742
.file("examples/foo.rs", "fn main() {}")
743743
.build();
744744

745-
assert_that(p.cargo("install"), execs().with_status(0));
745+
assert_that(p.cargo("install --path ."), execs().with_status(0));
746746
assert_that(cargo_home(), has_installed_exe("foo"));
747747
assert_that(p.cargo("uninstall").arg("foo"), execs().with_status(0));
748748

749749
assert_that(
750-
p.cargo("install").arg("--no-default-features"),
750+
p.cargo("install --path .").arg("--no-default-features"),
751751
execs().with_status(101).with_stderr(format!(
752752
"\
753753
[INSTALLING] foo v0.0.1 ([..])
@@ -758,12 +758,12 @@ fn install_default_features() {
758758
);
759759
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
760760

761-
assert_that(p.cargo("install").arg("--bin=foo"), execs().with_status(0));
761+
assert_that(p.cargo("install --path .").arg("--bin=foo"), execs().with_status(0));
762762
assert_that(cargo_home(), has_installed_exe("foo"));
763763
assert_that(p.cargo("uninstall").arg("foo"), execs().with_status(0));
764764

765765
assert_that(
766-
p.cargo("install")
766+
p.cargo("install --path .")
767767
.arg("--bin=foo")
768768
.arg("--no-default-features"),
769769
execs().with_status(101).with_stderr(format!(
@@ -781,14 +781,14 @@ Consider enabling them by passing e.g. `--features=\"a\"`
781781
assert_that(cargo_home(), is_not(has_installed_exe("foo")));
782782

783783
assert_that(
784-
p.cargo("install").arg("--example=foo"),
784+
p.cargo("install --path .").arg("--example=foo"),
785785
execs().with_status(0),
786786
);
787787
assert_that(cargo_home(), has_installed_exe("foo"));
788788
assert_that(p.cargo("uninstall").arg("foo"), execs().with_status(0));
789789

790790
assert_that(
791-
p.cargo("install")
791+
p.cargo("install --path .")
792792
.arg("--example=foo")
793793
.arg("--no-default-features"),
794794
execs().with_status(101).with_stderr(format!(
@@ -868,21 +868,21 @@ fn install_multiple_required_features() {
868868
.file("src/foo_2.rs", "fn main() {}")
869869
.build();
870870

871-
assert_that(p.cargo("install"), execs().with_status(0));
871+
assert_that(p.cargo("install --path ."), execs().with_status(0));
872872
assert_that(cargo_home(), is_not(has_installed_exe("foo_1")));
873873
assert_that(cargo_home(), has_installed_exe("foo_2"));
874874
assert_that(p.cargo("uninstall").arg("foo"), execs().with_status(0));
875875

876876
assert_that(
877-
p.cargo("install").arg("--features").arg("c"),
877+
p.cargo("install --path .").arg("--features").arg("c"),
878878
execs().with_status(0),
879879
);
880880
assert_that(cargo_home(), has_installed_exe("foo_1"));
881881
assert_that(cargo_home(), has_installed_exe("foo_2"));
882882
assert_that(p.cargo("uninstall").arg("foo"), execs().with_status(0));
883883

884884
assert_that(
885-
p.cargo("install").arg("--no-default-features"),
885+
p.cargo("install --path .").arg("--no-default-features"),
886886
execs().with_status(101).with_stderr(
887887
"\
888888
[INSTALLING] foo v0.0.1 ([..])
@@ -1166,7 +1166,7 @@ Consider enabling them by passing e.g. `--features=\"bar/a\"`
11661166

11671167
// install
11681168
assert_that(
1169-
p.cargo("install"),
1169+
p.cargo("install --path ."),
11701170
execs().with_status(101).with_stderr(format!(
11711171
"\
11721172
[INSTALLING] foo v0.0.1 ([..])

0 commit comments

Comments
 (0)