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

Unify validation error presentation in Naga CLI and WebGPU compilation messages #6436

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ By @bradwerth [#6216](https://github.com/gfx-rs/wgpu/pull/6216).
- Implemented `const_assert` in WGSL. By @sagudev in [#6198](https://github.com/gfx-rs/wgpu/pull/6198).
- Support polyfilling `inverse` in WGSL. By @chyyran in [#6385](https://github.com/gfx-rs/wgpu/pull/6385).
- Add an internal skeleton for parsing `requires`, `enable`, and `diagnostic` directives that don't yet do anything besides emit nicer errors. By @ErichDonGubler in [#6352](https://github.com/gfx-rs/wgpu/pull/6352).
- Include error chain information as a message and notes in shader compilation messages. By @ErichDonGubler in [#6436](https://github.com/gfx-rs/wgpu/pull/6436).
- Unify Naga CLI error output with the format of shader compilation messages. By @ErichDonGubler in [#6436](https://github.com/gfx-rs/wgpu/pull/6436).

#### General

Expand Down
43 changes: 11 additions & 32 deletions naga-cli/src/bin/naga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,10 @@ fn run() -> anyhow::Result<()> {
// Validation failure is not fatal. Just report the error.
if let Some(input) = &input_text {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_annotated_error(&error, filename.unwrap_or("input"), input);
error.emit_to_stderr_with_path(input, filename.unwrap_or("input"));
} else {
print_err(&error);
}
print_err(&error);
None
}
};
Expand All @@ -544,9 +545,10 @@ fn run() -> anyhow::Result<()> {
eprintln!("Error validating compacted module:");
if let Some(input) = &input_text {
let filename = input_path.file_name().and_then(std::ffi::OsStr::to_str);
emit_annotated_error(&error, filename.unwrap_or("input"), input);
error.emit_to_stderr_with_path(input, filename.unwrap_or("input"));
} else {
print_err(&error);
}
print_err(&error);
None
}
}
Expand Down Expand Up @@ -869,9 +871,10 @@ fn bulk_validate(args: Args, params: &Parameters) -> anyhow::Result<()> {
eprintln!("Error validating {input_path}:");
if let Some(input) = &input_text {
let filename = path.file_name().and_then(std::ffi::OsStr::to_str);
emit_annotated_error(&error, filename.unwrap_or("input"), input);
error.emit_to_stderr_with_path(input, filename.unwrap_or("input"));
} else {
print_err(&error);
}
print_err(&error);
}
}

Expand All @@ -892,29 +895,5 @@ fn bulk_validate(args: Args, params: &Parameters) -> anyhow::Result<()> {
Ok(())
}

use codespan_reporting::{
diagnostic::{Diagnostic, Label},
files::SimpleFile,
term::{
self,
termcolor::{ColorChoice, StandardStream},
},
};
use naga::{FastHashMap, WithSpan};

pub fn emit_annotated_error<E: Error>(ann_err: &WithSpan<E>, filename: &str, source: &str) {
let files = SimpleFile::new(filename, source);
let config = codespan_reporting::term::Config::default();
let writer = StandardStream::stderr(ColorChoice::Auto);

let diagnostic = Diagnostic::error().with_labels(
ann_err
.spans()
.map(|(span, desc)| {
Label::primary((), span.to_range().unwrap()).with_message(desc.to_owned())
})
.collect(),
);

term::emit(&mut writer.lock(), &config, &files, &diagnostic).expect("cannot write error");
}
use codespan_reporting::term::termcolor::{ColorChoice, StandardStream};
use naga::FastHashMap;
20 changes: 3 additions & 17 deletions naga/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,14 @@ impl fmt::Display for ShaderError<crate::front::spv::Error> {
}
impl fmt::Display for ShaderError<crate::WithSpan<crate::valid::ValidationError>> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use codespan_reporting::{
diagnostic::{Diagnostic, Label},
files::SimpleFile,
term,
};
use codespan_reporting::{files::SimpleFile, term};

let label = self.label.as_deref().unwrap_or_default();
let files = SimpleFile::new(label, &self.source);
let config = term::Config::default();
let mut writer = termcolor::NoColor::new(Vec::new());

let diagnostic = Diagnostic::error().with_labels(
self.inner
.spans()
.map(|&(span, ref desc)| {
Label::primary((), span.to_range().unwrap()).with_message(desc.to_owned())
})
.collect(),
);

term::emit(&mut writer, &config, &files, &diagnostic).expect("cannot write error");

term::emit(&mut writer, &config, &files, &self.inner.diagnostic())
.expect("cannot write error");
write!(
f,
"\nShader validation {}",
Expand Down
2 changes: 1 addition & 1 deletion naga/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ impl<E> WithSpan<E> {
Some(self.spans[0].0.location(source))
}

fn diagnostic(&self) -> codespan_reporting::diagnostic::Diagnostic<()>
pub(crate) fn diagnostic(&self) -> codespan_reporting::diagnostic::Diagnostic<()>
where
E: Error,
{
Expand Down