Allow multiple imports of the same file in different modules (#2065)

This commit is contained in:
Casey Rodarmor 2024-05-20 01:04:03 -07:00 committed by GitHub
parent d6b2e6bad2
commit b9f63f1494
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 24 deletions

View File

@ -61,7 +61,7 @@ impl Compiler {
};
if let Some(import) = import {
if srcs.contains_key(&import) {
if current.file_path.contains(&import) {
return Err(Error::CircularImport {
current: current.path,
import,
@ -87,7 +87,7 @@ impl Compiler {
.lexiclean();
if import.is_file() {
if srcs.contains_key(&import) {
if current.file_path.contains(&import) {
return Err(Error::CircularImport {
current: current.path,
import,
@ -229,26 +229,11 @@ recipe_b: recipe_c
#[test]
fn recursive_includes_fail() {
let justfile_a = r#"
# A comment at the top of the file
import "./subdir/justfile_b"
some_recipe: recipe_b
echo "some recipe"
"#;
let justfile_b = r#"
import "../justfile"
recipe_b:
echo "recipe b"
"#;
let tmp = temptree! {
justfile: justfile_a,
subdir: {
justfile_b: justfile_b
}
justfile: "import './subdir/b'\na: b",
subdir: {
b: "import '../justfile'\nb:"
}
};
let loader = Loader::new();
@ -257,8 +242,8 @@ recipe_b:
let loader_output = Compiler::compile(false, &loader, &justfile_a_path).unwrap_err();
assert_matches!(loader_output, Error::CircularImport { current, import }
if current == tmp.path().join("subdir").join("justfile_b").lexiclean() &&
import == tmp.path().join("justfile").lexiclean()
if current == tmp.path().join("subdir").join("b").lexiclean() &&
import == tmp.path().join("justfile").lexiclean()
);
}
}

View File

@ -3,6 +3,7 @@ use super::*;
#[derive(Debug)]
pub(crate) struct Source<'src> {
pub(crate) file_depth: u32,
pub(crate) file_path: Vec<PathBuf>,
pub(crate) namepath: Namepath<'src>,
pub(crate) path: PathBuf,
pub(crate) submodule_depth: u32,
@ -13,6 +14,7 @@ impl<'src> Source<'src> {
pub(crate) fn root(path: &Path) -> Self {
Self {
file_depth: 0,
file_path: vec![path.into()],
namepath: Namepath::default(),
path: path.into(),
submodule_depth: 0,
@ -23,6 +25,12 @@ impl<'src> Source<'src> {
pub(crate) fn import(&self, path: PathBuf) -> Self {
Self {
file_depth: self.file_depth + 1,
file_path: self
.file_path
.clone()
.into_iter()
.chain(iter::once(path.clone()))
.collect(),
namepath: self.namepath.clone(),
path,
submodule_depth: self.submodule_depth,
@ -33,10 +41,16 @@ impl<'src> Source<'src> {
pub(crate) fn module(&self, name: Name<'src>, path: PathBuf) -> Self {
Self {
file_depth: self.file_depth + 1,
file_path: self
.file_path
.clone()
.into_iter()
.chain(iter::once(path.clone()))
.collect(),
namepath: self.namepath.join(name),
path: path.clone(),
submodule_depth: self.submodule_depth + 1,
working_directory: path.parent().unwrap().into(),
path,
}
}
}

View File

@ -361,3 +361,22 @@ fn recipes_imported_in_root_run_in_command_line_provided_working_directory() {
.stdout("BAZBAZ")
.run();
}
#[test]
fn reused_import_are_allowed() {
Test::new()
.justfile(
"
import 'a'
import 'b'
bar:
",
)
.tree(tree! {
a: "import 'c'",
b: "import 'c'",
c: "",
})
.run();
}