Skip to content

Commit

Permalink
Don't default to included recipes (#1740)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 22, 2023
1 parent 7337447 commit ab16c04
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ pub(crate) struct Analyzer<'src> {

impl<'src> Analyzer<'src> {
pub(crate) fn analyze(
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
Analyzer::default().justfile(asts, root)
Analyzer::default().justfile(paths, asts, root)
}

fn justfile(
mut self,
paths: &HashMap<PathBuf, PathBuf>,
asts: &HashMap<PathBuf, Ast<'src>>,
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
Expand Down Expand Up @@ -83,9 +85,12 @@ impl<'src> Analyzer<'src> {
aliases.insert(Self::resolve_alias(&recipes, alias)?);
}

let root = paths.get(root).unwrap();

Ok(Justfile {
first: recipes
.values()
.filter(|recipe| recipe.name.path == root)
.fold(None, |accumulator, next| match accumulator {
None => Some(Rc::clone(next)),
Some(previous) => Some(if previous.line_number() < next.line_number() {
Expand Down
17 changes: 11 additions & 6 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ impl Compiler {
) -> RunResult<'src, Compilation<'src>> {
let mut srcs: HashMap<PathBuf, &str> = HashMap::new();
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();

let mut paths: Vec<PathBuf> = Vec::new();
paths.push(root.into());
let mut stack: Vec<PathBuf> = Vec::new();
stack.push(root.into());

while let Some(current) = paths.pop() {
while let Some(current) = stack.pop() {
let (relative, src) = loader.load(root, &current)?;
paths.insert(current.clone(), relative.into());

let tokens = Lexer::lex(relative, src)?;
let mut ast = Parser::parse(&tokens)?;

Expand All @@ -37,14 +40,14 @@ impl Compiler {

*absolute = Some(include.clone());

paths.push(include);
stack.push(include);
}
}

asts.insert(current.clone(), ast.clone());
}

let justfile = Analyzer::analyze(&asts, root)?;
let justfile = Analyzer::analyze(&paths, &asts, root)?;

Ok(Compilation {
asts,
Expand All @@ -61,7 +64,9 @@ impl Compiler {
let root = PathBuf::from("justfile");
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
asts.insert(root.clone(), ast);
Analyzer::analyze(&asts, &root)
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert(root.clone(), root.clone());
Analyzer::analyze(&paths, &asts, &root)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub(crate) enum Error<'src> {
io_error: io::Error,
},
NoChoosableRecipes,
NoDefaultRecipe,
NoRecipes,
NotConfirmed {
recipe: &'src str,
Expand Down Expand Up @@ -350,6 +351,7 @@ impl<'src> ColorDisplay for Error<'src> {
write!(f, "Failed to read justfile at `{path}`: {io_error}")?;
}
NoChoosableRecipes => write!(f, "Justfile contains no choosable recipes.")?,
NoDefaultRecipe => write!(f, "Justfile contains no default recipe.")?,
NoRecipes => write!(f, "Justfile contains no recipes.")?,
NotConfirmed { recipe } => {
write!(f, "Recipe `{recipe}` was not confirmed")?;
Expand Down
4 changes: 3 additions & 1 deletion src/justfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,10 @@ impl<'src> Justfile<'src> {
});
}
vec![recipe.name()]
} else {
} else if self.recipes.is_empty() {
return Err(Error::NoRecipes);
} else {
return Err(Error::NoDefaultRecipe);
};

let arguments = argvec.as_slice();
Expand Down
4 changes: 4 additions & 0 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ impl<'key, V: Keyed<'key>> Table<'key, V> {
self.map.get(key)
}

pub(crate) fn is_empty(&self) -> bool {
self.map.is_empty()
}

pub(crate) fn values(&self) -> btree_map::Values<&'key str, V> {
self.map.values()
}
Expand Down
5 changes: 4 additions & 1 deletion src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ pub(crate) fn analysis_error(
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
asts.insert(root.clone(), ast);

match Analyzer::analyze(&asts, &root) {
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert("justfile".into(), "justfile".into());

match Analyzer::analyze(&paths, &asts, &root) {
Ok(_) => panic!("Analysis unexpectedly succeeded"),
Err(have) => {
let want = CompileError {
Expand Down
27 changes: 22 additions & 5 deletions tests/includes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ fn include_succeeds_with_unstable() {
fn trailing_spaces_after_include_are_ignored() {
Test::new()
.tree(tree! {
"include.justfile": "
a:
@echo A
",
"include.justfile": "",
})
.justfile("!include ./include.justfile\x20")
.justfile(
"
!include ./include.justfile\x20
a:
@echo A
",
)
.arg("--unstable")
.test_round_trip(false)
.stdout("A\n")
Expand Down Expand Up @@ -103,3 +106,17 @@ fn circular_include() {
))
.run();
}

#[test]
fn include_recipes_are_not_default() {
Test::new()
.tree(tree! {
"include.justfile": "bar:",
})
.justfile("!include ./include.justfile")
.arg("--unstable")
.test_round_trip(false)
.status(EXIT_FAILURE)
.stderr("error: Justfile contains no default recipe.\n")
.run();
}

0 comments on commit ab16c04

Please sign in to comment.