Skip to content
Open
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: 1 addition & 1 deletion src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub async fn all_checks(
compile::check(&mut template_diags, &template_world);
let template_dir = template_world
.root()
.strip_prefix(worlds.package.root())
.relative_template_dir()
.expect("Template should be in a subfolder of the package");
diags.extend(template_diags, template_dir);
}
Expand Down
6 changes: 3 additions & 3 deletions src/check/authors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub async fn check(diags: &mut Diagnostics, spec: &PackageSpec) -> Option<()> {
}

pub async fn commit_for_previous_version(spec: &PackageSpec) -> Option<String> {
let last_manifest = spec.previous_version()?.directory().join("typst.toml");
let last_manifest = spec.previous_version()?.git_dir().join("typst.toml");

let repo = git::repo_dir();
let repo = git::GitRepo::open(&repo).await.ok()?;
Expand All @@ -36,8 +36,8 @@ pub async fn commit_for_previous_version(spec: &PackageSpec) -> Option<String> {
}

pub async fn authors_are_differents(spec: &PackageSpec) -> Option<bool> {
let last_manifest = spec.previous_version()?.directory().join("typst.toml");
let new_manifest = spec.directory().join("typst.toml");
let last_manifest = spec.previous_version()?.git_dir().join("typst.toml");
let new_manifest = spec.git_dir().join("typst.toml");

let repo = git::repo_dir();
let repo = git::GitRepo::open(&repo).await.ok()?;
Expand Down
36 changes: 11 additions & 25 deletions src/check/imports.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
use std::{
path::{Path, PathBuf},
str::FromStr,
};
use std::path::{Path, PathBuf};
use std::str::FromStr;

use codespan_reporting::diagnostic::{Diagnostic, Severity};
use typst::{
World,
syntax::{
ast::{self, AstNode, ModuleImport},
package::{PackageSpec, PackageVersion, VersionlessPackageSpec},
},
};
use typst::syntax::ast::{self, AstNode, ModuleImport};
use typst::syntax::package::{PackageSpec, PackageVersion, VersionlessPackageSpec};
use walkdir::WalkDir;

use crate::check::path::PackagePath;
use crate::check::{Diagnostics, Result, TryExt, label};
use crate::world::SystemWorld;

pub fn check(diags: &mut Diagnostics, package_dir: &Path, world: &SystemWorld) -> Result<()> {
let root_path = world.root();
let main_path = root_path
.join(world.main().vpath().get_without_slash())
.canonicalize()
.ok();
let all_packages = root_path
.parent()
.and_then(|package_dir| package_dir.parent())
.and_then(|namespace_dir| namespace_dir.parent());
let all_packages = world.root().all_packages();
let entrypoint = world.root().is_package().then(|| world.entrypoint());

for ch in WalkDir::new(package_dir).into_iter().flatten() {
let Ok(meta) = ch.metadata() else {
Expand All @@ -46,7 +32,7 @@ pub fn check(diags: &mut Diagnostics, package_dir: &Path, world: &SystemWorld) -
world,
source.root(),
path.full(),
main_path.as_deref(),
entrypoint.as_deref(),
all_packages,
);
}
Expand All @@ -58,12 +44,12 @@ pub fn check(diags: &mut Diagnostics, package_dir: &Path, world: &SystemWorld) -
pub fn check_ast(
diags: &mut Diagnostics,
world: &SystemWorld,
root: &typst::syntax::SyntaxNode,
node: &typst::syntax::SyntaxNode,
path: &Path,
main_path: Option<&Path>,
package_entrypoint: Option<&Path>,
all_packages: Option<&Path>,
) {
let imports = root.children().filter_map(|ch| ch.cast::<ModuleImport>());
let imports = node.children().filter_map(|ch| ch.cast::<ModuleImport>());
for import in imports {
let ast::Expr::Str(source_str) = import.source() else {
continue;
Expand All @@ -74,7 +60,7 @@ pub fn check_ast(
.join(source_str.get().as_str())
.canonicalize()
.ok();
if main_path == import_path.as_deref() {
if package_entrypoint == import_path.as_deref() {
diags.emit(
Diagnostic::warning()
.with_labels(label(world, import.span()).into_iter().collect())
Expand Down
101 changes: 62 additions & 39 deletions src/check/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use typst::syntax::{
};

use crate::check::files;
use crate::check::path::{self, PackagePath};
use crate::check::path::PackagePath;
use crate::world::WorldRoot;
use crate::{
check::{Diagnostics, Result, TryExt},
world::SystemWorld,
Expand All @@ -34,7 +35,7 @@ pub struct Manifest {

#[derive(Debug, Clone)]
pub struct Package {
pub entrypoint: Spanned<PackagePath>,
pub entrypoint: Spanned<VirtualPath>,
pub name: Option<Spanned<String>>,
pub version: Option<Spanned<PackageVersion>>,
pub exclude: Spanned<Exclude>,
Expand All @@ -45,7 +46,7 @@ pub struct Template {
pub path: Option<Spanned<PackagePath>>,
/// The package directory of this path is still relative to the package, not
/// the template directory.
pub entrypoint: Option<Spanned<PackagePath>>,
pub entrypoint: Option<Spanned<VirtualPath>>,
pub thumbnail: Option<Spanned<PackagePath>>,
}

Expand Down Expand Up @@ -82,7 +83,15 @@ pub async fn check(
"manifest/package/entrypoint/missing",
"Packages must specify an `entrypoint` in their manifest",
)?;
let entrypoint = entrypoint.map(|e| PackagePath::from_relative(package_dir, e));
let entrypoint = entrypoint.try_map(|path| {
VirtualPath::new(path).map_err(|err| {
Diagnostic::error()
.with_label(Label::primary(manifest_id(), entrypoint.span()))
.with_code("manifest/template/entrypoint/invalid")
.with_message(format_args!("invalid entrypoint ({err})"))
})
})?;

let name = check_name(diags, package, package_spec);
let version = check_version(diags, package, package_spec);
let template = check_template(diags, &manifest, package_dir);
Expand Down Expand Up @@ -121,14 +130,10 @@ pub async fn check(
});

let world = SystemWorld::new(
package.entrypoint.full().to_owned(),
package_dir.to_owned(),
package.entrypoint.val.clone(),
WorldRoot::Package(package_dir.to_owned()),
package_spec.clone(),
)
.error(
"compile/package-world-init",
"Failed to initialize the Typst compiler",
)?;
);

let template_world = world_for_template(package_dir, package_spec, &package, &template);

Expand Down Expand Up @@ -162,7 +167,7 @@ fn check_name(
let error = Diagnostic::error().with_label(Label::primary(manifest_id(), name.span()));
let warning = Diagnostic::warning().with_label(Label::primary(manifest_id(), name.span()));

let Some(name) = name.try_map(Item::as_str) else {
let Some(name) = name.filter_map(Item::as_str) else {
diags.emit(
error
.with_code("manifest/package/name/type")
Expand Down Expand Up @@ -228,7 +233,7 @@ fn check_version(

let error = Diagnostic::error().with_label(Label::primary(manifest_id(), version.span()));

let Some(version) = version.try_map(Item::as_str) else {
let Some(version) = version.filter_map(Item::as_str) else {
diags.emit(
error
.with_code("manifest/package/version/type")
Expand All @@ -237,7 +242,7 @@ fn check_version(
return None;
};

let Some(version) = version.try_map(|v| v.parse::<PackageVersion>().ok()) else {
let Some(version) = version.filter_map(|v| v.parse::<PackageVersion>().ok()) else {
diags.emit(
error
.with_code("manifest/package/version/invalid")
Expand Down Expand Up @@ -272,7 +277,7 @@ fn check_version(
fn check_compiler_version(diags: &mut Diagnostics, package: Spanned<&Table>) -> Option<()> {
let compiler = package.get_spanned("compiler")?;

let Some(compiler) = compiler.try_map(Item::as_str) else {
let Some(compiler) = compiler.filter_map(Item::as_str) else {
diags.emit(
Diagnostic::error()
.with_label(Label::primary(manifest_id(), compiler.span()))
Expand Down Expand Up @@ -527,7 +532,7 @@ fn check_exclude(
return Ok(Spanned::new(Exclude::empty(), package.span()));
};

let exclude = exclude.try_map(Item::as_array).error(
let exclude = exclude.filter_map(Item::as_array).error(
"manifest/package/exclude/type",
"`exclude` must be an array of strings",
)?;
Expand Down Expand Up @@ -599,6 +604,10 @@ impl Exclude {
Self { globs }
}

pub fn root(&self) -> &Path {
self.globs.path()
}

/// Whether the package file path is excluded.
pub fn matches_file<T: AsRef<Path>>(&self, path: &PackagePath<T>) -> bool {
self.matches_relative_file(path.relative())
Expand Down Expand Up @@ -634,7 +643,7 @@ fn check_template(
) -> Option<Spanned<Template>> {
let template = manifest.get_spanned("template")?;

let Some(template) = template.try_map(Item::as_table) else {
let Some(template) = template.filter_map(Item::as_table) else {
diags.emit(
Diagnostic::error()
.with_label(Label::primary(manifest_id(), template.span()))
Expand All @@ -650,12 +659,17 @@ fn check_template(
});

let entrypoint = template.get_str("entrypoint").and_then(|entrypoint| {
let template_dir = path.as_ref()?;
Some(
entrypoint
.map(|entrypoint| path::join_to(template_dir.full(), entrypoint))
.map(|path| PackagePath::from_full(package_dir, path)),
)
let virtual_path = VirtualPath::new(entrypoint.val)
.inspect_err(|err| {
diags.emit(
Diagnostic::error()
.with_label(Label::primary(manifest_id(), entrypoint.span()))
.with_code("manifest/template/entrypoint/invalid")
.with_message(format_args!("invalid entrypoint ({err})")),
);
})
.ok()?;
Some(entrypoint.map(|_| virtual_path))
});

let thumbnail = template.get_str("thumbnail").map(|path| {
Expand All @@ -678,18 +692,17 @@ fn world_for_template(
) -> Option<SystemWorld> {
let package_spec = package_spec?;
let template = template.as_ref()?;
let template_path = template.path.as_ref()?;
let template_main = template.entrypoint.as_ref()?;

let mut world = SystemWorld::new(
template_main.full().to_owned(),
template_path.full().to_owned(),
Some(package_spec),
)
.ok()?
.with_package_override(package_dir.to_owned());
world.exclude(package.exclude.val.clone());
Some(world)
let template_dir = template.path.as_ref()?;
let main = template.entrypoint.as_ref()?;

let root = WorldRoot::Template {
package: package_dir.to_owned(),
template: template_dir.full().to_owned(),
};

let world = SystemWorld::new(main.val.clone(), root, Some(package_spec));

Some(world.exclude(package.exclude.val.clone()))
}

fn dont_exclude_template_files(
Expand Down Expand Up @@ -820,7 +833,7 @@ impl<T> Spanned<T> {
}
}

pub fn try_map<F, V>(self, f: F) -> Option<Spanned<V>>
pub fn filter_map<F, V>(self, f: F) -> Option<Spanned<V>>
where
F: FnOnce(T) -> Option<V>,
{
Expand All @@ -829,6 +842,16 @@ impl<T> Spanned<T> {
span: self.span,
})
}

pub fn try_map<F, V, E>(self, f: F) -> std::result::Result<Spanned<V>, E>
where
F: FnOnce(T) -> std::result::Result<V, E>,
{
Ok(Spanned {
val: f(self.val)?,
span: self.span,
})
}
}

impl<T: ?Sized + ToOwned> Spanned<&T> {
Expand Down Expand Up @@ -877,14 +900,14 @@ impl TomlExt for Table {
}

fn get_table(&self, name: &'static str) -> Option<Spanned<&Table>> {
self.get_spanned(name)?.try_map(Item::as_table)
self.get_spanned(name)?.filter_map(Item::as_table)
}

fn get_array(&self, name: &'static str) -> Option<Spanned<&Array>> {
self.get_spanned(name)?.try_map(Item::as_array)
self.get_spanned(name)?.filter_map(Item::as_array)
}

fn get_str(&self, name: &'static str) -> Option<Spanned<&str>> {
self.get_spanned(name)?.try_map(Item::as_str)
self.get_spanned(name)?.filter_map(Item::as_str)
}
}
28 changes: 8 additions & 20 deletions src/check/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ use codespan_reporting::diagnostic::{Diagnostic, Label};
use comrak::nodes::{LineColumn, NodeList, NodeValue as MdNode, Sourcepos};
use html5ever::tendril::TendrilSink;
use regex::Regex;
use typst::syntax::RootedPath;
use typst::{
World,
foundations::Bytes,
syntax::{FileId, VirtualPath},
};
use typst::foundations::Bytes;
use typst::syntax::{FileId, RootedPath, VirtualPath};
use url::Url;

use crate::check::path::PackagePath;
Expand All @@ -29,7 +25,7 @@ pub struct Readme {
pub async fn check(world: &SystemWorld, diags: &mut Diagnostics) -> crate::check::Result<Readme> {
// check syntax, versions and kebab-case
// warn on unsupported gfm features
let text = tokio::fs::read_to_string(world.root().join("README.md"))
let text = tokio::fs::read_to_string(world.root().package_dir().join("README.md"))
.await
.error("io/readme", "Failed to read README.md")?;

Expand Down Expand Up @@ -166,22 +162,14 @@ fn check_readme_code_block(

kebab_case::check_ast(world, diags, &HashSet::new(), source.root(), true);

let main_path = world
.root()
.join(world.main().vpath().get_without_slash())
.canonicalize()
.ok();
let all_packages = world
.root()
.parent()
.and_then(|package_dir| package_dir.parent())
.and_then(|namespace_dir| namespace_dir.parent());
let entrypoint = world.root().is_package().then(|| world.entrypoint());
let all_packages = world.root().all_packages();
imports::check_ast(
diags,
world,
source.root(),
&world.root().join("README.md"),
main_path.as_deref(),
&world.root().package_dir().join("README.md"),
entrypoint.as_deref(),
all_packages,
);
}
Expand Down Expand Up @@ -311,7 +299,7 @@ fn check_readme_link_url(
}

// Check if the local file exists.
let path = PackagePath::from_relative(world.root(), absolute_path.as_ref());
let path = PackagePath::from_relative(world.root().package_dir(), absolute_path.as_ref());
if !path.full().exists() {
diags.emit(
Diagnostic::error()
Expand Down
Loading
Loading