how bazel detects the file changes

31 views
Skip to first unread message

Saicharan Parasharam

unread,
Aug 22, 2025, 5:25:13 AM (14 days ago) Aug 22
to bazel-discuss
hello Group,
lets take the following example
rule custom(
name = "foo"
srcs=[foo.txt],
)
rule custom(
name="bar",
srcs = ["bar.txt"],
deps = [":foo"]
)

I observed that bar or foo wont be out of date, if there is a change to input files unless I use actions.run_shell or actions.run which take the input files as inputs, am I missing anything?

my use case is that I want to write a file when I build a target, so I used ctx.action.write but this dosen't track the file changes. how to make sure that bazel tracks the input files given to a target and any change in those files make the targets which consume that file as out-of-date

David Turner

unread,
Aug 22, 2025, 12:38:50 PM (14 days ago) Aug 22
to Saicharan Parasharam, bazel-discuss
On Fri, Aug 22, 2025 at 11:25 AM Saicharan Parasharam <charan...@gmail.com> wrote:
hello Group,
lets take the following example
rule custom(
name = "foo"
srcs=[foo.txt],
)
rule custom(
name="bar",
srcs = ["bar.txt"],
deps = [":foo"]
)

I observed that bar or foo wont be out of date, if there is a change to input files unless I use actions.run_shell or actions.run which take the input files as inputs, am I missing anything?

It's unclear what the implementation function for rule_custom does, but it should generate actions that take foo.txt and bar.txt as inputs (respectively) and output something.
Something that looks the following, where [action 1] is generated by your first target definition, and [action 2] is generated by your second one.

    foo.txt
       |
       v
   [action 1]
       |
       v
   foo.out    bar.txt
        \      /
         \    /
          v  v
       [action 2]
           |
           v
        bar.out

The action graph (built by running rule implementation functions) is what Bazel uses to determine whether to rebuild artifacts or not. It uses input/output chains between actions to determine which need to run, and in which order. Besides, it an action is run, but generates the same output as before, the output is considered up-to-date and its downstream actions will not be re-run.

Also each implementation rule must return a DefaultInfo value which corresponds to the outputs to generate when you do something like `bazel build //src:foo` or `bazel build //src:bar`.
 
my use case is that I want to write a file when I build a target, so I used ctx.action.write but this dosen't track the file changes. how to make sure that bazel tracks the input files given to a target and any change in those files make the targets which consume that file as out-of-date

It's unclear what you mean "write a file when I build a target". I ctx.action.write() action has no input at all, so will not be re-run in foo.txt or bar.txt is modified.

Since writing a file is an action, and "building a target" would be another action. The only way they can interact is through input/output dependencies.
You would need the output of your second target to be the input to your "write file" action. In the example above that would be something like:

      foo.txt
         |
         v
     [action 1]
         |
         v
     foo.out    bar.txt
       /  \      /
      /    \    /
     /      v  v
    /     [action 2]
 [write 1]    |    \
    |         |     \
    v         v      \

foo.stamp   bar.out   v
                    [write 2]
                      |
                      v
                     bar.stamp
  

Where [write 1] and [write 2] are the new writing actions that output stamp files whenever foo.out is modified (not rebuilt, because Bazel uses content hashes aggressively to avoid rebuilding stuff).
There is probably very little value in these stamp files (just use foo.out and bar.out directly instead).

If you want to only write a file when an action is run, independent from whether its output was modified or not, I don't think this is possible in general.

Hope this helps,

- Digit
--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/bazel-discuss/4e101a77-37a3-4a74-b970-1cd88f068f4dn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages