Skip to content

Commit 6ebaa38

Browse files
committed
auto merge of rust-lang#320 : alexcrichton/cargo/fix-doc-bins, r=wycats
This removes the check in the compilation phase, but adds an error if you're documenting a library and a binary with the same name (as the rustdoc output would conflict). Closes rust-lang#318
2 parents 6d78a2f + c64d191 commit 6ebaa38

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

src/cargo/ops/cargo_doc.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
use std::collections::HashSet;
2+
3+
use core::source::Source;
14
use ops;
2-
use util::CargoResult;
5+
use sources::PathSource;
6+
use util::{CargoResult, human};
37

48
pub struct DocOptions<'a> {
59
pub all: bool,
@@ -8,6 +12,27 @@ pub struct DocOptions<'a> {
812

913
pub fn doc(manifest_path: &Path,
1014
options: &mut DocOptions) -> CargoResult<()> {
15+
let mut source = PathSource::for_path(&manifest_path.dir_path());
16+
try!(source.update());
17+
let package = try!(source.get_root_package());
18+
19+
let mut lib_names = HashSet::new();
20+
let mut bin_names = HashSet::new();
21+
for target in package.get_targets().iter().filter(|t| t.get_profile().is_doc()) {
22+
if target.is_lib() {
23+
assert!(lib_names.insert(target.get_name()));
24+
} else {
25+
assert!(bin_names.insert(target.get_name()));
26+
}
27+
}
28+
for bin in bin_names.iter() {
29+
if lib_names.contains(bin) {
30+
return Err(human("Cannot document a package where a library and a \
31+
binary have the same name. Consider renaming one \
32+
or marking the target as `doc = false`"))
33+
}
34+
}
35+
1136
try!(ops::compile(manifest_path, &mut options.compile_opts));
1237
Ok(())
1338
}

src/cargo/ops/cargo_rustc/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,6 @@ fn prepare_rustc(package: &Package, target: &Target, crate_types: Vec<&str>,
237237

238238

239239
fn rustdoc(package: &Package, target: &Target, cx: &mut Context) -> Work {
240-
// Can't document binaries, but they have a doc target listed so we can
241-
// build documentation of dependencies even when `cargo doc` is run.
242-
if target.is_bin() {
243-
return proc() Ok(())
244-
}
245-
246240
let kind = KindTarget;
247241
let pkg_root = package.get_root();
248242
let cx_root = cx.layout(kind).proxy().dest().dir_path().join("doc");

tests/test_cargo_doc.rs

+23-22
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,17 @@ test!(simple {
2727
assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
2828
})
2929

30-
test!(no_build_main {
30+
test!(doc_no_libs {
3131
let p = project("foo")
3232
.file("Cargo.toml", r#"
3333
[package]
3434
name = "foo"
3535
version = "0.0.1"
3636
authors = []
37-
"#)
38-
.file("src/lib.rs", r#"
39-
pub fn foo() {}
40-
"#)
41-
.file("src/main.rs", r#"
42-
bad code
43-
"#);
4437
45-
assert_that(p.cargo_process("cargo-doc"),
46-
execs().with_status(0).with_stdout(format!("\
47-
{compiling} foo v0.0.1 (file:{dir})
48-
",
49-
compiling = COMPILING,
50-
dir = p.root().display()).as_slice()));
51-
})
52-
53-
test!(doc_no_libs {
54-
let p = project("foo")
55-
.file("Cargo.toml", r#"
56-
[package]
38+
[[bin]]
5739
name = "foo"
58-
version = "0.0.1"
59-
authors = []
40+
doc = false
6041
"#)
6142
.file("src/main.rs", r#"
6243
bad code
@@ -212,4 +193,24 @@ test!(doc_only_bin {
212193

213194
assert_that(&p.root().join("target/doc"), existing_dir());
214195
assert_that(&p.root().join("target/doc/bar/index.html"), existing_file());
196+
assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
197+
})
198+
199+
test!(doc_lib_bin_same_name {
200+
let p = project("foo")
201+
.file("Cargo.toml", r#"
202+
[package]
203+
name = "foo"
204+
version = "0.0.1"
205+
authors = []
206+
"#)
207+
.file("src/main.rs", "fn main() {}")
208+
.file("src/lib.rs", "fn foo() {}");
209+
210+
assert_that(p.cargo_process("cargo-doc"),
211+
execs().with_status(101)
212+
.with_stderr("\
213+
Cannot document a package where a library and a binary have the same name. \
214+
Consider renaming one or marking the target as `doc = false`
215+
"));
215216
})

0 commit comments

Comments
 (0)