|
| 1 | +use std::{env, fs, path::PathBuf, process::exit}; |
| 2 | + |
| 3 | +use miette::Diagnostic as _; |
| 4 | +use serde::Serialize; |
| 5 | + |
| 6 | +use crate::{errors::HermesError, parse}; |
| 7 | + |
| 8 | +/// The entrypoint for running under the `ui_test` crate, which expects us to be |
| 9 | +/// `rustc`. This is a bit of a hack, but it works. |
| 10 | +pub fn run() { |
| 11 | + let args: Vec<String> = env::args().collect(); |
| 12 | + |
| 13 | + // Spoof version if requested |
| 14 | + if args.contains(&"-vV".to_string()) || args.contains(&"--version".to_string()) { |
| 15 | + println!("rustc 1.93.0-nightly (hermes-shim)"); |
| 16 | + println!("binary: rustc"); |
| 17 | + println!("commit-hash: 0000000000000000000000000000000000000000"); |
| 18 | + println!("commit-date: 2025-01-01"); |
| 19 | + println!("host: x86_64-unknown-linux-gnu"); |
| 20 | + println!("release: 1.93.0-nightly"); |
| 21 | + exit(0); |
| 22 | + } |
| 23 | + |
| 24 | + // Find the file (ignoring rustc flags like --out-dir) |
| 25 | + let file_path = args |
| 26 | + .iter() |
| 27 | + .skip(1) |
| 28 | + .find(|arg| arg.ends_with(".rs") && !arg.starts_with("--")) |
| 29 | + .map(PathBuf::from) |
| 30 | + .unwrap_or_else(|| { |
| 31 | + // If no file found, maybe it's just a flag check. Exit successfully |
| 32 | + // to appease ui_test. |
| 33 | + exit(0); |
| 34 | + }); |
| 35 | + |
| 36 | + // Run logic with JSON emitter |
| 37 | + let source = fs::read_to_string(&file_path).unwrap_or_default(); |
| 38 | + let mut has_errors = false; |
| 39 | + |
| 40 | + parse::visit_hermes_items_in_file(&file_path, &source, |res| { |
| 41 | + if let Err(e) = res { |
| 42 | + has_errors = true; |
| 43 | + emit_rustc_json(&e, &source, file_path.to_str().unwrap()); |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + if has_errors { |
| 48 | + exit(1); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +#[derive(Serialize)] |
| 53 | +struct RustcDiagnostic { |
| 54 | + message: String, |
| 55 | + level: String, |
| 56 | + spans: Vec<RustcSpan>, |
| 57 | + children: Vec<RustcDiagnostic>, |
| 58 | + rendered: String, |
| 59 | +} |
| 60 | + |
| 61 | +#[derive(Serialize)] |
| 62 | +struct RustcSpan { |
| 63 | + file_name: String, |
| 64 | + byte_start: usize, |
| 65 | + byte_end: usize, |
| 66 | + line_start: usize, |
| 67 | + line_end: usize, |
| 68 | + column_start: usize, |
| 69 | + column_end: usize, |
| 70 | + is_primary: bool, |
| 71 | + text: Vec<RustcSpanLine>, // ui_test sometimes checks the snippet context |
| 72 | +} |
| 73 | + |
| 74 | +#[derive(Serialize)] |
| 75 | +struct RustcSpanLine { |
| 76 | + text: String, |
| 77 | + highlight_start: usize, |
| 78 | + highlight_end: usize, |
| 79 | +} |
| 80 | + |
| 81 | +pub fn emit_rustc_json(e: &HermesError, source: &str, file: &str) { |
| 82 | + let msg = e.to_string(); |
| 83 | + // Use miette's span to get byte offsets. |
| 84 | + let span = e.labels().and_then(|mut l| l.next()); |
| 85 | + |
| 86 | + let mut spans = Vec::new(); |
| 87 | + if let Some(labeled_span) = span { |
| 88 | + let offset = labeled_span.offset(); |
| 89 | + let len = labeled_span.len(); |
| 90 | + |
| 91 | + // Calculate lines/cols manually (miette makes this hard to extract |
| 92 | + // without a Report). This is isolated here now, so it's fine. |
| 93 | + let prefix = &source[..offset]; |
| 94 | + let line_start = prefix.lines().count().max(1); |
| 95 | + let last_nl = prefix.rfind('\n').map(|i| i + 1).unwrap_or(0); |
| 96 | + let column_start = (offset - last_nl) + 1; |
| 97 | + |
| 98 | + // Grab the line text for the snippet |
| 99 | + let line_end_idx = source[offset..].find('\n').map(|i| offset + i).unwrap_or(source.len()); |
| 100 | + let line_text = source[last_nl..line_end_idx].to_string(); |
| 101 | + |
| 102 | + spans.push(RustcSpan { |
| 103 | + file_name: file.to_string(), |
| 104 | + byte_start: offset, |
| 105 | + byte_end: offset + len, |
| 106 | + line_start, |
| 107 | + line_end: line_start, // Assuming single line for simplicity |
| 108 | + column_start, |
| 109 | + column_end: column_start + len, |
| 110 | + is_primary: true, |
| 111 | + text: vec![RustcSpanLine { |
| 112 | + text: line_text, |
| 113 | + highlight_start: column_start, |
| 114 | + highlight_end: column_start + len, |
| 115 | + }], |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + let diag = RustcDiagnostic { |
| 120 | + message: msg.clone(), |
| 121 | + level: "error".to_string(), |
| 122 | + spans, |
| 123 | + children: vec![], |
| 124 | + rendered: format!("error: {}\n", msg), |
| 125 | + }; |
| 126 | + |
| 127 | + eprintln!("{}", serde_json::to_string(&diag).unwrap()); |
| 128 | +} |
0 commit comments