Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into note-functions-work…
Browse files Browse the repository at this point in the history
…-in-var-assignments-too
  • Loading branch information
casey committed Oct 30, 2024
2 parents ae35b31 + a71f2a5 commit b593c7c
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 51 deletions.
36 changes: 33 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ most Windows users.)
</tr>
<tr>
<td><a href=https://nixos.org/nix/>Nix</a></td>
<td><a href=https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/just/default.nix>just</a></td>
<td><a href=https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ju/just/package.nix>just</a></td>
<td><code>nix-env -iA nixpkgs.just</code></td>
</tr>
<tr>
Expand Down Expand Up @@ -268,7 +268,7 @@ most Windows users.)
<tr>
<td><a href=https://nixos.org/nixos/>NixOS</a></td>
<td><a href=https://nixos.org/nix/>Nix</a></td>
<td><a href=https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/just/default.nix>just</a></td>
<td><a href=https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ju/just/package.nix>just</a></td>
<td><code>nix-env -iA nixos.just</code></td>
</tr>
<tr>
Expand Down Expand Up @@ -2059,6 +2059,10 @@ See the [Strings](#strings) section for details on unindenting.
Backticks may not start with `#!`. This syntax is reserved for a future
upgrade.

The [`shell(…)` function](#external-commands) provides a more general mechanism
to invoke external commands, including the ability to execute the contents of a
variable as a command, and to pass arguments to a command.

### Conditional Expressions

`if`/`else` expressions evaluate different branches depending on if two
Expand Down Expand Up @@ -3319,7 +3323,33 @@ Imports may be made optional by putting a `?` after the `import` keyword:
import? 'foo/bar.just'
```

Missing source files for optional imports do not produce an error.
Importing the same source file multiple times is not an error<sup>master</sup>.
This allows importing multiple justfiles, for example `foo.just` and
`bar.just`, which both import a third justfile containing shared recipes, for
example `baz.just`, without the duplicate import of `baz.just` being an error:

```mf
# justfile
import 'foo.just'
import 'bar.just'
```

```mf
# foo.just
import 'baz.just'
foo: baz
```

```mf
# bar.just
import 'baz.just'
bar: baz
```

```just
# baz
baz:
```

### Modules<sup>1.19.0</sup>

Expand Down
4 changes: 2 additions & 2 deletions README.中文.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ list:
<tr>
<td><a href="https://nixos.org/download.html#download-nix">Various</a></td>
<td><a href="https://nixos.org/nix/">Nix</a></td>
<td><a href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/just/default.nix">just</a></td>
<td><a href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ju/just/package.nix">just</a></td>
<td><code>nix-env -iA nixpkgs.just</code></td>
</tr>
<tr>
<td><a href="https://nixos.org/nixos/">NixOS</a></td>
<td><a href="https://nixos.org/nix/">Nix</a></td>
<td><a href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/development/tools/just/default.nix">just</a></td>
<td><a href="https://github.com/NixOS/nixpkgs/blob/master/pkgs/by-name/ju/just/package.nix">just</a></td>
<td><code>nix-env -iA nixos.just</code></td>
</tr>
<tr>
Expand Down
5 changes: 4 additions & 1 deletion src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl<'run, 'src> Analyzer<'run, 'src> {
root: &Path,
) -> CompileResult<'src, Justfile<'src>> {
let mut definitions = HashMap::new();
let mut imports = HashSet::new();

let mut stack = Vec::new();
let ast = asts.get(root).unwrap();
Expand All @@ -54,7 +55,9 @@ impl<'run, 'src> Analyzer<'run, 'src> {
Item::Comment(_) => (),
Item::Import { absolute, .. } => {
if let Some(absolute) = absolute {
stack.push(asts.get(absolute).unwrap());
if imports.insert(absolute) {
stack.push(asts.get(absolute).unwrap());
}
}
}
Item::Module {
Expand Down
10 changes: 1 addition & 9 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ impl Compiler {
let tokens = Lexer::lex(relative, src)?;
let mut ast = Parser::parse(
current.file_depth,
&current.path,
&current.import_offsets,
&current.namepath,
&tokens,
Expand Down Expand Up @@ -214,14 +213,7 @@ impl Compiler {
#[cfg(test)]
pub(crate) fn test_compile(src: &str) -> CompileResult<Justfile> {
let tokens = Lexer::test_lex(src)?;
let ast = Parser::parse(
0,
&PathBuf::new(),
&[],
&Namepath::default(),
&tokens,
&PathBuf::new(),
)?;
let ast = Parser::parse(0, &[], &Namepath::default(), &tokens, &PathBuf::new())?;
let root = PathBuf::from("justfile");
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
asts.insert(root.clone(), ast);
Expand Down
24 changes: 3 additions & 21 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use {super::*, TokenKind::*};
pub(crate) struct Parser<'run, 'src> {
expected_tokens: BTreeSet<TokenKind>,
file_depth: u32,
file_path: &'run Path,
import_offsets: Vec<usize>,
module_namepath: &'run Namepath<'src>,
next_token: usize,
Expand All @@ -39,7 +38,6 @@ impl<'run, 'src> Parser<'run, 'src> {
/// Parse `tokens` into an `Ast`
pub(crate) fn parse(
file_depth: u32,
file_path: &'run Path,
import_offsets: &[usize],
module_namepath: &'run Namepath<'src>,
tokens: &'run [Token<'src>],
Expand All @@ -48,7 +46,6 @@ impl<'run, 'src> Parser<'run, 'src> {
Self {
expected_tokens: BTreeSet::new(),
file_depth,
file_path,
import_offsets: import_offsets.to_vec(),
module_namepath,
next_token: 0,
Expand Down Expand Up @@ -910,7 +907,6 @@ impl<'run, 'src> Parser<'run, 'src> {
dependencies,
doc,
file_depth: self.file_depth,
file_path: self.file_path.into(),
import_offsets: self.import_offsets.clone(),
name,
namepath: self.module_namepath.join(name),
Expand Down Expand Up @@ -1162,15 +1158,8 @@ mod tests {
fn test(text: &str, want: Tree) {
let unindented = unindent(text);
let tokens = Lexer::test_lex(&unindented).expect("lexing failed");
let justfile = Parser::parse(
0,
&PathBuf::new(),
&[],
&Namepath::default(),
&tokens,
&PathBuf::new(),
)
.expect("parsing failed");
let justfile = Parser::parse(0, &[], &Namepath::default(), &tokens, &PathBuf::new())
.expect("parsing failed");
let have = justfile.tree();
if have != want {
println!("parsed text: {unindented}");
Expand Down Expand Up @@ -1208,14 +1197,7 @@ mod tests {
) {
let tokens = Lexer::test_lex(src).expect("Lexing failed in parse test...");

match Parser::parse(
0,
&PathBuf::new(),
&[],
&Namepath::default(),
&tokens,
&PathBuf::new(),
) {
match Parser::parse(0, &[], &Namepath::default(), &tokens, &PathBuf::new()) {
Ok(_) => panic!("Parsing unexpectedly succeeded"),
Err(have) => {
let want = CompileError {
Expand Down
2 changes: 0 additions & 2 deletions src/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub(crate) struct Recipe<'src, D = Dependency<'src>> {
#[serde(skip)]
pub(crate) file_depth: u32,
#[serde(skip)]
pub(crate) file_path: PathBuf,
#[serde(skip)]
pub(crate) import_offsets: Vec<usize>,
pub(crate) name: Name<'src>,
pub(crate) namepath: Namepath<'src>,
Expand Down
11 changes: 2 additions & 9 deletions src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,8 @@ pub(crate) fn analysis_error(
) {
let tokens = Lexer::test_lex(src).expect("Lexing failed in parse test...");

let ast = Parser::parse(
0,
&PathBuf::new(),
&[],
&Namepath::default(),
&tokens,
&PathBuf::new(),
)
.expect("Parsing failed in analysis test...");
let ast = Parser::parse(0, &[], &Namepath::default(), &tokens, &PathBuf::new())
.expect("Parsing failed in analysis test...");

let root = PathBuf::from("justfile");
let mut asts: HashMap<PathBuf, Ast> = HashMap::new();
Expand Down
1 change: 0 additions & 1 deletion src/unresolved_recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ impl<'src> UnresolvedRecipe<'src> {
dependencies,
doc: self.doc,
file_depth: self.file_depth,
file_path: self.file_path,
import_offsets: self.import_offsets,
name: self.name,
namepath: self.namepath,
Expand Down
48 changes: 48 additions & 0 deletions tests/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,51 @@ fn reused_import_are_allowed() {
})
.run();
}

#[test]
fn multiply_imported_items_do_not_conflict() {
Test::new()
.justfile(
"
import 'a.just'
import 'a.just'
foo: bar
",
)
.write(
"a.just",
"
x := 'y'
@bar:
echo hello
",
)
.stdout("hello\n")
.run();
}

#[test]
fn nested_multiply_imported_items_do_not_conflict() {
Test::new()
.justfile(
"
import 'a.just'
import 'b.just'
foo: bar
",
)
.write("a.just", "import 'c.just'")
.write("b.just", "import 'c.just'")
.write(
"c.just",
"
x := 'y'
@bar:
echo hello
",
)
.stdout("hello\n")
.run();
}
17 changes: 14 additions & 3 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use {super::*, pretty_assertions::assert_eq};
use {
super::*,
pretty_assertions::{assert_eq, StrComparison},
};

macro_rules! test {
{
Expand Down Expand Up @@ -205,6 +208,14 @@ impl Test {
equal
}

fn compare_string(name: &str, have: &str, want: &str) -> bool {
let equal = have == want;
if !equal {
eprintln!("Bad {name}: {}", StrComparison::new(&have, &want));
}
equal
}

if let Some(justfile) = &self.justfile {
let justfile = unindent(justfile);
fs::write(self.justfile_path(), justfile).unwrap();
Expand Down Expand Up @@ -266,8 +277,8 @@ impl Test {
}

if !compare("status", output.status.code(), Some(self.status))
| (self.stdout_regex.is_none() && !compare("stdout", output_stdout, &stdout))
| (self.stderr_regex.is_none() && !compare("stderr", output_stderr, &stderr))
| (self.stdout_regex.is_none() && !compare_string("stdout", output_stdout, &stdout))
| (self.stderr_regex.is_none() && !compare_string("stderr", output_stderr, &stderr))
{
panic!("Output mismatch.");
}
Expand Down

0 comments on commit b593c7c

Please sign in to comment.