Search code examples
sandboxbazelrule

Disable sandbox in custom rule


How can I disable the sandbox in a custom Bazel rule?

I want the sandbox always disabled for every instantiation of this rule, without the user having to do anything.


Solution

  • When creating actions in the rule implementation, include the execution_requirements dict argument containing a no-sandbox key with a value of 1. This forces the action to never run in the sandbox.

    def _impl(ctx):
      ctx.actions.run_shell(
        outputs = [ctx.outputs.executable],
        command = "..",
        execution_requirements = {
          "no-sandbox": "1",
          "no-cache": "1",
          "no-remote": "1",
          "local": "1",
        },
      )
    

    See the tags attribute on the documentation for common build attributes for more information on these tags/requirements.