Search code examples
bazelbazel-rulesbazel-aspect

Why `ctx.actions.run` cannot refer `generated file type` files as a `inputs` attribute even though `source file type` files can be referred?


I'm creating a rules file that generate some scripts with ctx.actions.expand_template and runs these scripts with ctx.actions.run.

ctx.actions.run uses the script file generated by ctx.actions.expand_template and the 'generated file type' file(filelist file contains several file name, path information) generated from other rule files which has a dependency relationship with this rule file as input attribute.

When the script is executed in ctx.actions.run, the generated file type filelist mentioned above is not found. If I check the sandbox path where the actual build takes place, this filelist does not exist.

What should I do?

This is a part of my rule file

def _my_rule_impl(ctx):
...
    my_script = ctx.actions.declare_file("my_script.sh")
    ctx.actions.expand_template(
        output = compile_script,
        template = ctx.file._my_template,
        substitutions = {
            "{TOP}": "{}".format(top_name),
            "{FLISTS}": " ".join(["-f {}".format(f.short_path) for f in flists_list]),
            ...
        },
    )

    compile_srcs = flists_list + srcs_list + [my_script]
    outputs = ctx.outputs.executable
    executable = compile_script.path

    ctx.actions.run(
        inputs = depset(compile_srcs),
        outputs = [outputs],
        executable = executable,
        env = {
            "HOME": "/home/grrrr",
        },
    )

    allfiles = depset(compile_srcs)
    runfiles = ctx.runfiles(files = compile_srcs)

    return [DefaultInfo(
        files = allfiles,
        runfiles = runfiles,
    )]

my_rule = rule(
    implementation = _my_rule_impl,
    attrs = {
        "deps": attr.label_list(
            mandatory = True,
        ),
        "_my_template": attr.label(
            allow_single_file = True,
            default = Label("@my_rules//my_test:my_script.sh.template"),
        ),
        ...
    },
    executable = True,
)  

As a result of checking with print, this path is the location where the script is executed.

/home/grrrr/.cache/bazel/_bazel_grrrr/.../sandbox/processwrapper-sandbox/.../execroot/my_rules/

As a result of checking with print, the script refers to sources including a filelist in this path. However, there are only source file type files. There is not a filelist.

/home/grrrr/.cache/bazel/_bazel_grrrr/.../sandbox/processwrapper-sandbox/.../execroot/my_rules/my_test

However, There is a filelist in this path. I'm wondering why this filelist is not in above directory.

/home/grrrr/.cache/bazel/_bazel_grrrr/.../sandbox/processwrapper-sandbox/.../execroot/my_rules/bazel-out/k8-fastbuild/bin/my_test

Solution

  • It's resolved by using sandboxfs instead of sandbox.

    Here is the useful page regarding sandboxfs.