diff --git a/crates/typst-cli/src/world.rs b/crates/typst-cli/src/world.rs index 95bee235c..3dd0a6337 100644 --- a/crates/typst-cli/src/world.rs +++ b/crates/typst-cli/src/world.rs @@ -404,7 +404,9 @@ fn system_path( // Join the path to the root. If it tries to escape, deny // access. Note: It can still escape via symlinks. - id.vpath().resolve(root).ok_or(FileError::AccessDenied) + id.vpath() + .resolve(root) + .ok_or_else(|| FileError::AccessDenied(id.vpath().as_rootless_path().into())) } /// Reads a file from a `FileId`. @@ -427,7 +429,7 @@ fn read( fn read_from_disk(path: &Path) -> FileResult> { let f = |e| FileError::from_io(e, path); if fs::metadata(path).map_err(f)?.is_dir() { - Err(FileError::IsDirectory) + Err(FileError::IsDirectory(path.into())) } else { fs::read(path).map_err(f) } diff --git a/crates/typst-kit/src/package.rs b/crates/typst-kit/src/package.rs index 3bfb46c3e..b51c0a7c8 100644 --- a/crates/typst-kit/src/package.rs +++ b/crates/typst-kit/src/package.rs @@ -143,7 +143,6 @@ impl PackageStorage { } /// Tries to determine the latest version of a package. - /// TODO improve here too pub fn determine_latest_version( &self, spec: &VersionlessPackageSpec, diff --git a/crates/typst-library/src/diag.rs b/crates/typst-library/src/diag.rs index 4c7424875..2214b63f4 100644 --- a/crates/typst-library/src/diag.rs +++ b/crates/typst-library/src/diag.rs @@ -440,13 +440,13 @@ pub enum FileError { /// A file was not found at this path. NotFound(PathBuf), /// A file could not be accessed. - AccessDenied, + AccessDenied(PathBuf), /// A directory was found, but a file was expected. - IsDirectory, + IsDirectory(PathBuf), /// The file is not a Typst source file, but should have been. - NotSource, + NotSource(PathBuf), /// The file was not valid UTF-8, but should have been. - InvalidUtf8, + InvalidUtf8(Option), /// The package the file is part of could not be loaded. Package(PackageError), /// Another error. @@ -460,11 +460,11 @@ impl FileError { pub fn from_io(err: io::Error, path: &Path) -> Self { match err.kind() { io::ErrorKind::NotFound => Self::NotFound(path.into()), - io::ErrorKind::PermissionDenied => Self::AccessDenied, + io::ErrorKind::PermissionDenied => Self::AccessDenied(path.into()), io::ErrorKind::InvalidData if err.to_string().contains("stream did not contain valid UTF-8") => { - Self::InvalidUtf8 + Self::InvalidUtf8(Some(path.into())) } _ => Self::Other(Some(eco_format!("{err}"))), } @@ -479,10 +479,19 @@ impl Display for FileError { Self::NotFound(path) => { write!(f, "file not found (searched at {})", path.display()) } - Self::AccessDenied => f.pad("failed to load file (access denied)"), - Self::IsDirectory => f.pad("failed to load file (is a directory)"), - Self::NotSource => f.pad("not a typst source file"), - Self::InvalidUtf8 => f.pad("file is not valid utf-8"), + Self::AccessDenied(path) => { + write!(f, "failed to load file {} (access denied)", path.display()) + } + Self::IsDirectory(path) => { + write!(f, "failed to load file {} (is a directory)", path.display()) + } + Self::NotSource(path) => { + write!(f, "{} is not a typst source file", path.display()) + } + Self::InvalidUtf8(Some(path)) => { + write!(f, "file {} is not valid utf-8", path.display()) + } + Self::InvalidUtf8(None) => f.pad("file is not valid utf-8"), Self::Package(error) => error.fmt(f), Self::Other(Some(err)) => write!(f, "failed to load file ({err})"), Self::Other(None) => f.pad("failed to load file"), @@ -492,13 +501,13 @@ impl Display for FileError { impl From for FileError { fn from(_: Utf8Error) -> Self { - Self::InvalidUtf8 + Self::InvalidUtf8(None) } } impl From for FileError { fn from(_: FromUtf8Error) -> Self { - Self::InvalidUtf8 + Self::InvalidUtf8(None) } } @@ -545,7 +554,11 @@ impl Display for PackageError { write!(f, "package not found: {detail} (searching for {spec})",) } Self::VersionNotFound(spec, latest, registry) => { - write!(f, "package found, but version {} does not exist", spec.version,)?; + write!( + f, + "package '{}' found, but version {} does not exist", + spec.name, spec.version + )?; if let Some(version) = latest { write!(f, " (latest version provided by {registry} is {version})") } else { diff --git a/crates/typst/src/lib.rs b/crates/typst/src/lib.rs index a6bb4fe38..9d388f3bb 100644 --- a/crates/typst/src/lib.rs +++ b/crates/typst/src/lib.rs @@ -190,7 +190,7 @@ fn hint_invalid_main_file( file_error: FileError, input: FileId, ) -> EcoVec { - let is_utf8_error = matches!(file_error, FileError::InvalidUtf8); + let is_utf8_error = matches!(file_error, FileError::InvalidUtf8(_)); let mut diagnostic = SourceDiagnostic::error(Span::detached(), EcoString::from(file_error)); diff --git a/pending/manifest-unreadable.typ b/pending/manifest-unreadable.typ deleted file mode 100644 index 9da916486..000000000 --- a/pending/manifest-unreadable.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@local/mypkg:0.0.0" diff --git a/pending/namespace-doesnt-exist.typ b/pending/namespace-doesnt-exist.typ deleted file mode 100644 index 7d1515e1a..000000000 --- a/pending/namespace-doesnt-exist.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@nope/mypkg:0.0.1" diff --git a/pending/no-versions.typ b/pending/no-versions.typ deleted file mode 100644 index d279c96ff..000000000 --- a/pending/no-versions.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@local/nover:0.0.1" diff --git a/pending/package-doesnt-exist.typ b/pending/package-doesnt-exist.typ deleted file mode 100644 index d035abd9c..000000000 --- a/pending/package-doesnt-exist.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@local/nopkg:0.0.1" diff --git a/pending/preview-package-doesnt-exist.typ b/pending/preview-package-doesnt-exist.typ deleted file mode 100644 index f354b64e3..000000000 --- a/pending/preview-package-doesnt-exist.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@preview/nope:0.0.1" diff --git a/pending/preview-version-doesnt-exist.typ b/pending/preview-version-doesnt-exist.typ deleted file mode 100644 index 0914b42da..000000000 --- a/pending/preview-version-doesnt-exist.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@preview/cetz:5.0.0" diff --git a/pending/read-any-file.typ b/pending/read-any-file.typ deleted file mode 100644 index 3e19ec3ae..000000000 --- a/pending/read-any-file.typ +++ /dev/null @@ -1 +0,0 @@ -#let _ = read("/some" + "/path") diff --git a/pending/runall.sh b/pending/runall.sh deleted file mode 100755 index bfe75c63d..000000000 --- a/pending/runall.sh +++ /dev/null @@ -1,5 +0,0 @@ -#! /usr/bin/env bash - -for file in $(ls pending/*.typ); do - cargo run -- compile $file -done diff --git a/pending/version-doesnt-exist.typ b/pending/version-doesnt-exist.typ deleted file mode 100644 index 5010e4daa..000000000 --- a/pending/version-doesnt-exist.typ +++ /dev/null @@ -1 +0,0 @@ -#import "@local/penpo:5.0.0" diff --git a/tests/testit b/tests/testit deleted file mode 100644 index 2be10fee3..000000000 --- a/tests/testit +++ /dev/null @@ -1,2 +0,0 @@ -alias testit="cargo test --workspace --test tests --" -