-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[CLI] Add message-format-json
experiment value to aptos move compile
and aptos move lint
#15540
Open
mkurnikov
wants to merge
7
commits into
aptos-labs:main
Choose a base branch
from
mkurnikov:render-diagnostics-as-json
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+167
−23
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
415de9c
add --message-format to aptos move compile
mkurnikov 51040e4
get rid of diagnostic reporter
mkurnikov 7c92606
get rid of explicit message_format flag in favor of experiments
mkurnikov dc7c563
receive mut ref writer instead of value to enable code reuse
mkurnikov 8b27689
remove commented code
mkurnikov 9367be0
Merge branch 'main' into render-diagnostics-as-json
mkurnikov a6065ec
move from dyn trait in argument position
mkurnikov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
third_party/move/move-compiler-v2/src/diagnostics/human.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use crate::diagnostics::Emitter; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::{ | ||
diagnostic::Diagnostic, | ||
term::{emit, termcolor::WriteColor, Config}, | ||
}; | ||
|
||
pub struct HumanEmitter<'w, W: WriteColor> { | ||
writer: &'w mut W, | ||
} | ||
|
||
impl<'w, W> HumanEmitter<'w, W> | ||
where | ||
W: WriteColor, | ||
{ | ||
pub fn new(writer: &'w mut W) -> Self { | ||
HumanEmitter { writer } | ||
} | ||
} | ||
|
||
impl<'w, W> Emitter for HumanEmitter<'w, W> | ||
where | ||
W: WriteColor, | ||
{ | ||
fn emit(&mut self, source_files: &Files<String>, diag: &Diagnostic<FileId>) { | ||
emit(&mut self.writer, &Config::default(), source_files, diag).expect("emit must not fail") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::diagnostics::Emitter; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::diagnostic::Diagnostic; | ||
use std::io::Write; | ||
|
||
pub struct JsonEmitter<'w, W: Write> { | ||
writer: &'w mut W, | ||
} | ||
|
||
impl<'w, W: Write> JsonEmitter<'w, W> { | ||
pub fn new(writer: &'w mut W) -> Self { | ||
JsonEmitter { writer } | ||
} | ||
} | ||
|
||
impl<'w, W: Write> Emitter for JsonEmitter<'w, W> { | ||
fn emit(&mut self, _source_files: &Files<String>, diag: &Diagnostic<FileId>) { | ||
serde_json::to_writer(&mut self.writer, diag).expect("emit must not fail"); | ||
writeln!(&mut self.writer).unwrap(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate::{ | ||
diagnostics::{human::HumanEmitter, json::JsonEmitter}, | ||
options, Experiment, | ||
}; | ||
use anyhow::bail; | ||
use codespan::{FileId, Files}; | ||
use codespan_reporting::{ | ||
diagnostic::{Diagnostic, Severity}, | ||
term::termcolor::StandardStream, | ||
}; | ||
use move_model::model::GlobalEnv; | ||
|
||
pub mod human; | ||
pub mod json; | ||
|
||
impl options::Options { | ||
pub fn to_emitter<'w>(&self, stderr: &'w mut StandardStream) -> Box<dyn Emitter + 'w> { | ||
if self.experiment_on(Experiment::MESSAGE_FORMAT_JSON) { | ||
Box::new(JsonEmitter::new(stderr)) | ||
} else { | ||
Box::new(HumanEmitter::new(stderr)) | ||
} | ||
} | ||
} | ||
|
||
pub trait Emitter { | ||
fn emit(&mut self, source_files: &Files<String>, diag: &Diagnostic<FileId>); | ||
|
||
/// Writes accumulated diagnostics of given or higher severity. | ||
fn report_diag(&mut self, global_env: &GlobalEnv, severity: Severity) { | ||
global_env.report_diag_with_filter( | ||
|files, diag| self.emit(files, diag), | ||
|d| d.severity >= severity, | ||
); | ||
} | ||
|
||
/// Helper function to report diagnostics, check for errors, and fail with a message on | ||
/// errors. This function is idempotent and will not report the same diagnostics again. | ||
fn check_diag( | ||
&mut self, | ||
global_env: &GlobalEnv, | ||
report_severity: Severity, | ||
msg: &str, | ||
) -> anyhow::Result<()> { | ||
self.report_diag(global_env, report_severity); | ||
if global_env.has_errors() { | ||
bail!("exiting with {}", msg); | ||
} else { | ||
Ok(()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the code duplication? Could we not create the emitter based on the message format in options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, it's a draft, I'll remove it when someone confirms that the Emitter trait approach works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.