Skip to content

Commit 66ea3e4

Browse files
wmmc88fornwall
authored andcommitted
fix: RUST_SCRIPT_BASE_PATH reports the wrong path when rust-script executes with --base-path
135
1 parent fb4e627 commit 66ea3e4

File tree

3 files changed

+48
-33
lines changed

3 files changed

+48
-33
lines changed

src/main.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,33 @@ fn try_main() -> MainResult<i32> {
123123

124124
let script_path = std::env::current_dir()?.join(path);
125125

126-
Input::File(script_name, script_path, body)
126+
let base_path = if let Some(base_path_arg) = &args.base_path {
127+
Path::new(base_path_arg).into()
128+
} else {
129+
script_path
130+
.parent()
131+
.expect("couldn't get parent directory for file input base path")
132+
.into()
133+
};
134+
135+
Input::File(script_name, script_path, body, base_path)
136+
}
137+
(expr, true, false) => {
138+
let base_path = if let Some(base_path_arg) = &args.base_path {
139+
Path::new(base_path_arg).into()
140+
} else {
141+
std::env::current_dir().expect("couldn't get current directory for input base path")
142+
};
143+
Input::Expr(expr, base_path)
144+
}
145+
(loop_, false, true) => {
146+
let base_path = if let Some(base_path_arg) = &args.base_path {
147+
Path::new(base_path_arg).into()
148+
} else {
149+
std::env::current_dir().expect("couldn't get current directory for input base path")
150+
};
151+
Input::Loop(loop_, args.count, base_path)
127152
}
128-
(expr, true, false) => Input::Expr(expr),
129-
(loop_, false, true) => Input::Loop(loop_, args.count),
130153
(_, _, _) => {
131154
panic!("Internal error: Invalid args");
132155
}
@@ -499,14 +522,9 @@ fn decide_action_for(
499522

500523
let script_name = format!("{}.rs", input.safe_name());
501524

502-
let base_path = match &args.base_path {
503-
Some(path) => Path::new(path).into(),
504-
None => input.base_path(),
505-
};
506-
507525
let (mani_str, script_path, script_str) = manifest::split_input(
508526
input,
509-
&base_path,
527+
input.base_path(),
510528
&deps,
511529
&prelude,
512530
&pkg_path,
@@ -566,23 +584,23 @@ pub enum Input {
566584
/**
567585
The input is a script file.
568586
569-
The tuple members are: the name, absolute path, script contents.
587+
The tuple members are: the name, absolute path, script contents, base path.
570588
*/
571-
File(String, PathBuf, String),
589+
File(String, PathBuf, String, PathBuf),
572590

573591
/**
574592
The input is an expression.
575593
576-
The tuple member is: the script contents.
594+
The tuple member is: the script contents, base path.
577595
*/
578-
Expr(String),
596+
Expr(String, PathBuf),
579597

580598
/**
581599
The input is a loop expression.
582600
583-
The tuple member is: the script contents, whether the `--count` flag was given.
601+
The tuple member is: the script contents, whether the `--count` flag was given, base path.
584602
*/
585-
Loop(String, bool),
603+
Loop(String, bool, PathBuf),
586604
}
587605

588606
impl Input {
@@ -593,7 +611,7 @@ impl Input {
593611
use crate::Input::*;
594612

595613
match self {
596-
File(_, path, _) => Some(path),
614+
File(_, path, _, _) => Some(path),
597615
Expr(..) => None,
598616
Loop(..) => None,
599617
}
@@ -608,7 +626,7 @@ impl Input {
608626
use crate::Input::*;
609627

610628
match self {
611-
File(name, _, _) => name,
629+
File(name, _, _, _) => name,
612630
Expr(..) => "expr",
613631
Loop(..) => "loop",
614632
}
@@ -646,15 +664,11 @@ impl Input {
646664
/**
647665
Base directory for resolving relative paths.
648666
*/
649-
pub fn base_path(&self) -> PathBuf {
667+
pub fn base_path(&self) -> &PathBuf {
650668
match self {
651-
Self::File(_, path, _) => path
652-
.parent()
653-
.expect("couldn't get parent directory for file input base path")
654-
.into(),
655-
Self::Expr(..) | Self::Loop(..) => {
656-
std::env::current_dir().expect("couldn't get current directory for input base path")
657-
}
669+
Input::File(_, _, _, base_path)
670+
| Input::Expr(_, base_path)
671+
| Input::Loop(_, _, base_path) => base_path,
658672
}
659673
}
660674

@@ -680,7 +694,7 @@ impl Input {
680694
};
681695

682696
match self {
683-
File(_, path, _) => {
697+
File(_, path, _, _) => {
684698
let mut hasher = Sha1::new();
685699

686700
// Hash the path to the script.
@@ -692,7 +706,7 @@ impl Input {
692706
id.push(&*digest);
693707
id
694708
}
695-
Expr(content) => {
709+
Expr(content, _) => {
696710
let mut hasher = hash_deps();
697711

698712
hasher.update(content);
@@ -703,7 +717,7 @@ impl Input {
703717
id.push(&*digest);
704718
id
705719
}
706-
Loop(content, count) => {
720+
Loop(content, count, _) => {
707721
let mut hasher = hash_deps();
708722

709723
// 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() {
757771
"Script".to_string(),
758772
Path::new("path").into(),
759773
"script".to_string(),
774+
Path::new("path").into(),
760775
);
761776
assert_eq!("script", input.package_name());
762777
let input = Input::File(
763778
"1Script".to_string(),
764779
Path::new("path").into(),
765780
"script".to_string(),
781+
Path::new("path").into(),
766782
);
767783
assert_eq!("_1script", input.package_name());
768784
}

src/manifest.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn split_input(
3838

3939
let source_in_package = package_path.as_ref().join(script_name);
4040
let (part_mani, source_path, source, template, sub_prelude) = match input {
41-
Input::File(_, path, content) => {
41+
Input::File(_, path, content, _) => {
4242
assert_eq!(prelude_items.len(), 0);
4343
let content = strip_shebang(content);
4444
let (manifest, source) =
@@ -56,14 +56,14 @@ pub fn split_input(
5656
)
5757
}
5858
}
59-
Input::Expr(content) => (
59+
Input::Expr(content, _) => (
6060
Manifest::Toml(""),
6161
source_in_package,
6262
content.to_string(),
6363
Some(consts::EXPR_TEMPLATE),
6464
true,
6565
),
66-
Input::Loop(content, count) => (
66+
Input::Loop(content, count, _) => (
6767
Manifest::Toml(""),
6868
source_in_package,
6969
content.to_string(),
@@ -152,7 +152,7 @@ fn test_split_input() {
152152

153153
let f = |c: &str| {
154154
let dummy_path: ::std::path::PathBuf = "/dummy/main.rs".into();
155-
Input::File("n".to_string(), dummy_path, c.to_string())
155+
Input::File("n".to_string(), dummy_path.clone(), c.to_string(), dummy_path)
156156
};
157157

158158
macro_rules! r {

src/platform.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ mod inner {
6969

7070
#[cfg(windows)]
7171
pub mod inner {
72-
pub use super::*;
7372

7473
/**
7574
Returns `true` if `rust-script` should force Cargo to use coloured output.

0 commit comments

Comments
 (0)