Skip to content

Commit a4cdd75

Browse files
committed
Auto merge of #10171 - ehuss:no-executable-doc, r=alexcrichton
Don't emit "executable" JSON field for non-executables. The "executable" field of JSON artifact messages was accidentally filled (with the path to `index.html`) when documenting a binary target. This fixes it so that it is null. Closes #10149
2 parents dadeae8 + 6a79008 commit a4cdd75

File tree

3 files changed

+103
-12
lines changed

3 files changed

+103
-12
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,15 @@ impl CompileMode {
219219
pub fn is_run_custom_build(self) -> bool {
220220
self == CompileMode::RunCustomBuild
221221
}
222+
223+
/// Returns `true` if this mode may generate an executable.
224+
///
225+
/// Note that this also returns `true` for building libraries, so you also
226+
/// have to check the target.
227+
pub fn generates_executable(self) -> bool {
228+
matches!(
229+
self,
230+
CompileMode::Test | CompileMode::Bench | CompileMode::Build
231+
)
232+
}
222233
}

src/cargo/core/compiler/context/mod.rs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,16 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
294294

295295
/// Returns the executable for the specified unit (if any).
296296
pub fn get_executable(&mut self, unit: &Unit) -> CargoResult<Option<PathBuf>> {
297-
for output in self.outputs(unit)?.iter() {
298-
if output.flavor != FileFlavor::Normal {
299-
continue;
300-
}
301-
302-
let is_binary = unit.target.is_executable();
303-
let is_test = unit.mode.is_any_test() && !unit.mode.is_check();
304-
305-
if is_binary || is_test {
306-
return Ok(Option::Some(output.bin_dst().clone()));
307-
}
297+
let is_binary = unit.target.is_executable();
298+
let is_test = unit.mode.is_any_test();
299+
if !unit.mode.generates_executable() || !(is_binary || is_test) {
300+
return Ok(None);
308301
}
309-
Ok(None)
302+
Ok(self
303+
.outputs(unit)?
304+
.iter()
305+
.find(|o| o.flavor == FileFlavor::Normal)
306+
.map(|output| output.bin_dst().clone()))
310307
}
311308

312309
pub fn prepare_units(&mut self) -> CargoResult<()> {

tests/testsuite/doc.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,89 @@ fn doc_message_format() {
16601660
.run();
16611661
}
16621662

1663+
#[cargo_test]
1664+
fn doc_json_artifacts() {
1665+
// Checks the output of json artifact messages.
1666+
let p = project()
1667+
.file("src/lib.rs", "")
1668+
.file("src/bin/somebin.rs", "fn main() {}")
1669+
.build();
1670+
1671+
p.cargo("doc --message-format=json")
1672+
.with_json_contains_unordered(
1673+
r#"
1674+
{
1675+
"reason": "compiler-artifact",
1676+
"package_id": "foo 0.0.1 [..]",
1677+
"manifest_path": "[ROOT]/foo/Cargo.toml",
1678+
"target":
1679+
{
1680+
"kind": ["lib"],
1681+
"crate_types": ["lib"],
1682+
"name": "foo",
1683+
"src_path": "[ROOT]/foo/src/lib.rs",
1684+
"edition": "2015",
1685+
"doc": true,
1686+
"doctest": true,
1687+
"test": true
1688+
},
1689+
"profile": "{...}",
1690+
"features": [],
1691+
"filenames": ["[ROOT]/foo/target/debug/deps/libfoo-[..].rmeta"],
1692+
"executable": null,
1693+
"fresh": false
1694+
}
1695+
1696+
{
1697+
"reason": "compiler-artifact",
1698+
"package_id": "foo 0.0.1 [..]",
1699+
"manifest_path": "[ROOT]/foo/Cargo.toml",
1700+
"target":
1701+
{
1702+
"kind": ["lib"],
1703+
"crate_types": ["lib"],
1704+
"name": "foo",
1705+
"src_path": "[ROOT]/foo/src/lib.rs",
1706+
"edition": "2015",
1707+
"doc": true,
1708+
"doctest": true,
1709+
"test": true
1710+
},
1711+
"profile": "{...}",
1712+
"features": [],
1713+
"filenames": ["[ROOT]/foo/target/doc/foo/index.html"],
1714+
"executable": null,
1715+
"fresh": false
1716+
}
1717+
1718+
{
1719+
"reason": "compiler-artifact",
1720+
"package_id": "foo 0.0.1 [..]",
1721+
"manifest_path": "[ROOT]/foo/Cargo.toml",
1722+
"target":
1723+
{
1724+
"kind": ["bin"],
1725+
"crate_types": ["bin"],
1726+
"name": "somebin",
1727+
"src_path": "[ROOT]/foo/src/bin/somebin.rs",
1728+
"edition": "2015",
1729+
"doc": true,
1730+
"doctest": false,
1731+
"test": true
1732+
},
1733+
"profile": "{...}",
1734+
"features": [],
1735+
"filenames": ["[ROOT]/foo/target/doc/somebin/index.html"],
1736+
"executable": null,
1737+
"fresh": false
1738+
}
1739+
1740+
{"reason":"build-finished","success":true}
1741+
"#,
1742+
)
1743+
.run();
1744+
}
1745+
16631746
#[cargo_test]
16641747
fn short_message_format() {
16651748
let p = project().file("src/lib.rs", BAD_INTRA_LINK_LIB).build();

0 commit comments

Comments
 (0)