Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skylark ctx.command doesn't incorporate runfiles from input executables; py_binary/java_binary executables fail #1192

Closed
dfabulich opened this issue Apr 25, 2016 · 3 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug untriaged

Comments

@dfabulich
Copy link
Contributor

dfabulich commented Apr 25, 2016

(I forked this bug off of issue #1136, because #1136 turned out to be a mix of several bugs.)

Consider this repository https://github.com/dfabulich/cannot-find-runfiles

There's a simple python script in build_tools/whatevah.py that accepts one CLI argument and writes a file to that location.

import sys

with open (sys.argv[1], "w") as OUT:
    OUT.write("hello world")

It's declared as a simple py_binary in build_tools/BUILD.

py_binary(
    name='whatevah',
    srcs=['whatevah.py'],
    visibility=['//visibility:public'],
)

There's a simple Skylark rule in build_tools/example.bzl; the example rule runs whatevah to generate a %{name}.txt file as output, but it uses the command parameter to ctx.action instead of the executable parameter.

def _impl(ctx):
    ctx.action(
        inputs = [ctx.executable._whatevah],
        outputs = [ctx.outputs.text],
        command = "{} {}".format(ctx.executable._whatevah.path, ctx.outputs.text.path),
    )

example = rule(
    implementation = _impl,
    attrs = {
        "_whatevah": attr.label(executable=True, allow_files=True, default=Label("//build_tools:whatevah"))
    },
    outputs = {
        "text": "%{name}.txt",
    }
)

Finally, foo/BUILD declares a foo target with the example rule.

load('/build_tools/example', 'example')

example(
    name='foo',
)

When I bazel build foo, I expect it to succeed and generate foo.txt, but instead I get this error:

$ bazel build foo --verbose_failures
INFO: Found 1 target...
ERROR: /private/tmp/repro/foo/BUILD:3:1: error executing shell command: 'bazel-out/local_darwin-fastbuild/bin/build_tools/whatevah bazel-out/local_darwin-fastbuild/bin/foo/foo.txt' failed: bash failed: error executing command
  (cd /private/var/tmp/_bazel_dfabulich/92410d0e3373f265902564b48493ac12/repro && \
  exec env - \
  /bin/bash -c 'bazel-out/local_darwin-fastbuild/bin/build_tools/whatevah bazel-out/local_darwin-fastbuild/bin/foo/foo.txt'): com.google.devtools.build.lib.shell.BadExitStatusException: Process exited with status 1.
Traceback (most recent call last):
  File "bazel-out/local_darwin-fastbuild/bin/build_tools/whatevah", line 110, in <module>
    Main()
  File "bazel-out/local_darwin-fastbuild/bin/build_tools/whatevah", line 66, in Main
    sys.argv[0])
AssertionError: Cannot find .runfiles directory for bazel-out/local_darwin-fastbuild/bin/build_tools/whatevah
Target //foo:foo failed to build
INFO: Elapsed time: 0.113s, Critical Path: 0.03s

As discussed in issue #1136, you can workaround this issue by using the executable parameter to ctx.action instead of command.

def _impl(ctx):
    ctx.action(
        executable = ctx.executable._whatevah,
        arguments = [ctx.outputs.text.path],
        outputs = [ctx.outputs.text],
    )
@dfabulich
Copy link
Contributor Author

dfabulich commented Apr 25, 2016

Beware, if you try to workaround this issue #1192 by converting your ctx.action's command into a sh_binary, you'll probably also hit issue #1147, in which sh_binary scripts have an incorrect working directory when run via Skylark.

@meteorcloudy meteorcloudy added P2 We'll consider working on this in future. (Assignee optional) category: skylark labels Apr 25, 2016
@dslomov dslomov added this to the 0.5 milestone Dec 8, 2016
@dslomov dslomov self-assigned this Dec 8, 2016
@dslomov dslomov modified the milestones: 0.6, 0.5 Dec 20, 2016
@laurentlb laurentlb assigned c-parsons and unassigned dslomov Nov 29, 2018
@laurentlb laurentlb removed this from the 0.6 milestone Nov 29, 2018
@jin jin added the type: bug label May 19, 2020
@brandjon brandjon added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Build-Language and removed P2 We'll consider working on this in future. (Assignee optional) team-Starlark labels Feb 15, 2021
@umap341
Copy link

umap341 commented Sep 23, 2022

Hi,

Is there a way now to invoke py_binary through skylark rule?

Thanks,

@brandjon brandjon added untriaged team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts and removed team-Build-Language labels Nov 4, 2022
@comius
Copy link
Contributor

comius commented Aug 16, 2023

This issue is pretty old and API has changed a lot.
ctx.actions.run incorporates runfiles when somebody uses ctx.executable.xxx in it. So I believe this is solved.

@comius comius closed this as completed Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug untriaged
Projects
None yet
Development

No branches or pull requests

9 participants