Skip to content
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
23 changes: 18 additions & 5 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,41 @@ pub(crate) struct Analyzer<'run, 'src> {

impl<'run, 'src> Analyzer<'run, 'src> {
pub(crate) fn analyze(
asts: &'run HashMap<PathBuf, Ast<'src>>,
asts: &'run HashMap<(Modulepath, PathBuf), Ast<'src>>,
config: &Config,
doc: Option<String>,
groups: &[StringLiteral<'src>],
loaded: &[PathBuf],
module_path: &Modulepath,
name: Option<Name<'src>>,
overrides: &mut HashMap<Number, String>,
paths: &HashMap<PathBuf, PathBuf>,
private: bool,
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
Self::default().justfile(
asts, config, doc, groups, loaded, name, overrides, paths, private, root,
asts,
config,
doc,
groups,
loaded,
module_path,
name,
overrides,
paths,
private,
root,
)
}

fn justfile(
mut self,
asts: &'run HashMap<PathBuf, Ast<'src>>,
asts: &'run HashMap<(Modulepath, PathBuf), Ast<'src>>,
config: &Config,
doc: Option<String>,
groups: &[StringLiteral<'src>],
loaded: &[PathBuf],
module_path: &Modulepath,
name: Option<Name<'src>>,
overrides: &mut HashMap<Number, String>,
paths: &HashMap<PathBuf, PathBuf>,
Expand All @@ -51,7 +63,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
let mut unstable_features = BTreeSet::new();

let mut stack = Vec::new();
let ast = asts.get(root).unwrap();
let ast = asts.get(&(module_path.clone(), root.to_owned())).unwrap();
stack.push(ast);

while let Some(ast) = stack.pop() {
Expand Down Expand Up @@ -80,7 +92,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
if let Some(absolute) = absolute
&& imports.insert(absolute)
{
stack.push(asts.get(absolute).unwrap());
stack.push(asts.get(&(module_path.clone(), absolute.clone())).unwrap());
}
}
Item::Module {
Expand All @@ -99,6 +111,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
doc.clone(),
&attributes.groups(),
loaded,
&module_path.join(name.lexeme()),
Some(*name),
overrides,
paths,
Expand Down
7 changes: 5 additions & 2 deletions src/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ use super::*;

#[derive(Debug)]
pub(crate) struct Compilation<'src> {
pub(crate) asts: HashMap<PathBuf, Ast<'src>>,
pub(crate) asts: HashMap<(Modulepath, PathBuf), Ast<'src>>,
pub(crate) justfile: Justfile<'src>,
pub(crate) overrides: HashMap<Number, String>,
pub(crate) root: PathBuf,
}

impl<'src> Compilation<'src> {
pub(crate) fn root_ast(&self) -> &Ast<'src> {
self.asts.get(&self.root).unwrap()
self
.asts
.get(&(Modulepath::default(), self.root.clone()))
.unwrap()
}
}
32 changes: 24 additions & 8 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,37 @@ impl Compiler {
loader: &'src Loader,
root: &Path,
) -> RunResult<'src, Compilation<'src>> {
let mut asts = HashMap::<PathBuf, Ast>::new();
let mut asts = HashMap::<(Modulepath, PathBuf), Ast>::new();
let mut loaded = Vec::new();
let mut numerator = Numerator::new();
let mut paths = HashMap::<PathBuf, PathBuf>::new();
let mut stack = Vec::new();
stack.push(Source::root(root));

while let Some(current) = stack.pop() {
if paths.contains_key(&current.path) {
let key = (
current
.namepath
.as_ref()
.map(Modulepath::from)
.unwrap_or_default(),
current.path.clone(),
);

if asts.contains_key(&key) {
continue;
}

let (relative, src) = loader.load(config, root, &current.path)?;
loaded.push(relative.into());
let mut ast = Parser::parse_source(&mut numerator, relative, &current, src)?;

paths.insert(current.path.clone(), relative.into());
if paths
.insert(current.path.clone(), relative.into())
.is_none()
{
loaded.push(relative.into());
}

let mut ast = Parser::parse_source(&mut numerator, relative, &current, src)?;

for item in &mut ast.items {
if !item.is_enabled() {
Expand Down Expand Up @@ -93,7 +107,7 @@ impl Compiler {
}
}

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

let mut overrides = HashMap::new();
Expand All @@ -104,6 +118,7 @@ impl Compiler {
None,
&[],
&loaded,
&Modulepath::default(),
None,
&mut overrides,
&paths,
Expand Down Expand Up @@ -247,8 +262,8 @@ impl Compiler {
let tokens = Lexer::test_lex(src)?;
let ast = Parser::parse_tokens(&mut Numerator::new(), &tokens)?;
let root = PathBuf::from("justfile");
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
asts.insert(root.clone(), ast);
let mut asts: HashMap<(Modulepath, PathBuf), Ast> = HashMap::new();
asts.insert((Modulepath::default(), root.clone()), ast);
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert(root.clone(), root.clone());
Analyzer::analyze(
Expand All @@ -257,6 +272,7 @@ impl Compiler {
None,
&[],
&[],
&Modulepath::default(),
None,
&mut HashMap::new(),
&paths,
Expand Down
2 changes: 1 addition & 1 deletion src/modulepath.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::*;

#[derive(Debug, Default, Eq, Ord, PartialEq, PartialOrd, Clone)]
#[derive(Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Clone)]
pub(crate) struct Modulepath {
pub(crate) components: Vec<String>,
pub(crate) spaced: bool,
Expand Down
5 changes: 3 additions & 2 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ pub(crate) fn analysis_error(
.expect("Parsing failed in analysis test...");

let root = PathBuf::from("justfile");
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
asts.insert(root.clone(), ast);
let mut asts: HashMap<(Modulepath, PathBuf), Ast> = HashMap::new();
asts.insert((Modulepath::default(), root.clone()), ast);

let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
paths.insert("justfile".into(), "justfile".into());
Expand All @@ -74,6 +74,7 @@ pub(crate) fn analysis_error(
None,
&[],
&[],
&Modulepath::default(),
None,
&mut HashMap::new(),
&paths,
Expand Down
90 changes: 90 additions & 0 deletions tests/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,93 @@ fn submodule_override_not_evaluated() {
.stdout("b\n")
.success();
}

#[test]
fn overrides_of_modules_sharing_a_source_file() {
Test::new()
.write(
"shared.just",
"
x := 'foo'
show:
@echo x={{x}}
",
)
.justfile(
"
mod a 'shared.just'

mod b 'shared.just'
",
)
.args(["--set", "a::x", "bar", "a::show", "b::show"])
.stdout(
"
x=bar
x=foo
",
)
.success();

Test::new()
.write(
"shared.just",
"
x := 'foo'
show:
@echo x={{x}}
",
)
.justfile(
"
mod a 'shared.just'

mod b 'shared.just'
",
)
.args(["--set", "b::x", "bar", "a::show", "b::show"])
.stdout(
"
x=foo
x=bar
",
)
.success();
}

#[test]
fn overrides_of_import_shared_across_modules() {
Test::new()
.write("common.just", "x := 'foo'")
.write(
"a.just",
"
import 'common.just'
show:
@echo x={{x}}
",
)
.write(
"b.just",
"
import 'common.just'
show:
@echo x={{x}}
",
)
.justfile(
"
mod a 'a.just'

mod b 'b.just'
",
)
.args(["--set", "a::x", "bar", "a::show", "b::show"])
.stdout(
"
x=bar
x=foo
",
)
.success();
}
Loading