🦀
Rust: 開発中に crate のコードの変更を反映する
要約
Cargo.toml
の dependencies で、コードを変更した crate を local path で指定する。
[dependencies]
chrono-humanize = { path = "/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/" }
経緯
OSS で Rust のコードを触っていたときに、print デバッグのために crate のコードを変更して実行したくなった。コードを変更して単純に cargo build
するだけでは、crate の変更が反映されなかった (crate の方はコンパイルされなかった) ので、どうやって反映したらいいか確認した。
サンプルの用意
- サンプルとして使うプロジェクトの作成、crate のインストール
$ cargo new local_lib_test
Created binary (application) `local_lib_test` package
$ cd local_lib_test
$ cargo add chrono_humanize # なんてもいいけど適当なクレートを追加
Updating crates.io index
~~ 略 ~~
$ cargo add chrono
Updating crates.io index
~~ 略 ~~
- 適当な例を用意
use chrono::Duration;
use chrono_humanize::{Accuracy, HumanTime, Tense};
fn main() {
let dt = Duration::d(45);
let ht = HumanTime::from(dt);
println!("{}", ht.to_text_en(Accuracy::Rough, Tense::Present));
}
- 実行すると時間を英語表記にしたものを返してくれる
$ cargo run
Compiling local_lib_test v0.1.0 (/Users/hiroki/Development/local_lib_test)
Finished dev [unoptimized + debuginfo] target(s) in 0.52s
Running `target/debug/local_lib_test`
an hour
確認
例えば、to_text_en
関数の中の挙動を確認するために、エディタで関数の実装に jump したあと、142行目の部分に dbg!(&periods);
を挿入したとする。
そのまま cargo run
をしても変更は反映されない。
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.38s
Running `target/debug/local_lib_test`
an hour <- dbg の結果が出ていない
修正
調べた感じ Cargo.toml
で local path を指定できるみたい。
実際の Cargo.toml
を見ると以下のようになっていた。
これでは local のコードを変更しても反映されないのも納得。
[dependencies]
chrono = "0.4.38"
chrono-humanize = "0.2.3"
以下のように local path を指定してやる。
[dependencies]
chrono = "0.4.38"
chrono-humanize = { path = "/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/" }
変更後に再度 cargo run
無事 dbg! の結果が出ている 🥳
$ cargo run
Compiling chrono-humanize v0.2.3 (/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3)
Compiling local_lib_test v0.1.0 (/Users/hiroki/Development/local_lib_test)
Finished dev [unoptimized + debuginfo] target(s) in 0.90s
Running `target/debug/local_lib_test`
[/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/src/humantime.rs:142] &periods = [
Hours(
1,
),
] <- dbg の結果が出ている!
an hour
念の為、local の crate の code に追加した dbg!(&periods);
の下に dbg!(&tense);
も追加。
ちゃんと追加で変更しても build してくれている。
$ cargo run
Compiling chrono-humanize v0.2.3 (/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3)
Compiling local_lib_test v0.1.0 (/Users/hiroki/Development/local_lib_test)
Finished dev [unoptimized + debuginfo] target(s) in 0.62s
Running `target/debug/local_lib_test`
[/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/src/humantime.rs:142] &periods = [
Hours(
1,
),
]
[/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/src/humantime.rs:143] &tense = Present <- 追加した dbg の結果も反映された 👍
an hour
逆に何も変更せずに再度 run。
特に build していないので、コードの変更があったがどうかはちゃんと見てくれているみたい。
うれC。
$ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.07s
Running `target/debug/local_lib_test`
[/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/src/humantime.rs:142] &periods = [
Hours(
1,
),
]
[/Users/hiroki/.cargo/registry/src/github.com-1ecc6299db9ec823/chrono-humanize-0.2.3/src/humantime.rs:143] &tense = Present
an hour
Discussion