Skip to content

Avoid shebang causing line number mismatch #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rust-script"
version = "0.24.0"
version = "0.25.0"
edition = "2021"
rust-version = "1.64"
authors = ["Fredrik Fornwall <[email protected]>"]
Expand Down
19 changes: 12 additions & 7 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ pub fn split_input(
let (part_mani, source, template, sub_prelude) = match input {
Input::File(_, _, content) => {
assert_eq!(prelude_items.len(), 0);
let content = strip_shebang(content);
let (content, shebang_used) = strip_shebang(content);
let (manifest, source) =
find_embedded_manifest(content).unwrap_or((Manifest::Toml(""), content));

let source = if contains_main_method(source) {
source.to_string()
if shebang_used {
format!("//\n{}", source)
} else {
source.to_string()
}
} else {
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{\n {{\n {} }}\n Ok(())\n}}", source)
format!("fn main() -> Result<(), Box<dyn std::error::Error+Sync+Send>> {{ {{\n{} }}\n Ok(())\n}}", source)
};
(manifest, source, consts::FILE_TEMPLATE, false)
}
Expand Down Expand Up @@ -169,7 +173,8 @@ name = "n"
version = "0.1.0""#,
STRIP_SECTION
),
r#"fn main() {}"#
r#"//
fn main() {}"#
)
);

Expand Down Expand Up @@ -395,11 +400,11 @@ fn main() {}
/**
Returns a slice of the input string with the leading shebang, if there is one, omitted.
*/
fn strip_shebang(s: &str) -> &str {
fn strip_shebang(s: &str) -> (&str, bool) {
let re_shebang: Regex = Regex::new(r"^#![^\[].*?(\r\n|\n)").unwrap();
match re_shebang.find(s) {
Some(m) => &s[m.end()..],
None => s,
Some(m) => (&s[m.end()..], true),
None => (s, false),
}
}

Expand Down