igordejanovic / rustemo

LR/GLR parser generator for Rust https://igordejanovic.github.io/rustemo/
Apache License 2.0
30 stars 3 forks source link

"*_actions.rs" in "target" should be always regenerated #13

Closed safinaskar closed 2 months ago

safinaskar commented 3 months ago

Reproduction Create these files in some test dir: Cargo.toml:

[package]
name = "test-proj"
version = "0.1.0"
edition = "2021"

[build-dependencies]
rustemo-compiler = "0.6.0"

[dependencies]
colored = "2.1.0"
once_cell = "1.19.0"
regex = "1.10.5"
rustemo = "0.6.0"

build.rs:

fn main() {
    rustemo_compiler::Settings::new()
        .process_dir().unwrap();
}

src/a.rustemo:

A: foo;

terminals
foo: "abc";

src/main.rs:

rustemo::rustemo_mod!(a, "/src");
rustemo::rustemo_mod!(a_actions, "/src");

fn main() {
    use rustemo::Parser;
    println!("{:#?}", a::AParser::new().parse("").unwrap());
}

Make sure nothing else exists in the dir. In particular, make sure there is no target dir.

Now run cargo build. Everything will build successfully (modulo some warnings). Then replace all foos with bars in src/a.rustemo, i. e. you should get this:

A: bar;

terminals
bar: "abc";

Now, without removing target, run cargo build. You will see this error message (I ommited warnings):

error[E0599]: no variant or associated item named `bar` found for enum `A` in the current scope
  --> /tmp/test-proj/target/debug/build/test-proj-5f0af2ed166c1a89/out/src/a_actions.rs:17:8
   |
10 | pub enum A {
   | ---------- variant or associated item `bar` not found for this enum
...
17 |     A::bar
   |        ^^^ variant or associated item not found in `A`

For more information about this error, try `rustc --explain E0599`.
warning: `test-proj` (bin "test-proj") generated 4 warnings
error: could not compile `test-proj` (bin "test-proj") due to 1 previous error; 4 warnings emitted

Now remove target and run cargo build again. Everything will be built successfully.

So, we see that rm -r target is needed to build project. This never should be necessary.

I thing the reason is so: rustemo doesn't regenerate a_actions.rs (despite a_actions.rs is located in target). I think this is wrong, because this behavior necessitates using of rm -r target. So, if *_actions.rs is located in target, then always regenerate it

fogarecious commented 2 months ago

To regenerate all action files, I think we can use force(true) in rustemo_compiler::Settings. For example, in build.rs, use the following code:

fn main() {
    rustemo_compiler::Settings::new()
        .force(true)
        .process_dir()
        .unwrap();
}

This should solve the problem mentioned in the post.

safinaskar commented 2 months ago

@fogarecious , yes, but anyway I think current behavior is a bug. force(true) should be ~applied~ implied. rm -r target should never be needed

igordejanovic commented 2 months ago

I agree. This should be treated as a bug as the product of parser compilation should not depend on what is already in the target folder. Until the bugfix is released @fogarecious workaround can be used.