bazelbuild / bazel

a fast, scalable, multi-language and extensible build system
https://bazel.build
Apache License 2.0
22.99k stars 4.03k forks source link

'no-cache' fails in cc_binary and execution_requirements #18208

Open YeahhhhLi opened 1 year ago

YeahhhhLi commented 1 year ago

Description of the bug:

First let me describe my usage scenario.

I want to customize a filegroup action, the purpose is to save the filegroup data in each scattered directory in the project to a specified directory, custom_file_group.bzl like this:

def _custom_filegroup_impl(ctx):
    all_input_targets = [
        target for target in ctx.attr.srcs
    ]
    for target in all_input_targets:
        for file in target.files.to_list():
            print("file_path[%s]" % (file.path))
            # ctx.actions.run_shell(xxxx)

_custom_filegroup = rule(
    implementation = _custom_filegroup_impl,
    attrs = {
        "srcs": attr.label_list(allow_files = True),
    },
)

def custom_filegroup(name, **kwargs):
  _custom_filegroup(
    name = name,
    **kwargs)

But due to the existence of the cache, it can only be saved during the first build, and I expect custom_filegroup will never be cached, that is, it is guaranteed to be copied every time.

Later, I tried some methods based on the documentation and previous issues:

  1. add common --experimental_allow_tags_propagation in bazelrc and add tags = ["no-cache"] in cc_binary or filegroup
  2. add execution_requirements = { "no-cache": "1", } in ctx.actions.run_shell Neither of these methods worked

What's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

  1. Directory hierarchy of my test project:

    .
    ├── BUILD
    ├── config
    │   ├── config1.txt
    │   └── config2.txt
    ├── custom_filegroup.bzl
    ├── dir1
    │   └── dir2
    │       └── dir3
    │           ├── BUILD
    │           ├── file1.txt
    │           └── file2.txt
    ├── file00.txt
    ├── file01.txt
    ├── [main.cc](http://main.cc/)
    ├── params
    │   ├── params1.txt
    │   └── params2.txt
    └── WORKSPACE
  2. custom_filegroup.bzl

    
    def _custom_filegroup_impl(ctx):
    all_input_targets = [
        target for target in ctx.attr.srcs
    ]
    for target in all_input_targets:
        for file in target.files.to_list():
            print("file_path[%s]" % (file.path))
            # ctx.actions.run_shell(xxxx)
    pass

_custom_filegroup = rule( implementation = _custom_filegroup_impl, attrs = { "srcs": attr.label_list(allow_files = True), }, )

def custom_filegroup(name, kwargs): _custom_filegroup( name = name, kwargs)


3. ./BUILD
```txt
load(
    "//:custom_filegroup.bzl",
    "custom_filegroup",
)

cc_binary(
    name = "main",
    srcs = ["main.cc"],
    data = [
        "//dir1/dir2/dir3:nested_files",
        ":test_files",
    ],
    tags = ["no-cache"],
)

custom_filegroup(
    name = "test_files",
    srcs = glob([
        "*.txt",
        "config/*.txt",
        "params/*.txt",
    ]),
    tags = ["no-cache"],
)
  1. ./dir1/dir2/dir3/BUILD
    
    load(
    "//:custom_filegroup.bzl",
    "custom_filegroup",
    )

package( default_visibility = ["//visibility:public"], )

custom_filegroup( name = "nested_files", srcs = glob([ "*.txt", ]), )

5. ./.bazelrc
```txt
common --experimental_allow_tags_propagation

First I run bazel build main:

DEBUG: /custom_filegroup.bzl:7:18: file_path[dir1/dir2/dir3/file1.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[dir1/dir2/dir3/file2.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[config/config1.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[config/config2.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[file00.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[file01.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[params/params1.txt]
DEBUG: /custom_filegroup.bzl:7:18: file_path[params/params2.txt]
INFO: Analyzed target //:main (37 packages loaded, 165 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 0.361s, Critical Path: 0.16s
INFO: 6 processes: 4 internal, 2 linux-sandbox.
INFO: Build completed successfully, 6 total actions

And then I run bazel build main again:

INFO: Analyzed target //:main (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:main up-to-date:
  bazel-bin/main
INFO: Elapsed time: 0.049s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

Which operating system are you running Bazel on?

Ubuntu 20.04

What is the output of bazel info release?

release 6.1.2

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

  1. no-cache doc: https://bazel.build/reference/be/common-definitions#common.tags
  2. no-cache invalid issue: https://github.com/bazelbuild/bazel/issues/6038

Any other information, logs, or outputs that you want to share?

No response

YeahhhhLi commented 1 year ago

@Pavank1992 Do you have any conclusions or suggestions?

Pavank1992 commented 1 year ago

Hi @YeahhhhLi,

Thanks for being patience. We have shared your request with concern team. They will get back to you soon.

github-actions[bot] commented 2 months ago

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.