google / rerast

A tool for transforming Rust code using rules
Apache License 2.0
712 stars 40 forks source link

Allow running when not in root directory #33

Closed jyn514 closed 4 years ago

jyn514 commented 4 years ago
(bash) ~/.../import-test/src ▶️ cargo +nightly rerast --file src/lib.rs -p 'l1: crate::data::prelude::Location' -s 'l1.foo()' -r 'l1.bar()' --diff_cmd diff
error: No such file or directory (os error 2)
(bash) ~/.../import-test/src ▶️ cd ..
Cargo.lock  Cargo.toml  src  target
(bash) ~/.../rerast/import-test ▶️ cargo +nightly rerast --file src/lib.rs -p 'l1: crate::data::prelude::Location' -s 'l1.foo()' -r 'l1.bar()' --diff_cmd diff
27c27
<         assert_eq!(l.foo(), 42);
---
>         assert_eq!(l.bar(), 42);

I think this might be happening after the diff is calculated because on my large project it runs for a while before giving an error.

jyn514 commented 4 years ago

It looks like this might be the culprit: https://github.com/google/rerast/blob/master/src/code_substitution.rs#L268

I'm interested in helping fix this, hopefully I'll have a PR by the end of today :)

jyn514 commented 4 years ago

Do you know how I would get the top level directory for a crate? I see https://docs.rs/cargo/0.40.0/cargo/sources/path/struct.PathSource.html in the docs but I'm not sure how I would get an instance of it.

Ignore me, you're using cargo metadata in cargo-rerast.rs so it's just workspace_root