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

(WIP) Use ariadne #2426

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [".", "crates/*"]

[dependencies]
ansi_term = "0.12.0"
ariadne = "0.4.1"
blake3 = { version = "1.5.0", features = ["rayon", "mmap"] }
camino = "1.0.4"
chrono = "0.4.38"
Expand Down
2 changes: 1 addition & 1 deletion justfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export JUST_LOG := log
watch +args='test':
cargo watch --clear --exec '{{ args }}'

[group: 'test']
[group('test')]
test:
cargo test --all

Expand Down
56 changes: 56 additions & 0 deletions src/compile_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,62 @@ impl<'src> CompileError<'src> {
}
}

pub(crate) fn render_compile_error(error: &CompileError, color: Color) {
use ariadne::{Config, Label, Report, ReportKind, Source};

let token = error.token;
let source = Source::from(token.src);

let start = token.offset;
let end = token.offset + token.length;

let path = format!("{}", token.path.display());
let label = Label::new((&path, start..end));

let config = Config::default().with_color(color.stderr().active());
let report = Report::build(ReportKind::Error, &path, start).with_config(config);

let report = match &*error.kind {
CompileErrorKind::AttributeArgumentCountMismatch {
attribute,
found,
min,
max,
} => {
let label_msg = format!("Found {found} {}", Count("argument", *found));

let note = if min == max {
format!("`{attribute}` takes {min} {}", Count("argument", *min))
} else {
format!("`{attribute}` takes between {min} and {max} arguments")
};

report
.with_message("Attribute argument count mismatch")
.with_label(label.with_message(label_msg))
.with_note(note)
.finish()
}
CompileErrorKind::DuplicateAttribute { attribute, first } => {
let original_label = source
.line(*first)
.map(|line| Label::new((&path, line.span())).with_message("original"));

let mut report = report.with_message(format!("Duplicate attribute `{attribute}`"));
if let Some(original) = original_label {
report = report.with_label(original);
}
report.with_label(label.with_message("duplicate")).finish()
}
_ => {
let message = format!("{error}");
report.with_message(message).with_label(label).finish()
}
};

report.eprint((&path, source)).unwrap();
}

fn capitalize(s: &str) -> String {
let mut chars = s.chars();
match chars.next() {
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,13 @@ impl ColorDisplay for Error<'_> {
}
}

pub(crate) fn render_error(error: &Error, color: Color) {
match error {
Error::Compile { compile_error } => compile_error::render_compile_error(compile_error, color),
_ => eprintln!("{}", error.color_display(color.stderr())),
}
}

fn format_cmd(binary: &OsString, arguments: &Vec<OsString>) -> String {
iter::once(binary)
.chain(arguments)
Expand Down
2 changes: 1 addition & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn run(args: impl Iterator<Item = impl Into<OsString> + Clone>) -> Result<()
})
.map_err(|error| {
if !verbosity.quiet() && error.print_message() {
eprintln!("{}", error.color_display(color.stderr()));
crate::error::render_error(&error, color);
}
error.code().unwrap_or(EXIT_FAILURE)
})
Expand Down
64 changes: 32 additions & 32 deletions tests/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ test! {
args: ("a"),
stdout: "",
stderr: "
error: Unterminated string
——▶ justfile:1:6
1 │ a b= ':
│ ^
",
Error: Unterminated string
╭─[justfile:1:6]
1 │ a b= ':
───╯
",
status: EXIT_FAILURE,
}

Expand All @@ -201,12 +201,12 @@ test! {
args: ("a"),
stdout: "",
stderr: r#"
error: Unterminated string
——▶ justfile:1:6
1 │ a b= ":
│ ^
"#,
Error: Unterminated string
╭─[justfile:1:6]
1 │ a b= ":
───╯
"#,
status: EXIT_FAILURE,
}

Expand Down Expand Up @@ -234,11 +234,11 @@ test! {
args: ("a"),
stdout: "",
stderr: "
error: Unterminated string
——▶ justfile:1:6
1 │ a b= ''':
│ ^^^
Error: Unterminated string
╭─[justfile:1:6]
1 │ a b= ''':
───╯
",
status: EXIT_FAILURE,
}
Expand All @@ -251,11 +251,11 @@ test! {
args: ("a"),
stdout: "",
stderr: r#"
error: Unterminated string
——▶ justfile:1:6
1 │ a b= """:
│ ^^^
Error: Unterminated string
╭─[justfile:1:6]
1 │ a b= """:
───╯
"#,
status: EXIT_FAILURE,
}
Expand Down Expand Up @@ -481,11 +481,11 @@ fn unicode_escape_non_hex() {
.status(1)
.stderr(
r#"
error: expected hex digit [0-9A-Fa-f] but found `o`
——▶ justfile:1:6
1 │ x := "\u{foo}"
│ ^^^^^^^^^
Error: expected hex digit [0-9A-Fa-f] but found `o`
╭─[justfile:1:6]
1 │ x := "\u{foo}"
───╯
"#,
)
.run();
Expand Down Expand Up @@ -517,11 +517,11 @@ fn unicode_escape_too_long() {
.status(1)
.stderr(
r#"
error: unicode escape sequence starting with `\u{FFFFFFF` longer than six hex digits
——▶ justfile:1:6
1 │ x := "\u{FFFFFFFFFF}"
│ ^^^^^^^^^^^^^^^^
Error: unicode escape sequence starting with `\u{FFFFFFF` longer than six hex digits
╭─[justfile:1:6]
1 │ x := "\u{FFFFFFFFFF}"
───╯
"#,
)
.run();
Expand Down
14 changes: 7 additions & 7 deletions tests/subsequents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ test! {
justfile: "
foo: && foo
",
stderr: "
error: Recipe `foo` depends on itself
——▶ justfile:1:9
1 │ foo: && foo
│ ^^^
",
stderr:
"Error: Recipe `foo` depends on itself
╭─[justfile:1:9]
1 │ foo: && foo
───╯
",
status: EXIT_FAILURE,
}

Expand Down
3 changes: 2 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ impl Test {
fn compare_string(name: &str, have: &str, want: &str) -> bool {
let equal = have == want;
if !equal {
eprintln!("Bad {name}: {}", StrComparison::new(&have, &want));
//eprintln!("Bad {name}: {}", StrComparison::new(&have, &want));
eprintln!("Bad {name}:\n{}||\n-------------\n{}||\n=========", &have, &want);
}
equal
}
Expand Down
12 changes: 6 additions & 6 deletions tests/working_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,12 @@ fn attribute_with_nocd_is_forbidden() {
)
.stderr(
"
error: Recipe `bar` has both `[no-cd]` and `[working-directory]` attributes
——▶ justfile:3:1
3 │ bar:
│ ^^^
",
Error: Recipe `bar` has both `[no-cd]` and `[working-directory]` attributes
╭─[justfile:3:1]
3 │ bar:
───╯
"
)
.status(EXIT_FAILURE)
.run();
Expand Down
Loading