How to unconditionally execute an action?

623 views
Skip to first unread message

sbag...@grailbio.com

unread,
Feb 2, 2019, 7:27:13 PM2/2/19
to bazel-discuss
Hello,

I have an action that captures some system state like version of installed tools, etc. My rules then depend on this state so that I can invalidate the cache for builds when the system state has changed.

How do I make this action run unconditionally on every build without using the local action cache?

I looked into repository_rule and even the `local` attribute there does not give me unconditional execution. I know that people depend on environment variables to capture system state, but I really do want to execute a few commands to get version information, etc.

The other option I know is to include my state in bazel's workspace_status, but that would invalidate cache for targets of all rule types that depend on the volatile or stable status files, and would also slow down every bazel invocation, not just the ones that build targets of my rule type.

Thanks.

Sid

Jochen Issing

unread,
Feb 3, 2019, 2:02:11 AM2/3/19
to sbag...@grailbio.com, bazel-discuss
You mean like a phony target (in Makefile language) similar to what the build status does? We did a little hack here, which only works on linux platforms: we depend on /proc/uptime.

Maybe that helps :)

/jochen

--


This email message, including attachments, may contain private,
proprietary, or privileged information and is the confidential information
and/or property of GRAIL, Inc., and is for the sole use of the intended
recipient(s). Any unauthorized review, use, disclosure or distribution is
strictly prohibited. If you are not the intended recipient, please contact
the sender by reply email and destroy all copies of the original message.

--
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 on the web visit https://groups.google.com/d/msgid/bazel-discuss/afdc0c35-6022-4220-8d9f-9f202d2a7ecd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Siddhartha Bagaria

unread,
Feb 3, 2019, 2:25:24 AM2/3/19
to Jochen Issing, bazel-discuss
Oh, that's a neat trick! But it won't work without the proc filesystem. More specifically, it won't work on darwin for us. I tried, and it definitely works on linux.

I found this field on spawn actions, but no way to set it... :-(

Jochen Issing

unread,
Feb 3, 2019, 6:39:53 AM2/3/19
to Siddhartha Bagaria, bazel-discuss
That’s interesting and maybe could lead to a correctly designed solution. Another one is actually in place for the build info, but I haven’t checked the sources. 

Thinking of a multi-platform hack: you coukd have a genrule creating some random content (similar to /dev/random but limited to some bytes, e.g. dd if=/dev/random count=20...) and depend on the outcome of that rule. Something like that could work cross platform. But it will still be a hack ofc.

:)

Cheers, jochen

Andrew Suffield

unread,
Feb 3, 2019, 7:07:44 AM2/3/19
to Jochen Issing, Siddhartha Bagaria, bazel-discuss
It sounds like what you're asking for is a more generic version of workspace_status, which is controlled via starlark. That's an interesting idea, although it doesn't exist today.

Siddhartha Bagaria

unread,
Feb 3, 2019, 7:55:36 AM2/3/19
to Andrew Suffield, Jochen Issing, bazel-discuss
The problem with the genrule is also that once built, it will be cached.

I don't think there is a way right now to mark an action to be unconditionally executed. I thought that the 'no-cache' key in execution_requirements will be enough, but it does not apply to the action cache.

sbag...@grailbio.com

unread,
Feb 22, 2020, 11:28:15 PM2/22/20
to bazel-discuss
Bumping up this thread again. Have there been any developments that allow unconditional execution of an action without using hacks like depending on /proc/uptime?

Thanks.

Jared Neil

unread,
Feb 24, 2020, 2:55:30 AM2/24/20
to bazel-discuss
This seem like exactly what workspace status command is for. https://docs.bazel.build/versions/2.0.0/user-manual.html#flag--workspace_status_command

Can you explain why that doesn't work for you?

sta...@gmail.com

unread,
Feb 24, 2020, 3:23:03 AM2/24/20
to bazel-discuss
Using the workspace status command for this purpose, i.e. changing the stable status vars, will unconditionally execute all actions that ingest the stable status file. I want only one specific action to unconditionally execute.

Jared Neil

unread,
Feb 24, 2020, 2:37:45 PM2/24/20
to bazel-discuss
Ah, yes. We got around this by writing a genrule that extracts just the variables we care about for later actions.

workspace_status.sh:
echo "STABLE_TOOLS $(determine-tool-versions)"

BUILD.bazel:
genrule(
    name
= "tool-versions",
    outs
= ["tool-versions.txt"],
    cmd
= "grep '^STABLE_TOOLS' bazel-out/stable-status.txt > $@",
    stamp
= 1,
)

Then all later actions depend on the :tool-versions target, and will only run if tool-versions.txt actually changed

Siddhartha Bagaria

unread,
Feb 24, 2020, 4:38:13 PM2/24/20
to bazel-discuss
Aah! Can't do that either. There are many 3p rules like rules_go, rules_docker, etc. that consume the entire file and are not within my control.

Jingwen Chen

unread,
Feb 24, 2020, 4:41:14 PM2/24/20
to Siddhartha Bagaria, bazel-discuss
> I looked into repository_rule and even the `local` attribute there does not give me unconditional execution.  I know that people depend on environment variables to capture system state, but I really do want to execute a few commands to get version information, etc.

You can create a repository_rule and mark it as `configure`. Then, before every build, run `bazel sync --configure`. This will trigger a forced evaluation of the rule. Then, if the rule generates a different BUILD file, your downstream targets will be automatically invalidated.

This email message, including attachments, may contain private, proprietary, or privileged information and is the confidential information and/or property of GRAIL, Inc., and is for the sole use of the intended recipient(s). Any unauthorized review, use, disclosure or distribution is strictly prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message.

--
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.

Siddhartha Bagaria

unread,
Feb 26, 2020, 2:58:54 PM2/26/20
to bazel-discuss
Thanks. This may work out. I will have to redesign some of the internals in my rules, because I was using a `rule` and not a `repository_rule`. But it could be OK if it goes with the broader story of how users are expected to tell bazel to re-read the system state, i.e. `bazel sync --configure`.
Reply all
Reply to author
Forward
0 new messages