Skip to content

Commit 31fe5fd

Browse files
committed
Preserve script name
Gives better error messages
1 parent c81321a commit 31fe5fd

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/main.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ struct InputAction {
297297
/// Directory where the package should live.
298298
pkg_path: PathBuf,
299299

300+
script_name: String,
301+
300302
/**
301303
Is the package directory in the cache?
302304
@@ -333,7 +335,7 @@ impl InputAction {
333335
}
334336

335337
fn script_path(&self) -> PathBuf {
336-
self.pkg_path.join("main.rs")
338+
self.pkg_path.join(&self.script_name)
337339
}
338340

339341
fn cargo(&self, script_args: &[String]) -> MainResult<Command> {
@@ -458,8 +460,16 @@ fn decide_action_for(
458460
_ => None,
459461
});
460462

461-
let (mani_str, script_str) =
462-
manifest::split_input(input, &deps, &prelude, &bin_name, toolchain_version.clone())?;
463+
let script_name = format!("{}.rs", input.safe_name());
464+
465+
let (mani_str, script_str) = manifest::split_input(
466+
input,
467+
&deps,
468+
&prelude,
469+
&bin_name,
470+
&script_name,
471+
toolchain_version.clone(),
472+
)?;
463473

464474
// Forcibly override some flags based on build kind.
465475
let debug = match args.build_kind {
@@ -473,6 +483,7 @@ fn decide_action_for(
473483
force_compile: args.force,
474484
execute: !args.gen_pkg_only,
475485
pkg_path,
486+
script_name,
476487
using_cache,
477488
toolchain_version,
478489
debug,

src/manifest.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn split_input(
2323
deps: &[(String, String)],
2424
prelude_items: &[String],
2525
bin_name: &str,
26+
script_name: &str,
2627
toolchain: Option<String>,
2728
) -> MainResult<(String, String)> {
2829
fn contains_main_method(line: &str) -> bool {
@@ -85,7 +86,7 @@ pub fn split_input(
8586
info!("source: {:?}", source);
8687

8788
// It's-a mergin' time!
88-
let def_mani = default_manifest(input, bin_name, toolchain);
89+
let def_mani = default_manifest(input, bin_name, script_name, toolchain);
8990
let dep_mani = deps_manifest(deps)?;
9091

9192
let mani = merge_manifest(def_mani, part_mani)?;
@@ -110,15 +111,16 @@ strip = true
110111
#[test]
111112
fn test_split_input() {
112113
let bin_name = "binary-name".to_string();
114+
let script_name = "main.rs".to_string();
113115
let toolchain = None;
114116
macro_rules! si {
115117
($i:expr) => {
116-
split_input(&$i, &[], &[], &bin_name, toolchain.clone()).ok()
118+
split_input(&$i, &[], &[], &bin_name, &script_name, toolchain.clone()).ok()
117119
};
118120
}
119121

120122
let f = |c: &str| {
121-
let dummy_path: ::std::path::PathBuf = "p".into();
123+
let dummy_path: ::std::path::PathBuf = "main.rs".into();
122124
Input::File("n".to_string(), dummy_path, c.to_string())
123125
};
124126

@@ -203,6 +205,7 @@ fn main() {}"#
203205
&[],
204206
&[],
205207
&bin_name,
208+
"main.rs",
206209
Some("stable".to_string())
207210
)
208211
.ok(),
@@ -1089,6 +1092,7 @@ Generates a default Cargo manifest for the given input.
10891092
fn default_manifest(
10901093
input: &Input,
10911094
bin_name: &str,
1095+
script_name: &str,
10921096
toolchain: Option<String>,
10931097
) -> toml::value::Table {
10941098
let mut package_map = toml::map::Map::new();
@@ -1138,7 +1142,7 @@ fn default_manifest(
11381142
);
11391143
bin_map.insert(
11401144
"path".to_string(),
1141-
toml::value::Value::String("main.rs".to_string()),
1145+
toml::value::Value::String(script_name.to_string()),
11421146
);
11431147

11441148
let mut mani_map = toml::map::Map::new();

0 commit comments

Comments
 (0)