From 92bae080aba135c2fc969e53c24b9aeaf98ffe9e Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Fri, 24 Nov 2023 23:15:41 -0800 Subject: [PATCH] List included recipes in load order (#1745) --- src/analyzer.rs | 5 ++++- src/compiler.rs | 6 ++++-- src/justfile.rs | 13 ++++++++++++- src/lib.rs | 1 + src/testing.rs | 2 +- tests/includes.rs | 22 ++++++++++++++++++++++ 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/analyzer.rs b/src/analyzer.rs index e4cef06..b5ae872 100644 --- a/src/analyzer.rs +++ b/src/analyzer.rs @@ -9,15 +9,17 @@ pub(crate) struct Analyzer<'src> { impl<'src> Analyzer<'src> { pub(crate) fn analyze( + loaded: Vec, paths: &HashMap, asts: &HashMap>, root: &Path, ) -> CompileResult<'src, Justfile<'src>> { - Analyzer::default().justfile(paths, asts, root) + Analyzer::default().justfile(loaded, paths, asts, root) } fn justfile( mut self, + loaded: Vec, paths: &HashMap, asts: &HashMap>, root: &Path, @@ -101,6 +103,7 @@ impl<'src> Analyzer<'src> { }), aliases, assignments: self.assignments, + loaded, recipes, settings, warnings, diff --git a/src/compiler.rs b/src/compiler.rs index 070748e..59d981e 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -11,12 +11,14 @@ impl Compiler { let mut asts: HashMap = HashMap::new(); let mut paths: HashMap = HashMap::new(); let mut srcs: HashMap = HashMap::new(); + let mut loaded = Vec::new(); let mut stack: Vec = Vec::new(); stack.push(root.into()); while let Some(current) = stack.pop() { let (relative, src) = loader.load(root, ¤t)?; + loaded.push(relative.into()); let tokens = Lexer::lex(relative, src)?; let mut ast = Parser::parse(&tokens)?; @@ -42,7 +44,7 @@ impl Compiler { asts.insert(current.clone(), ast.clone()); } - let justfile = Analyzer::analyze(&paths, &asts, root)?; + let justfile = Analyzer::analyze(loaded, &paths, &asts, root)?; Ok(Compilation { asts, @@ -61,7 +63,7 @@ impl Compiler { asts.insert(root.clone(), ast); let mut paths: HashMap = HashMap::new(); paths.insert(root.clone(), root.clone()); - Analyzer::analyze(&paths, &asts, &root) + Analyzer::analyze(Vec::new(), &paths, &asts, &root) } } diff --git a/src/justfile.rs b/src/justfile.rs index b08d431..45b327d 100644 --- a/src/justfile.rs +++ b/src/justfile.rs @@ -6,6 +6,8 @@ pub(crate) struct Justfile<'src> { pub(crate) assignments: Table<'src, Assignment<'src>>, #[serde(rename = "first", serialize_with = "keyed::serialize_option")] pub(crate) default: Option>>, + #[serde(skip)] + pub(crate) loaded: Vec, pub(crate) recipes: Table<'src, Rc>>, pub(crate) settings: Settings<'src>, pub(crate) warnings: Vec, @@ -365,7 +367,16 @@ impl<'src> Justfile<'src> { .collect::>>(); if source_order { - recipes.sort_by_key(|recipe| recipe.name.offset); + recipes.sort_by_key(|recipe| { + ( + self + .loaded + .iter() + .position(|path| path == recipe.name.path) + .unwrap(), + recipe.name.offset, + ) + }); } recipes diff --git a/src/lib.rs b/src/lib.rs index bd4b833..fa88ede 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ clippy::enum_glob_use, clippy::let_underscore_untyped, clippy::needless_pass_by_value, + clippy::similar_names, clippy::too_many_lines, clippy::unnecessary_wraps, clippy::wildcard_imports diff --git a/src/testing.rs b/src/testing.rs index b0f6c4b..5d1cf74 100644 --- a/src/testing.rs +++ b/src/testing.rs @@ -68,7 +68,7 @@ pub(crate) fn analysis_error( let mut paths: HashMap = HashMap::new(); paths.insert("justfile".into(), "justfile".into()); - match Analyzer::analyze(&paths, &asts, &root) { + match Analyzer::analyze(Vec::new(), &paths, &asts, &root) { Ok(_) => panic!("Analysis unexpectedly succeeded"), Err(have) => { let want = CompileError { diff --git a/tests/includes.rs b/tests/includes.rs index af2e7b6..0bc615c 100644 --- a/tests/includes.rs +++ b/tests/includes.rs @@ -120,3 +120,25 @@ fn include_recipes_are_not_default() { .stderr("error: Justfile contains no default recipe.\n") .run(); } + +#[test] +fn listed_recipes_in_includes_are_in_load_order() { + Test::new() + .justfile( + " + !include ./include.justfile + foo: + ", + ) + .write("include.justfile", "bar:") + .args(["--list", "--unstable", "--unsorted"]) + .test_round_trip(false) + .stdout( + " + Available recipes: + foo + bar + ", + ) + .run(); +}