Skip to content
Open
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
40 changes: 28 additions & 12 deletions src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ impl<'src> Justfile<'src> {
}

let mut ran = Ran::default();

for invocation in invocations {
let context = ExecutionContext {
config,
Expand All @@ -207,18 +208,32 @@ impl<'src> Justfile<'src> {
search,
};

Self::run_recipe(
&invocation
.arguments
.iter()
.copied()
.map(str::to_string)
.collect::<Vec<String>>(),
&context,
&mut ran,
invocation.recipe,
false,
)?;
let evaluated = &invocation
.arguments
.iter()
.copied()
.map(str::to_string)
.collect::<Vec<String>>();

let result = Self::run_recipe(evaluated, &context, &mut ran, invocation.recipe, false);

let mut evaluator = Evaluator::new(&context, true, &scope);
if let Err(err) = result {
if invocation.recipe.recoveries().next().is_none() || context.config.no_dependencies {
return Err(err);
}

let mut ran = Ran::default();

for Dependency { recipe, arguments } in invocation.recipe.recoveries() {
let evaluated = arguments
.iter()
.map(|argument| evaluator.evaluate_expression(argument))
.collect::<RunResult<Vec<String>>>()?;

Self::run_recipe(&evaluated, &context, &mut ran, recipe, true)?;
}
}
}

Ok(())
Expand Down Expand Up @@ -344,6 +359,7 @@ impl<'src> Justfile<'src> {
}
}

// eprintln!("name:{} scope:{}", recipe.name, is_dependency);
Copy link
Preview

Copilot AI Jul 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove or replace this commented-out debug statement with proper logging or delete it to keep the code clean.

Copilot uses AI. Check for mistakes.

recipe.run(context, &scope, &positional, is_dependency)?;

if !context.config.no_dependencies {
Expand Down
6 changes: 6 additions & 0 deletions src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,12 @@ mod tests {
tokens: (AmpersandAmpersand),
}

test! {
name: bar_bar,
text: "||",
tokens: (BarBar),
}

test! {
name: equals,
text: "=",
Expand Down
19 changes: 18 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,22 @@ impl<'run, 'src> Parser<'run, 'src> {
dependencies.append(&mut subsequents);
}

let if_error = dependencies.len();

if self.accepted(BarBar)? {
let mut recoveries = Vec::new();

while let Some(recovery) = self.accept_dependency()? {
recoveries.push(recovery);
}

if recoveries.is_empty() {
return Err(self.unexpected_token()?);
}

dependencies.append(&mut recoveries);
}

self.expect_eol()?;

let body = self.parse_body()?;
Expand Down Expand Up @@ -1007,6 +1023,7 @@ impl<'run, 'src> Parser<'run, 'src> {
dependencies,
doc: doc.filter(|doc| !doc.is_empty()),
file_depth: self.file_depth,
if_error,
import_offsets: self.import_offsets.clone(),
name,
namepath: self
Expand Down Expand Up @@ -2533,7 +2550,7 @@ mod tests {
column: 9,
width: 1,
kind: UnexpectedToken{
expected: vec![AmpersandAmpersand, Comment, Eof, Eol, Identifier, ParenL],
expected: vec![AmpersandAmpersand, BarBar, Comment, Eof, Eol, Identifier, ParenL],
found: Equals
},
}
Expand Down
13 changes: 11 additions & 2 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) struct Recipe<'src, D = Dependency<'src>> {
pub(crate) doc: Option<String>,
#[serde(skip)]
pub(crate) file_depth: u32,
pub(crate) if_error: usize,
#[serde(skip)]
pub(crate) import_offsets: Vec<usize>,
pub(crate) name: Name<'src>,
Expand Down Expand Up @@ -503,7 +504,11 @@ impl<'src, D> Recipe<'src, D> {
}

pub(crate) fn subsequents(&self) -> impl Iterator<Item = &D> {
self.dependencies.iter().skip(self.priors)
self.dependencies[self.priors..self.if_error].iter()
}

pub(crate) fn recoveries(&self) -> impl Iterator<Item = &D> {
self.dependencies[self.if_error..].iter()
}
}

Expand Down Expand Up @@ -535,10 +540,14 @@ impl<D: Display> ColorDisplay for Recipe<'_, D> {
write!(f, ":")?;

for (i, dependency) in self.dependencies.iter().enumerate() {
if i == self.priors {
if i == self.priors && self.subsequents().next().is_some() {
write!(f, " &&")?;
}

if i == self.if_error {
write!(f, " ||")?;
}

write!(f, " {dependency}")?;
}

Expand Down
1 change: 1 addition & 0 deletions src/unresolved_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ impl<'src> UnresolvedRecipe<'src> {
dependencies,
doc: self.doc,
file_depth: self.file_depth,
if_error: self.if_error,
import_offsets: self.import_offsets,
name: self.name,
namepath: self.namepath,
Expand Down
21 changes: 21 additions & 0 deletions tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,27 @@ fn no_trailing_newline() {
.run();
}

#[test]
fn recovery() {
Test::new()
.arg("--dump")
.justfile(
"
bar:
foo: || bar
echo foo",
)
.stdout(
"
bar:

foo: || bar
echo foo
",
)
.run();
}

#[test]
fn subsequent() {
Test::new()
Expand Down
4 changes: 4 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ struct Recipe<'a> {
body: Vec<Value>,
dependencies: Vec<Dependency<'a>>,
doc: Option<&'a str>,
if_error: u32,
name: &'a str,
namepath: &'a str,
parameters: Vec<Parameter<'a>>,
Expand Down Expand Up @@ -277,6 +278,7 @@ fn dependencies() {
..default()
}]
.into(),
if_error: 1,
priors: 1,
..default()
},
Expand Down Expand Up @@ -356,6 +358,7 @@ fn dependency_argument() {
.into(),
}]
.into(),
if_error: 1,
priors: 1,
..default()
},
Expand Down Expand Up @@ -601,6 +604,7 @@ fn priors() {
.into(),
name: "b",
namepath: "b",
if_error: 2,
priors: 1,
..default()
},
Expand Down
1 change: 1 addition & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ mod private;
mod quiet;
mod quote;
mod readme;
mod recoveries;
mod recursion_limit;
mod regexes;
mod request;
Expand Down
2 changes: 1 addition & 1 deletion tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ fn unexpected_token_in_dependency_position() {
.arg("foo")
.justfile("foo: 'bar'")
.stderr(
"error: Expected '&&', comment, end of file, end of line, \
"error: Expected '&&', '||', comment, end of file, end of line, \
identifier, or '(', but found string
——▶ justfile:1:6
Expand Down
25 changes: 25 additions & 0 deletions tests/no_dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ fn skip_prior_dependency() {
.run();
}

#[test]
fn skip_recovery_deps() {
Test::new()
.justfile(
"
a: || b
@echo 'a'
exit 1
b:
@echo 'b'

",
)
.args(["--no-deps"])
.stdout("a\n")
.stderr(
"
exit 1
error: Recipe `a` failed on line 3 with exit code 1
",
)
.status(EXIT_FAILURE)
.run();
}

#[test]
fn skip_dependency_multi() {
Test::new()
Expand Down
Loading