Skip to content

Commit 79e8b18

Browse files
authored
Merge pull request #97 from fornwall/avoid-shebang-line-number-mismatch
Avoid shebang causing line number mismatch
2 parents 6776198 + a22bf35 commit 79e8b18

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-script"
3-
version = "0.24.0"
3+
version = "0.25.0"
44
edition = "2021"
55
rust-version = "1.64"
66
authors = ["Fredrik Fornwall <[email protected]>"]

src/manifest.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,18 @@ pub fn split_input(
3434
let (part_mani, source, template, sub_prelude) = match input {
3535
Input::File(_, _, content) => {
3636
assert_eq!(prelude_items.len(), 0);
37-
let content = strip_shebang(content);
37+
let (content, shebang_used) = strip_shebang(content);
3838
let (manifest, source) =
3939
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));
4040

4141
let source = if contains_main_method(source) {
42-
source.to_string()
42+
if shebang_used {
43+
format!("//\n{}", source)
44+
} else {
45+
source.to_string()
46+
}
4347
} else {
44-
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
48+
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n{} }}\n Ok(())\n}}", source)
4549
};
4650
(manifest, source, consts::FILE_TEMPLATE, false)
4751
}
@@ -169,7 +173,8 @@ name = "n"
169173
version = "0.1.0""#,
170174
STRIP_SECTION
171175
),
172-
r#"fn main() {}"#
176+
r#"//
177+
fn main() {}"#
173178
)
174179
);
175180

@@ -395,11 +400,11 @@ fn main() {}
395400
/**
396401
Returns a slice of the input string with the leading shebang, if there is one, omitted.
397402
*/
398-
fn strip_shebang(s: &str) -> &str {
403+
fn strip_shebang(s: &str) -> (&str, bool) {
399404
let re_shebang: Regex = Regex::new(r"^#![^\[].*?(\r\n|\n)").unwrap();
400405
match re_shebang.find(s) {
401-
Some(m) => &s[m.end()..],
402-
None => s,
406+
Some(m) => (&s[m.end()..], true),
407+
None => (s, false),
403408
}
404409
}
405410

0 commit comments

Comments
 (0)