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)
Oh, that project is set up in a very unconventional manner.
Most workspaces are set up where each subcrate has a
srcdirectory containing the sources, rather than redefining where the entry point is inCargo.toml. (They also can have other “magic” directories, such astestsandexamples.)Your problem isn’t that you’re in a workspace, but that you’ve defined a different
lib.path.A common workspace layout
Any change to where the
#[grammar = ""]path starts from is technically a breaking change. The path is currently defined to be relative to thesrcdirectory, 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 oflib.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 whensrcdoesn’t exist (and “just” collapsing isn’t necessarily correct in the face of symlinks, thus why Rust’scanonicalizecurrently 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.