Skip to content

Commit

Permalink
fix(embedded): Ensure we don't auto-discover any targets
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Jun 15, 2023
1 parent 2d085ff commit eab5985
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
21 changes: 19 additions & 2 deletions src/cargo/util/toml/embedded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const DEFAULT_EDITION: crate::core::features::Edition =
crate::core::features::Edition::LATEST_STABLE;
const DEFAULT_VERSION: &str = "0.0.0";
const DEFAULT_PUBLISH: bool = false;
const AUTO_FIELDS: &[&str] = &["autobins", "autoexamples", "autotests", "autobenches"];

pub fn expand_manifest(
content: &str,
Expand Down Expand Up @@ -56,8 +57,11 @@ fn expand_manifest_(
.or_insert_with(|| toml::Table::new().into())
.as_table_mut()
.ok_or_else(|| anyhow::format_err!("`package` must be a table"))?;
for key in ["workspace", "build", "links"] {
if package.contains_key(key) {
for key in ["workspace", "build", "links"]
.iter()
.chain(AUTO_FIELDS.iter())
{
if package.contains_key(*key) {
anyhow::bail!("`package.{key}` is not allowed in embedded manifests")
}
}
Expand Down Expand Up @@ -88,6 +92,11 @@ fn expand_manifest_(
package
.entry("publish".to_owned())
.or_insert_with(|| toml::Value::Boolean(DEFAULT_PUBLISH));
for field in AUTO_FIELDS {
package
.entry(field.to_owned())
.or_insert_with(|| toml::Value::Boolean(false));
}

let mut bin = toml::Table::new();
bin.insert("name".to_owned(), toml::Value::String(bin_name));
Expand Down Expand Up @@ -355,6 +364,10 @@ name = "test"
path = "test.rs"
[package]
autobenches = false
autobins = false
autoexamples = false
autotests = false
edition = "2021"
name = "test"
publish = false
Expand All @@ -380,6 +393,10 @@ path = "test.rs"
time = "0.1.25"
[package]
autobenches = false
autobins = false
autoexamples = false
autotests = false
edition = "2021"
name = "test"
publish = false
Expand Down
10 changes: 7 additions & 3 deletions tests/testsuite/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,12 +523,16 @@ fn main() {

p.cargo("-Zscript script.rs --help")
.masquerade_as_nightly_cargo(&["script"])
.with_status(101)
.with_stdout(
r#"Hello world!
"#,
)
.with_stderr(
"\
[WARNING] `package.edition` is unspecifiead, defaulting to `2021`
[ERROR] `cargo run` could not determine which binary to run. Use the `--bin` option to specify a binary, or the `default-run` manifest key.
available binaries: not-script, script
[COMPILING] script v0.0.0 ([ROOT]/foo)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]s
[RUNNING] `[..]/debug/script[EXE] --help`
",
)
.run();
Expand Down

0 comments on commit eab5985

Please sign in to comment.