diff --git a/src/executor.rs b/src/executor.rs index 6c31aacff5..08fadf985d 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -92,44 +92,32 @@ impl<'a> Executor<'a> { // numbers in errors from generated script match justfile source lines. pub(crate) fn script(&self, recipe: &Recipe, lines: &[String]) -> String { let mut script = String::new(); - - match self { - Self::Shebang(shebang) => { - let mut n = 0; - - for (i, (line, evaluated)) in recipe.body.iter().zip(lines).enumerate() { - if i == 0 { - if shebang.include_shebang_line() { - script.push_str(evaluated); - script.push('\n'); - n += 1; - } - } else { - while n < line.number { - script.push('\n'); - n += 1; - } - - script.push_str(evaluated); - script.push('\n'); - n += 1; - } + let mut n = 0; + let shebangs = recipe + .body + .iter() + .take_while(|line| line.is_shebang()) + .count(); + + if let Self::Shebang(shebang) = self { + for shebang_line in &lines[..shebangs] { + if shebang.include_shebang_line() { + script.push_str(shebang_line); } + script.push('\n'); + n += 1; } - Self::Command(_) => { - let mut n = 0; - - for (line, evaluated) in recipe.body.iter().zip(lines) { - while n < line.number { - script.push('\n'); - n += 1; - } + } - script.push_str(evaluated); - script.push('\n'); - n += 1; - } + for (line, text) in recipe.body.iter().zip(lines).skip(n) { + while n < line.number { + script.push('\n'); + n += 1; } + + script.push_str(text); + script.push('\n'); + n += 1; } script diff --git a/tests/script.rs b/tests/script.rs index 0a2931650e..d5d0e2cbae 100644 --- a/tests/script.rs +++ b/tests/script.rs @@ -243,6 +243,43 @@ b +c +", + ) + .run(); +} + +#[cfg(not(windows))] +#[test] +fn multiline_shebang_line_numbers() { + Test::new() + .justfile( + "foo: + #!/usr/bin/env cat + #!shebang + #!shebang + + a + + b + + + c + + +", + ) + .stdout( + "#!/usr/bin/env cat +#!shebang +#!shebang + + +a + +b + + c ", ) diff --git a/tests/shebang.rs b/tests/shebang.rs index e5ae23d99b..a7cceece04 100644 --- a/tests/shebang.rs +++ b/tests/shebang.rs @@ -44,6 +44,18 @@ default: stdout: "Hello-World\r\n", } +#[cfg(windows)] +test! { + name: multi_line_cmd_shebangs_are_removed, + justfile: r#" +default: + #!cmd.exe /c + #!foo + @echo Hello-World +"#, + stdout: "Hello-World\r\n", +} + #[test] fn simple() { Test::new()