Skip to content

Commit 6a79008

Browse files
committed
Don't emit "executable" JSON field for non-executables.
1 parent f9089fc commit 6a79008

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
@@ -1638,6 +1638,89 @@ fn doc_message_format() {
16381638
.run();
16391639
}
16401640

1641+
#[cargo_test]
1642+
fn doc_json_artifacts() {
1643+
// Checks the output of json artifact messages.
1644+
let p = project()
1645+
.file("src/lib.rs", "")
1646+
.file("src/bin/somebin.rs", "fn main() {}")
1647+
.build();
1648+
1649+
p.cargo("doc --message-format=json")
1650+
.with_json_contains_unordered(
1651+
r#"
1652+
{
1653+
"reason": "compiler-artifact",
1654+
"package_id": "foo 0.0.1 [..]",
1655+
"manifest_path": "[ROOT]/foo/Cargo.toml",
1656+
"target":
1657+
{
1658+
"kind": ["lib"],
1659+
"crate_types": ["lib"],
1660+
"name": "foo",
1661+
"src_path": "[ROOT]/foo/src/lib.rs",
1662+
"edition": "2015",
1663+
"doc": true,
1664+
"doctest": true,
1665+
"test": true
1666+
},
1667+
"profile": "{...}",
1668+
"features": [],
1669+
"filenames": ["[ROOT]/foo/target/debug/deps/libfoo-[..].rmeta"],
1670+
"executable": null,
1671+
"fresh": false
1672+
}
1673+
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/doc/foo/index.html"],
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": ["bin"],
1703+
"crate_types": ["bin"],
1704+
"name": "somebin",
1705+
"src_path": "[ROOT]/foo/src/bin/somebin.rs",
1706+
"edition": "2015",
1707+
"doc": true,
1708+
"doctest": false,
1709+
"test": true
1710+
},
1711+
"profile": "{...}",
1712+
"features": [],
1713+
"filenames": ["[ROOT]/foo/target/doc/somebin/index.html"],
1714+
"executable": null,
1715+
"fresh": false
1716+
}
1717+
1718+
{"reason":"build-finished","success":true}
1719+
"#,
1720+
)
1721+
.run();
1722+
}
1723+
16411724
#[cargo_test]
16421725
fn short_message_format() {
16431726
let p = project().file("src/lib.rs", BAD_INTRA_LINK_LIB).build();

0 commit comments

Comments
 (0)