tonic icon indicating copy to clipboard operation
tonic copied to clipboard

Project using `tonic-prost-build` rebuilding when unrelated files not in `.gitignore` change

Open WolleTD opened this issue 4 months ago • 2 comments

Bug Report

Version

  • tonic-prost-build 0.14.x / 969408e
  • Rust 1.89.0, Rust 1.90.0

Platform

Linux wolfgnu 6.16.8-arch3-1 #1 SMP PREEMPT_DYNAMIC Mon, 22 Sep 2025 22:08:35 +0000 x86_64 GNU/Linux

Description

When using tonic-prost-build (any version, since it didn't exist before 0.14) in a build.rs, the project is rebuilt whenever a file in the project directory changes, even if it isn't part of the build. Adding the file to .gitignore fixes that. target/ isn't affected even when not in .gitignore.

I narrowed this issue down to commit 969408e. Unfortunately, that's a pretty large one introducing the tonic-prost-build crate for the first time.

I managed to create a minimal example. Create a project with these files (patch):

# Cargo.toml
[package]
name = "test-prost"
version = "0.1.0"
edition = "2024"

[dependencies]

[build-dependencies]
#tonic-prost-build = { path = "../tonic/tonic-prost-build" }
#tonic-build = { git = "https://github.com/hyperium/tonic.git", rev = "761ebf5f" }
tonic-prost-build = "0.14.2"
// build.rs
fn main() {
    tonic_prost_build::compile_protos("src/test.proto").unwrap();
    //tonic_build::compile_protos("src/test.proto").unwrap();
}
// src/main.rs
fn main() {
    println!("Hello, world!");
}
// src/test.proto
syntax = "proto3";
message Null {}
service Test {
  rpc Test(Null) returns (Null) {}
}
  1. Run cargo build once
  2. Do nothing an run cargo build again: no build happens
  3. touch foo.txt and run cargo build: project is rebuilding
  4. Repeat 2 and 3 until sufficiently convinced
  5. Add foo.txt to .gitignore
  6. Run cargo build: project is rebuilding because .gitignore changed
  7. touch foo.txt and run cargo build: no build happens

The issue does not appear with tonic-build from 761ebf5f, which is already using prost 0.14.

WolleTD avatar Oct 03 '25 17:10 WolleTD

Ok, on the one hand, this seems to be an issue with prost-build as it also happens when I use that directly.

On the other hand, prost-build shows this issue in both 0.13.5 and 0.14.1 and tonic-build somehow did not.

WolleTD avatar Oct 03 '25 19:10 WolleTD

Ah, I got it: tonic-build emitted cargo:rerun-if-changed= lines by itself, tonic-prost-build relies on prost-build to do that, but that change got reverted with prost-build 0.14.1. So we currently emit no rerun-if-changed lines and cargo reruns build.rs on any change in the project directory.

As a workaround, I can emit my own rerun-if-changed lines.

WolleTD avatar Oct 04 '25 08:10 WolleTD