triing to turn absolute to relative paths in error messages

This commit is contained in:
mlokr 2024-09-18 10:07:40 +02:00
parent 98dfd6b09c
commit 6fc0eb3498
No known key found for this signature in database
GPG key ID: DEA147DDEE644993
3 changed files with 22 additions and 36 deletions

View file

@ -241,7 +241,7 @@ main := fn(): int {
return @inline(foo.foo) return @inline(foo.foo)
} }
// in module: fooasd.hb // in module: foo.hb
Type := struct { Type := struct {
brah: int, brah: int,

View file

@ -30,7 +30,7 @@ use {
parser::Ast, parser::Ast,
std::{ std::{
collections::{hash_map, BTreeMap, VecDeque}, collections::{hash_map, BTreeMap, VecDeque},
io::{self, Read}, io,
ops::Range, ops::Range,
path::{Path, PathBuf}, path::{Path, PathBuf},
rc::Rc, rc::Rc,
@ -1170,7 +1170,12 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
impl std::fmt::Display for CantLoadFile { impl std::fmt::Display for CantLoadFile {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "can't load file: {} (from: {})", self.path.display(), self.from.display(),) write!(
f,
"can't load file: {} (from: {})",
parser::display_rel_path(&self.path),
parser::display_rel_path(&self.from),
)
} }
} }
@ -1186,27 +1191,6 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
} }
} }
#[derive(Debug)]
struct InvalidFileData(std::str::Utf8Error);
impl std::fmt::Display for InvalidFileData {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "invalid file data")
}
}
impl std::error::Error for InvalidFileData {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.0)
}
}
impl From<InvalidFileData> for io::Error {
fn from(e: InvalidFileData) -> Self {
io::Error::new(io::ErrorKind::InvalidData, e)
}
}
type Task = (u32, PathBuf, Option<std::process::Command>); type Task = (u32, PathBuf, Option<std::process::Command>);
let seen = Mutex::new(HashMap::<PathBuf, u32>::default()); let seen = Mutex::new(HashMap::<PathBuf, u32>::default());
@ -1236,7 +1220,7 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
let ImportPath::Git { link, chk, .. } = path else { let ImportPath::Git { link, chk, .. } = path else {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::NotFound, io::ErrorKind::NotFound,
format!("can't find file: {}", physiscal_path.display()), format!("can't find file: {}", parser::display_rel_path(&physiscal_path)),
)); ));
}; };
@ -1261,7 +1245,7 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
Ok(id) Ok(id)
}; };
let execute_task = |(_, path, command): Task, buffer: &mut Vec<u8>| { let execute_task = |(_, path, command): Task| {
if let Some(mut command) = command { if let Some(mut command) = command {
let output = command.output()?; let output = command.output()?;
if !output.status.success() { if !output.status.success() {
@ -1274,21 +1258,15 @@ pub fn parse_from_fs(extra_threads: usize, root: &str) -> io::Result<Vec<Ast>> {
let path = path.to_str().ok_or_else(|| { let path = path.to_str().ok_or_else(|| {
io::Error::new( io::Error::new(
io::ErrorKind::InvalidData, io::ErrorKind::InvalidData,
format!("path contains invalid characters: {}", path.display()), format!("path contains invalid characters: {}", parser::display_rel_path(&path)),
) )
})?; })?;
let mut file = std::fs::File::open(path)?; Ok(Ast::new(path, std::fs::read_to_string(path)?, &loader))
file.read_to_end(buffer)?;
let src = std::str::from_utf8(buffer).map_err(InvalidFileData)?;
Ok(Ast::new(path, src.to_owned(), &loader))
}; };
let thread = || { let thread = || {
let mut buffer = Vec::new();
while let Some(task @ (indx, ..)) = tasks.pop() { while let Some(task @ (indx, ..)) = tasks.pop() {
let res = execute_task(task, &mut buffer); let res = execute_task(task);
buffer.clear();
let mut ast = ast.lock().unwrap(); let mut ast = ast.lock().unwrap();
let len = ast.len().max(indx as usize + 1); let len = ast.len().max(indx as usize + 1);
ast.resize_with(len, || Err(io::ErrorKind::InvalidData.into())); ast.resize_with(len, || Err(io::ErrorKind::InvalidData.into()));

View file

@ -5,8 +5,10 @@ use {
}, },
std::{ std::{
cell::{Cell, UnsafeCell}, cell::{Cell, UnsafeCell},
ffi::OsStr,
fmt, io, fmt, io,
ops::{Deref, Not}, ops::{Deref, Not},
path::PathBuf,
ptr::NonNull, ptr::NonNull,
sync::atomic::AtomicUsize, sync::atomic::AtomicUsize,
}, },
@ -1239,6 +1241,12 @@ impl AstInner<[Symbol]> {
} }
} }
pub fn display_rel_path(path: &(impl AsRef<OsStr> + ?Sized)) -> std::path::Display {
static CWD: std::sync::LazyLock<PathBuf> =
std::sync::LazyLock::new(|| std::env::current_dir().unwrap_or_default());
std::path::Path::new(path).strip_prefix(&*CWD).unwrap_or(std::path::Path::new(path)).display()
}
pub fn report_to( pub fn report_to(
file: &str, file: &str,
path: &str, path: &str,
@ -1247,7 +1255,7 @@ pub fn report_to(
out: &mut impl fmt::Write, out: &mut impl fmt::Write,
) { ) {
let (line, mut col) = lexer::line_col(file.as_bytes(), pos); let (line, mut col) = lexer::line_col(file.as_bytes(), pos);
_ = writeln!(out, "{}:{}:{}: {}", path, line, col, msg); _ = writeln!(out, "{}:{}:{}: {}", display_rel_path(path), line, col, msg);
let line = &file[file[..pos as usize].rfind('\n').map_or(0, |i| i + 1) let line = &file[file[..pos as usize].rfind('\n').map_or(0, |i| i + 1)
..file[pos as usize..].find('\n').unwrap_or(file.len()) + pos as usize]; ..file[pos as usize..].find('\n').unwrap_or(file.len()) + pos as usize];