From 66ea3e49fe87d1df5f24223ea88e49f4c6ec75b2 Mon Sep 17 00:00:00 2001 From: Melvin Wang Date: Thu, 1 Aug 2024 10:52:57 -0700 Subject: [PATCH] fix: `RUST_SCRIPT_BASE_PATH` reports the wrong path when rust-script executes with `--base-path` 135 --- src/main.rs | 72 ++++++++++++++++++++++++++++++------------------- src/manifest.rs | 8 +++--- src/platform.rs | 1 - 3 files changed, 48 insertions(+), 33 deletions(-) diff --git a/src/main.rs b/src/main.rs index 00b17e0..79bb165 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,10 +123,33 @@ fn try_main() -> MainResult { let script_path = std::env::current_dir()?.join(path); - Input::File(script_name, script_path, body) + let base_path = if let Some(base_path_arg) = &args.base_path { + Path::new(base_path_arg).into() + } else { + script_path + .parent() + .expect("couldn't get parent directory for file input base path") + .into() + }; + + Input::File(script_name, script_path, body, base_path) + } + (expr, true, false) => { + let base_path = if let Some(base_path_arg) = &args.base_path { + Path::new(base_path_arg).into() + } else { + std::env::current_dir().expect("couldn't get current directory for input base path") + }; + Input::Expr(expr, base_path) + } + (loop_, false, true) => { + let base_path = if let Some(base_path_arg) = &args.base_path { + Path::new(base_path_arg).into() + } else { + std::env::current_dir().expect("couldn't get current directory for input base path") + }; + Input::Loop(loop_, args.count, base_path) } - (expr, true, false) => Input::Expr(expr), - (loop_, false, true) => Input::Loop(loop_, args.count), (_, _, _) => { panic!("Internal error: Invalid args"); } @@ -499,14 +522,9 @@ fn decide_action_for( let script_name = format!("{}.rs", input.safe_name()); - let base_path = match &args.base_path { - Some(path) => Path::new(path).into(), - None => input.base_path(), - }; - let (mani_str, script_path, script_str) = manifest::split_input( input, - &base_path, + input.base_path(), &deps, &prelude, &pkg_path, @@ -566,23 +584,23 @@ pub enum Input { /** The input is a script file. - The tuple members are: the name, absolute path, script contents. + The tuple members are: the name, absolute path, script contents, base path. */ - File(String, PathBuf, String), + File(String, PathBuf, String, PathBuf), /** The input is an expression. - The tuple member is: the script contents. + The tuple member is: the script contents, base path. */ - Expr(String), + Expr(String, PathBuf), /** The input is a loop expression. - The tuple member is: the script contents, whether the `--count` flag was given. + The tuple member is: the script contents, whether the `--count` flag was given, base path. */ - Loop(String, bool), + Loop(String, bool, PathBuf), } impl Input { @@ -593,7 +611,7 @@ impl Input { use crate::Input::*; match self { - File(_, path, _) => Some(path), + File(_, path, _, _) => Some(path), Expr(..) => None, Loop(..) => None, } @@ -608,7 +626,7 @@ impl Input { use crate::Input::*; match self { - File(name, _, _) => name, + File(name, _, _, _) => name, Expr(..) => "expr", Loop(..) => "loop", } @@ -646,15 +664,11 @@ impl Input { /** Base directory for resolving relative paths. */ - pub fn base_path(&self) -> PathBuf { + pub fn base_path(&self) -> &PathBuf { match self { - Self::File(_, path, _) => path - .parent() - .expect("couldn't get parent directory for file input base path") - .into(), - Self::Expr(..) | Self::Loop(..) => { - std::env::current_dir().expect("couldn't get current directory for input base path") - } + Input::File(_, _, _, base_path) + | Input::Expr(_, base_path) + | Input::Loop(_, _, base_path) => base_path, } } @@ -680,7 +694,7 @@ impl Input { }; match self { - File(_, path, _) => { + File(_, path, _, _) => { let mut hasher = Sha1::new(); // Hash the path to the script. @@ -692,7 +706,7 @@ impl Input { id.push(&*digest); id } - Expr(content) => { + Expr(content, _) => { let mut hasher = hash_deps(); hasher.update(content); @@ -703,7 +717,7 @@ impl Input { id.push(&*digest); id } - Loop(content, count) => { + Loop(content, count, _) => { let mut hasher = hash_deps(); // Make sure to include the [non-]presence of the `--count` flag in the flag, since it changes the actual generated script output. @@ -757,12 +771,14 @@ fn test_package_name() { "Script".to_string(), Path::new("path").into(), "script".to_string(), + Path::new("path").into(), ); assert_eq!("script", input.package_name()); let input = Input::File( "1Script".to_string(), Path::new("path").into(), "script".to_string(), + Path::new("path").into(), ); assert_eq!("_1script", input.package_name()); } diff --git a/src/manifest.rs b/src/manifest.rs index db345aa..13f3533 100644 --- a/src/manifest.rs +++ b/src/manifest.rs @@ -38,7 +38,7 @@ pub fn split_input( let source_in_package = package_path.as_ref().join(script_name); let (part_mani, source_path, source, template, sub_prelude) = match input { - Input::File(_, path, content) => { + Input::File(_, path, content, _) => { assert_eq!(prelude_items.len(), 0); let content = strip_shebang(content); let (manifest, source) = @@ -56,14 +56,14 @@ pub fn split_input( ) } } - Input::Expr(content) => ( + Input::Expr(content, _) => ( Manifest::Toml(""), source_in_package, content.to_string(), Some(consts::EXPR_TEMPLATE), true, ), - Input::Loop(content, count) => ( + Input::Loop(content, count, _) => ( Manifest::Toml(""), source_in_package, content.to_string(), @@ -152,7 +152,7 @@ fn test_split_input() { let f = |c: &str| { let dummy_path: ::std::path::PathBuf = "/dummy/main.rs".into(); - Input::File("n".to_string(), dummy_path, c.to_string()) + Input::File("n".to_string(), dummy_path.clone(), c.to_string(), dummy_path) }; macro_rules! r { diff --git a/src/platform.rs b/src/platform.rs index e8bb5e0..b6fd772 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -69,7 +69,6 @@ mod inner { #[cfg(windows)] pub mod inner { - pub use super::*; /** Returns `true` if `rust-script` should force Cargo to use coloured output.