2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2019-11-07 10:55:15 -08:00
|
|
|
|
|
|
|
pub(crate) struct Compiler;
|
|
|
|
|
|
|
|
impl Compiler {
|
2023-11-21 11:28:59 -08:00
|
|
|
pub(crate) fn compile<'src>(
|
|
|
|
loader: &'src Loader,
|
|
|
|
root: &Path,
|
|
|
|
) -> RunResult<'src, Compilation<'src>> {
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut asts = HashMap::<PathBuf, Ast>::new();
|
2024-07-07 20:45:03 -07:00
|
|
|
let mut loaded = Vec::new();
|
2024-05-14 20:07:41 -07:00
|
|
|
let mut paths = HashMap::<PathBuf, PathBuf>::new();
|
|
|
|
let mut srcs = HashMap::<PathBuf, &str>::new();
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
let mut stack = Vec::new();
|
|
|
|
stack.push(Source::root(root));
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
while let Some(current) = stack.pop() {
|
|
|
|
let (relative, src) = loader.load(root, ¤t.path)?;
|
2023-11-24 23:15:41 -08:00
|
|
|
loaded.push(relative.into());
|
2023-11-21 20:17:38 -08:00
|
|
|
let tokens = Lexer::lex(relative, src)?;
|
2024-01-11 19:00:38 -08:00
|
|
|
let mut ast = Parser::parse(
|
2024-05-18 23:38:57 -07:00
|
|
|
current.file_depth,
|
2024-01-11 19:00:38 -08:00
|
|
|
¤t.path,
|
2024-05-25 18:10:06 -07:00
|
|
|
¤t.import_offsets,
|
2024-01-11 19:00:38 -08:00
|
|
|
¤t.namepath,
|
2024-05-18 23:38:57 -07:00
|
|
|
current.submodule_depth,
|
2024-01-11 19:00:38 -08:00
|
|
|
&tokens,
|
|
|
|
¤t.working_directory,
|
|
|
|
)?;
|
2023-11-21 11:28:59 -08:00
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
paths.insert(current.path.clone(), relative.into());
|
|
|
|
srcs.insert(current.path.clone(), src);
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
for item in &mut ast.items {
|
2023-12-27 20:27:15 -08:00
|
|
|
match item {
|
2023-12-29 12:16:31 -08:00
|
|
|
Item::Module {
|
2023-12-28 04:23:58 -08:00
|
|
|
absolute,
|
2023-12-29 12:16:31 -08:00
|
|
|
name,
|
|
|
|
optional,
|
|
|
|
relative,
|
2024-06-28 21:13:11 -07:00
|
|
|
..
|
2023-12-28 04:23:58 -08:00
|
|
|
} => {
|
2024-01-08 13:26:33 -08:00
|
|
|
let parent = current.path.parent().unwrap();
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
let relative = relative
|
|
|
|
.as_ref()
|
|
|
|
.map(|relative| Self::expand_tilde(&relative.cooked))
|
|
|
|
.transpose()?;
|
2023-12-29 12:16:31 -08:00
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
let import = Self::find_module_file(parent, *name, relative.as_deref())?;
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2023-12-29 12:16:31 -08:00
|
|
|
if let Some(import) = import {
|
2024-05-20 01:04:03 -07:00
|
|
|
if current.file_path.contains(&import) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(Error::CircularImport {
|
|
|
|
current: current.path,
|
|
|
|
import,
|
|
|
|
});
|
2023-12-29 12:16:31 -08:00
|
|
|
}
|
|
|
|
*absolute = Some(import.clone());
|
2024-01-08 13:26:33 -08:00
|
|
|
stack.push(current.module(*name, import));
|
2023-12-29 12:16:31 -08:00
|
|
|
} else if !*optional {
|
|
|
|
return Err(Error::MissingModuleFile { module: *name });
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
2023-12-29 12:16:31 -08:00
|
|
|
Item::Import {
|
|
|
|
relative,
|
|
|
|
absolute,
|
|
|
|
optional,
|
|
|
|
path,
|
|
|
|
} => {
|
2023-12-28 18:08:02 -08:00
|
|
|
let import = current
|
2024-01-08 13:26:33 -08:00
|
|
|
.path
|
2023-12-28 18:08:02 -08:00
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join(Self::expand_tilde(&relative.cooked)?)
|
|
|
|
.lexiclean();
|
2023-12-29 12:16:31 -08:00
|
|
|
|
|
|
|
if import.is_file() {
|
2024-05-20 01:04:03 -07:00
|
|
|
if current.file_path.contains(&import) {
|
2024-01-08 13:26:33 -08:00
|
|
|
return Err(Error::CircularImport {
|
|
|
|
current: current.path,
|
|
|
|
import,
|
|
|
|
});
|
2023-12-29 12:16:31 -08:00
|
|
|
}
|
|
|
|
*absolute = Some(import.clone());
|
2024-05-25 18:10:06 -07:00
|
|
|
stack.push(current.import(import, path.offset));
|
2023-12-29 12:16:31 -08:00
|
|
|
} else if !*optional {
|
|
|
|
return Err(Error::MissingImportFile { path: *path });
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-08 13:26:33 -08:00
|
|
|
asts.insert(current.path, ast.clone());
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
|
2024-06-28 21:13:11 -07:00
|
|
|
let justfile = Analyzer::analyze(&asts, None, &loaded, None, &paths, root)?;
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
Ok(Compilation {
|
|
|
|
asts,
|
|
|
|
justfile,
|
|
|
|
root: root.into(),
|
2024-07-07 20:45:03 -07:00
|
|
|
srcs,
|
2023-11-21 11:28:59 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
fn find_module_file<'src>(
|
|
|
|
parent: &Path,
|
|
|
|
module: Name<'src>,
|
|
|
|
path: Option<&Path>,
|
|
|
|
) -> RunResult<'src, Option<PathBuf>> {
|
|
|
|
let mut candidates = Vec::new();
|
|
|
|
|
|
|
|
if let Some(path) = path {
|
|
|
|
let full = parent.join(path);
|
|
|
|
|
|
|
|
if full.is_file() {
|
|
|
|
return Ok(Some(full));
|
|
|
|
}
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
candidates.push((path.join("mod.just"), true));
|
2023-12-27 20:27:15 -08:00
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
for name in search::JUSTFILE_NAMES {
|
|
|
|
candidates.push((path.join(name), false));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
candidates.push((format!("{module}.just").into(), true));
|
|
|
|
candidates.push((format!("{module}/mod.just").into(), true));
|
|
|
|
|
|
|
|
for name in search::JUSTFILE_NAMES {
|
|
|
|
candidates.push((format!("{module}/{name}").into(), false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut grouped = BTreeMap::<PathBuf, Vec<(PathBuf, bool)>>::new();
|
|
|
|
|
|
|
|
for (candidate, case_sensitive) in candidates {
|
|
|
|
let candidate = parent.join(candidate).lexiclean();
|
|
|
|
grouped
|
|
|
|
.entry(candidate.parent().unwrap().into())
|
|
|
|
.or_default()
|
|
|
|
.push((candidate, case_sensitive));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut found = Vec::new();
|
|
|
|
|
|
|
|
for (directory, candidates) in grouped {
|
|
|
|
let entries = match fs::read_dir(&directory) {
|
|
|
|
Ok(entries) => entries,
|
|
|
|
Err(io_error) => {
|
|
|
|
if io_error.kind() == io::ErrorKind::NotFound {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Err(
|
|
|
|
SearchError::Io {
|
|
|
|
io_error,
|
|
|
|
directory,
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
2023-12-27 20:27:15 -08:00
|
|
|
|
|
|
|
for entry in entries {
|
|
|
|
let entry = entry.map_err(|io_error| SearchError::Io {
|
|
|
|
io_error,
|
|
|
|
directory: directory.clone(),
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if let Some(name) = entry.file_name().to_str() {
|
2024-07-08 15:38:25 -07:00
|
|
|
for (candidate, case_sensitive) in &candidates {
|
|
|
|
let candidate_name = candidate.file_name().unwrap().to_str().unwrap();
|
|
|
|
|
|
|
|
let eq = if *case_sensitive {
|
|
|
|
name == candidate_name
|
|
|
|
} else {
|
|
|
|
name.eq_ignore_ascii_case(candidate_name)
|
|
|
|
};
|
|
|
|
|
|
|
|
if eq {
|
|
|
|
found.push(candidate.parent().unwrap().join(name));
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-08 15:38:25 -07:00
|
|
|
if found.len() > 1 {
|
|
|
|
found.sort();
|
|
|
|
Err(Error::AmbiguousModuleFile {
|
|
|
|
found: found
|
|
|
|
.into_iter()
|
|
|
|
.map(|found| found.strip_prefix(parent).unwrap().into())
|
|
|
|
.collect(),
|
2023-12-27 20:27:15 -08:00
|
|
|
module,
|
2024-07-08 15:38:25 -07:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
Ok(found.into_iter().next())
|
2023-12-27 20:27:15 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-28 18:08:02 -08:00
|
|
|
fn expand_tilde(path: &str) -> RunResult<'static, PathBuf> {
|
|
|
|
Ok(if let Some(path) = path.strip_prefix("~/") {
|
|
|
|
dirs::home_dir()
|
|
|
|
.ok_or(Error::Homedir)?
|
|
|
|
.join(path.trim_start_matches('/'))
|
|
|
|
} else {
|
|
|
|
PathBuf::from(path)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-11-21 11:28:59 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) fn test_compile(src: &str) -> CompileResult<Justfile> {
|
2023-11-21 20:17:38 -08:00
|
|
|
let tokens = Lexer::test_lex(src)?;
|
2024-01-11 19:00:38 -08:00
|
|
|
let ast = Parser::parse(
|
2024-05-18 23:38:57 -07:00
|
|
|
0,
|
2024-01-11 19:00:38 -08:00
|
|
|
&PathBuf::new(),
|
2024-05-25 18:10:06 -07:00
|
|
|
&[],
|
2024-01-11 19:00:38 -08:00
|
|
|
&Namepath::default(),
|
|
|
|
0,
|
|
|
|
&tokens,
|
|
|
|
&PathBuf::new(),
|
|
|
|
)?;
|
2023-11-21 20:17:38 -08:00
|
|
|
let root = PathBuf::from("justfile");
|
2023-11-21 11:28:59 -08:00
|
|
|
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
|
|
|
|
asts.insert(root.clone(), ast);
|
2023-11-22 10:27:49 -08:00
|
|
|
let mut paths: HashMap<PathBuf, PathBuf> = HashMap::new();
|
|
|
|
paths.insert(root.clone(), root.clone());
|
2024-06-28 21:13:11 -07:00
|
|
|
Analyzer::analyze(&asts, None, &[], None, &paths, &root)
|
2023-11-21 11:28:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use {super::*, temptree::temptree};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn include_justfile() {
|
|
|
|
let justfile_a = r#"
|
|
|
|
# A comment at the top of the file
|
2023-12-19 20:31:51 -08:00
|
|
|
import "./justfile_b"
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
#some_recipe: recipe_b
|
|
|
|
some_recipe:
|
|
|
|
echo "some recipe"
|
|
|
|
"#;
|
|
|
|
|
2023-12-19 20:31:51 -08:00
|
|
|
let justfile_b = r#"import "./subdir/justfile_c"
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
recipe_b: recipe_c
|
|
|
|
echo "recipe b"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let justfile_c = r#"recipe_c:
|
|
|
|
echo "recipe c"
|
|
|
|
"#;
|
|
|
|
|
|
|
|
let tmp = temptree! {
|
|
|
|
justfile: justfile_a,
|
|
|
|
justfile_b: justfile_b,
|
|
|
|
subdir: {
|
|
|
|
justfile_c: justfile_c
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let loader = Loader::new();
|
|
|
|
|
|
|
|
let justfile_a_path = tmp.path().join("justfile");
|
2024-07-07 20:45:03 -07:00
|
|
|
let compilation = Compiler::compile(&loader, &justfile_a_path).unwrap();
|
2023-11-21 11:28:59 -08:00
|
|
|
|
|
|
|
assert_eq!(compilation.root_src(), justfile_a);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn recursive_includes_fail() {
|
|
|
|
let tmp = temptree! {
|
2024-05-20 01:04:03 -07:00
|
|
|
justfile: "import './subdir/b'\na: b",
|
|
|
|
subdir: {
|
|
|
|
b: "import '../justfile'\nb:"
|
|
|
|
}
|
2023-11-21 11:28:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let loader = Loader::new();
|
|
|
|
|
|
|
|
let justfile_a_path = tmp.path().join("justfile");
|
2024-07-07 20:45:03 -07:00
|
|
|
let loader_output = Compiler::compile(&loader, &justfile_a_path).unwrap_err();
|
2019-11-07 10:55:15 -08:00
|
|
|
|
2023-12-19 20:31:51 -08:00
|
|
|
assert_matches!(loader_output, Error::CircularImport { current, import }
|
2024-05-20 01:04:03 -07:00
|
|
|
if current == tmp.path().join("subdir").join("b").lexiclean() &&
|
|
|
|
import == tmp.path().join("justfile").lexiclean()
|
2023-11-21 11:28:59 -08:00
|
|
|
);
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|
2024-07-08 15:38:25 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn find_module_file() {
|
|
|
|
#[track_caller]
|
|
|
|
fn case(path: Option<&str>, files: &[&str], expected: Result<Option<&str>, &[&str]>) {
|
|
|
|
let module = Name {
|
|
|
|
token: Token {
|
|
|
|
column: 0,
|
|
|
|
kind: TokenKind::Identifier,
|
|
|
|
length: 3,
|
|
|
|
line: 0,
|
|
|
|
offset: 0,
|
|
|
|
path: Path::new(""),
|
|
|
|
src: "foo",
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let tempdir = tempfile::tempdir().unwrap();
|
|
|
|
|
|
|
|
for file in files {
|
|
|
|
if let Some(parent) = Path::new(file).parent() {
|
|
|
|
fs::create_dir_all(tempdir.path().join(parent)).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
fs::write(tempdir.path().join(file), "").unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
let actual = Compiler::find_module_file(tempdir.path(), module, path.map(Path::new));
|
|
|
|
|
|
|
|
match expected {
|
|
|
|
Err(expected) => match actual.unwrap_err() {
|
|
|
|
Error::AmbiguousModuleFile { found, .. } => {
|
|
|
|
assert_eq!(
|
|
|
|
found,
|
|
|
|
expected
|
|
|
|
.iter()
|
|
|
|
.map(|expected| expected.replace('/', std::path::MAIN_SEPARATOR_STR).into())
|
|
|
|
.collect::<Vec<PathBuf>>()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => panic!("unexpected error"),
|
|
|
|
},
|
|
|
|
Ok(Some(expected)) => assert_eq!(
|
|
|
|
actual.unwrap().unwrap(),
|
|
|
|
tempdir
|
|
|
|
.path()
|
|
|
|
.join(expected.replace('/', std::path::MAIN_SEPARATOR_STR))
|
|
|
|
),
|
|
|
|
Ok(None) => assert_eq!(actual.unwrap(), None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case(None, &["foo.just"], Ok(Some("foo.just")));
|
|
|
|
case(None, &["FOO.just"], Ok(None));
|
|
|
|
case(None, &["foo/mod.just"], Ok(Some("foo/mod.just")));
|
|
|
|
case(None, &["foo/MOD.just"], Ok(None));
|
|
|
|
case(None, &["foo/justfile"], Ok(Some("foo/justfile")));
|
|
|
|
case(None, &["foo/JUSTFILE"], Ok(Some("foo/JUSTFILE")));
|
|
|
|
case(None, &["foo/.justfile"], Ok(Some("foo/.justfile")));
|
|
|
|
case(None, &["foo/.JUSTFILE"], Ok(Some("foo/.JUSTFILE")));
|
|
|
|
case(
|
|
|
|
None,
|
|
|
|
&["foo/.justfile", "foo/justfile"],
|
|
|
|
Err(&["foo/.justfile", "foo/justfile"]),
|
|
|
|
);
|
|
|
|
case(None, &["foo/JUSTFILE"], Ok(Some("foo/JUSTFILE")));
|
|
|
|
|
|
|
|
case(Some("bar"), &["bar"], Ok(Some("bar")));
|
|
|
|
case(Some("bar"), &["bar/mod.just"], Ok(Some("bar/mod.just")));
|
|
|
|
case(Some("bar"), &["bar/justfile"], Ok(Some("bar/justfile")));
|
|
|
|
case(Some("bar"), &["bar/JUSTFILE"], Ok(Some("bar/JUSTFILE")));
|
|
|
|
case(Some("bar"), &["bar/.justfile"], Ok(Some("bar/.justfile")));
|
|
|
|
case(Some("bar"), &["bar/.JUSTFILE"], Ok(Some("bar/.JUSTFILE")));
|
|
|
|
|
|
|
|
case(
|
|
|
|
Some("bar"),
|
|
|
|
&["bar/justfile", "bar/mod.just"],
|
|
|
|
Err(&["bar/justfile", "bar/mod.just"]),
|
|
|
|
);
|
|
|
|
}
|
2019-11-07 10:55:15 -08:00
|
|
|
}
|