Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 20, 2024
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
31 changes: 8 additions & 23 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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();
Expand All @@ -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()
);
}
}
16 changes: 15 additions & 1 deletion src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
}
}
}
19 changes: 19 additions & 0 deletions tests/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}