Skip to content

Commit 478afe5

Browse files
committed
Fix wrong exe_path on script name clashes
Fixes #8. But note that·`rust-script --clear-cache` needs to be run once after updating to clear incorrectly cached executable paths.
1 parent ed1c225 commit 478afe5

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/main.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ fn parse_args() -> Args {
209209
)
210210

211211
/*
212-
Options that change how cargo script itself behaves, and don't alter what the script will do.
212+
Options that change how .about itself behaves, and don't alter what the script will do.
213213
*/
214214
.arg(Arg::new("build_only")
215215
.about("Build the script, but don't run it.")
@@ -352,7 +352,7 @@ fn try_main() -> MainResult<i32> {
352352
if args.clear_cache {
353353
clean_cache(0)?;
354354
if args.script.is_none() {
355-
println!("cargo script cache cleared.");
355+
println!("rust-script cache cleared.");
356356
return Ok(0);
357357
}
358358
}
@@ -644,7 +644,9 @@ fn gen_pkg_and_compile(input: &Input, action: &InputAction) -> MainResult<()> {
644644
/*
645645
*bursts through wall* It's Cargo Time! (Possibly)
646646
647-
Note that there's a complication here: we want to *temporarily* continue *even if compilation fails*. This is because if we don't, then every time you run `cargo script` on a script you're currently modifying, and it fails to compile, your compiled dependencies get obliterated.
647+
Note that there's a complication here: we want to *temporarily* continue *even if compilation fails*.
648+
This is because if we don't, then every time you run `rust-script` on a script you're currently modifying,
649+
and it fails to compile, your compiled dependencies get obliterated.
648650
649651
This is *really* annoying.
650652
@@ -1391,6 +1393,7 @@ fn cargo_target_by_message(
13911393
#[derive(Deserialize)]
13921394
struct Target {
13931395
name: String,
1396+
kind: Vec<String>,
13941397
}
13951398

13961399
#[derive(Deserialize)]
@@ -1400,9 +1403,13 @@ fn cargo_target_by_message(
14001403
filenames: Vec<PathBuf>,
14011404
}
14021405

1406+
let bin = "bin".to_string();
14031407
while let Some(Ok(line)) = lines.next() {
14041408
if let Ok(mut l) = serde_json::from_str::<Line>(&line).map_err(Box::new) {
1405-
if l.reason == "compiler-artifact" && l.target.name == package_name {
1409+
if l.reason == "compiler-artifact"
1410+
&& l.target.name == package_name
1411+
&& l.target.kind.contains(&bin)
1412+
{
14061413
let _ = child.kill();
14071414
return Ok(l.filenames.swap_remove(0));
14081415
}

tests/data/time.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// cargo-deps: chrono
2+
extern crate chrono;
3+
fn main() {
4+
println!("--output--");
5+
println!("Hello");
6+
}

tests/tests/script.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,12 @@ fn test_script_including_relative() {
135135
)
136136
.unwrap()
137137
}
138+
139+
#[test]
140+
fn script_with_same_name_as_dependency() {
141+
let out = rust_script!("tests/data/time.rs").unwrap();
142+
scan!(out.stdout_output();
143+
("Hello") => ()
144+
)
145+
.unwrap()
146+
}

0 commit comments

Comments
 (0)