Repository: dtolnay/cargo-expand (language: rust)

Issue:
[feature]Follow the `default-run` field in 'Cargo.toml' just like `cargo run`
Would you please add the feature:   
Follow the `default-run` field in 'Cargo.toml' just like `cargo run`
See https://doc.rust-lang.org/cargo/reference/manifest.html#the-default-run-field

`cargo expand` will just work as `cargo expand --bin a`, with the following configuration:
```toml
[package]
default-run = "a"

[[bin]]
name = "a"
path = "a.rs"

[[bin]]
name = "b"
path = "b.rs"
```


Discussion / hints:

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index df9e779..fd98b3b 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -185,12 +185,22 @@ version = "1.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "38fcc2979eff34a4b84e1cf9a1e3da42a7d44b3b690a40cdcb23e3d556cfb2e5"
 
+[[package]]
+name = "camino"
+version = "1.1.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c"
+dependencies = [
+ "serde",
+]
+
 [[package]]
 name = "cargo-expand"
 version = "1.0.59"
 dependencies = [
  "bat",
  "cargo-subcommand-metadata",
+ "cargo_metadata",
  "clap",
  "is-terminal",
  "prettyplease",
@@ -209,12 +219,35 @@ dependencies = [
 name = "cargo-expand-test"
 version = "0.0.0"
 
+[[package]]
+name = "cargo-platform"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cbdb825da8a5df079a43676dbe042702f1707b1109f713a01420fbb4cc71fa27"
+dependencies = [
+ "serde",
+]
+
 [[package]]
 name = "cargo-subcommand-metadata"
 version = "0.1.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "a33d3b80a8db16c4ad7676653766a8e59b5f95443c8823cb7cff587b90cb91ba"
 
+[[package]]
+name = "cargo_metadata"
+version = "0.15.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eee4243f1f26fc7a42710e7439c149e2b10b05472f88090acce52632f231a73a"
+dependencies = [
+ "camino",
+ "cargo-platform",
+ "semver",
+ "serde",
+ "serde_json",
+ "thiserror",
+]
+
 [[package]]
 name = "cc"
 version = "1.0.79"
@@ -867,6 +900,9 @@ name = "semver"
 version = "1.0.17"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed"
+dependencies = [
+ "serde",
+]
 
 [[package]]
 name = "serde"
diff --git a/Cargo.toml b/Cargo.toml
index 0b78ccd..2377b25 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -19,6 +19,7 @@ prettyplease = []
 [dependencies]
 bat = { version = "0.23", default-features = false, features = ["paging", "regex-fancy"] }
 cargo-subcommand-metadata = "0.1"
+cargo_metadata = "0.15.4"
 clap = { version = "4", features = ["deprecated", "derive"] }
 is-terminal = "0.4"
 prettyplease = { version = "0.2.9", features = ["verbatim"] }
diff --git a/src/main.rs b/src/main.rs
index 6b5a7d1..2ba94e3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -282,28 +282,50 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
         line.arg("--no-default-features");
     }
 
+    let mut no_build_target = true;
     if args.lib {
         line.arg("--lib");
+        no_build_target = false;
     }
 
     if let Some(bin) = &args.bin {
         line.arg("--bin");
         line.args(bin);
+        no_build_target = false;
     }
 
     if let Some(example) = &args.example {
         line.arg("--example");
         line.args(example);
+        no_build_target = false;
     }
 
     if let Some(test) = &args.test {
         line.arg("--test");
         line.args(test);
+        no_build_target = false;
     }
 
     if let Some(bench) = &args.bench {
         line.arg("--bench");
         line.args(bench);
+        no_build_target = false;
+    }
+
+    if no_build_target {
+        match cargo_metadata(&args.manifest_path) {
+            Ok(cargo_metadata) => {
+                if let Some(root_package) = cargo_metadata.root_package() {
+                    if let Some(ref default_run) = root_package.default_run {
+                        line.arg("--bin");
+                        line.args(Some(default_run));
+                    }
+                }
+            },
+            Err(err) => {
+                let _ = writeln!(io::stderr(), "WARNING: run cargo metadata fail: {}", err);
+            },
+        }
     }
 
     if let Some(target) = &args.target {
@@ -482,3 +504,14 @@ fn get_color(args: &Expand, config: &Config) -> Coloring {
 
     Coloring::Auto // default
 }
+
+fn cargo_metadata(manifest_path: &Option<PathBuf>) -> cargo_metadata::Result<cargo_metadata::Metadata> {
+    let mut cmd = cargo_metadata::MetadataCommand::new();
+    match manifest_path {
+        Some(manifest_path) => {
+            cmd.manifest_path(manifest_path);
+        },
+        None => {},
+    }
+    cmd.exec()
+}
\ No newline at end of file


Test patch: