pest: Grammar file not found on workspace

I use pest in a crate of a workspace, so this crate has no src folder. I was forced to create an empty src subfolder in my crate so that pest could find my file, it’s not clean.

It’s easy to reproduce this bug, create a new workspace project and try to import pest grammar file from a crate of workspace.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 16 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Oh, that project is set up in a very unconventional manner.

Most workspaces are set up where each subcrate has a src directory containing the sources, rather than redefining where the entry point is in Cargo.toml. (They also can have other “magic” directories, such as tests and examples.)

Your problem isn’t that you’re in a workspace, but that you’ve defined a different lib.path.

A common workspace layout
/Cargo.toml
/derive/Cargo.toml
/derive/src/lib.rs
/derive/tests/test001.rs
/traits/Cargo.toml
/traits/src/lib.rs

Any change to where the #[grammar = ""] path starts from is technically a breaking change. The path is currently defined to be relative to the src directory, so for full strict backwards compatibility, that needs to be the default for a relative path.

One solution is to change the path to instead of being relative from src, to be relative from the directory of lib.path (bin.path?).

Another is to take a page from the JS world and use “anchored” paths: #[grammar = "@/src/pest/grammar.pest"], where @ is basically a “path macro” for %CARGO_MANIFEST_DIR%.

And another (poor?) solution is just to make the path %CARGO_MANIFEST_DIR%/src/../{} work even when src doesn’t exist (and “just” collapsing isn’t necessarily correct in the face of symlinks, thus why Rust’s canonicalize currently uses the filesystem instead of being “simple”).

Just want to leave a message here: Would be really nice to be able to use a path that does not start from the src/ dir. Use case: I’m using pest from a build.rs and thus don’t really want my grammar file(s) be located in src/, which should contain only runtime code.