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

Difference between files and runfiles providers is unclear #4519

Closed
jmillikin-stripe opened this issue Jan 24, 2018 · 10 comments
Closed

Difference between files and runfiles providers is unclear #4519

jmillikin-stripe opened this issue Jan 24, 2018 · 10 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: documentation (cleanup)

Comments

@jmillikin-stripe
Copy link
Contributor

Description of the problem / feature request

Given the following three files (BUILD, rule.bzl, the_test.sh) I would expect the output of the test to include the generated data file. However, the output does not -- it seems like files included in the DefaultInfo provider of a Skylark rule do not get used in the sh_test data or runfiles sets.

@dslomov's comment on an ancient issue from 2016 (#1305 (comment)) implies this used to work, or at least is expected to work. However, it doesn't seem to in contemporary Bazel versions.

### BUILD
load(":rule.bzl", "custom_rule")
custom_rule(name="the_data")
sh_test(
    name = "the_test",
    srcs = ["the_test.sh"],
    data = [":the_data"],
)
### rule.bzl
def _impl(ctx):
  out = ctx.actions.declare_file(ctx.label.name + "/out_file")
  ctx.actions.write(out, "data")
  return [DefaultInfo(files=depset([out]))]

custom_rule = rule(implementation=_impl)
### the_test.sh
#!/bin/bash
find .
exit 1
$ bazel test //:the_test --test_output=errors
INFO: Analysed target //:the_test (0 packages loaded).
INFO: Found 1 test target...
FAIL: //:the_test (see /private/var/tmp/_bazel_jmillikin/21ae14480478de0e5d7efd7aa0b3e856/execroot/__main__/bazel-out/darwin-fastbuild/testlogs/the_test/test.log)
INFO: From Testing //:the_test:
==================== Test output for //:the_test:
.
./the_test
./the_test.sh
================================================================================

What operating system are you running Bazel on?

MacOS

What's the output of bazel info release?

release 0.9.0-homebrew
@sergiocampama
Copy link
Contributor

Shouldn't these be propagated using the runfiles provider?

return struct(
      runfiles=ctx.runfiles(
          files=depset([out]),
      ),
  )

@benjaminp
Copy link
Collaborator

I think this has always been the behavior. At least, I can't see that this behavior has changed recently looking at the code.

@laurentlb
Copy link
Contributor

cc @buchgr

@szakmary
Copy link
Contributor

Still seems to be relevant in version 3.4.0.

@JaredNeil
Copy link
Contributor

This keeps biting me. It seems very strange to me, that a rule that generates a generic file has to provide it as both files and runfiles, just in the off-chance that sometime later a sh_binary might want to use a target of that rule type as a data input. I keep having to go edit upstream rules to provide runfiles even though they aren't generating a binary, which just seems wrong.
It also affects py_binary, which is where I usually run into it.

@brandjon
Copy link
Member

For the difference between "files" ("FilesToBuild" in the Java code) and "runfiles", see this comment. Perhaps we could elevate that information to documentation, if not already present. I'll admit, I was confused at first by the example.

One could also interpret this issue as a feature request to treat the runfiles as being, by default if not specified, the same as the files.

@brandjon brandjon changed the title sh_test won't build/use data files defined by Skylark returning DefaultInfo(files=[...]) Difference between files and runfiles providers is unclear Feb 16, 2021
@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 team-Starlark untriaged labels Feb 16, 2021
@q3k
Copy link

q3k commented Mar 8, 2021

We just also hit this (target using a custom rule outputting DefaultInfo(files=...) that could not be consumed by an sh_test specifying that target in its data). The documentation on this is a bit sparse and the behavior is quite confusing at first.

I think the confusion mostly comes from an assumption that runfiles in DefaultInfo means “anything that files=[...] will need if it's ever ran” - or at least that was my intepretation of it, and the documentation didn't contradict me on that. As in, if you output a binary foo.elf which wants to access some runtime stuff, foo.elf should live in files= and all its runtime deps should live in runfiles=. I've blissfully lived in this wrong interpretation for years.

In reality, runfiles= seems to mean “anything that is required to run any other target that depends on this target as its runfiles, including the actual output itself, as non-runfiles will not be included”. Or, in other words, that runfiles is used when the target is depended on as runfiles/data, while files is used when the target is used as a build input. Which is a fair explanation, but should IMO be more explicitly stated.

In addition, maybe some code changes could be made to make this a bit more obvious - maybe if a target's output is directly included as the runfiles of another target, Bazel should warn when there is a direct inclusion of an output with empty DefaultInfo.runfiles, and that is probably not what the user meant? Defaulting to runfiles=files if not set otherwise is tempting, but I'd be afraid of breakage this would cause. And it would probably muddle the distinction even further.

@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
@sgowroji sgowroji added the team-Documentation Documentation improvements that cannot be directly linked to other team labels label Jan 11, 2023
@uri-canva
Copy link
Contributor

#15052 changes the behaviour to reflect the understanding that a lot of people had, that runfiles are additional files needed at runtime by the files declared as regular outputs, so files that are declared as default outputs shouldn't be declared in runfiles.

@fmeum
Copy link
Collaborator

fmeum commented Mar 14, 2023

As @uri-canva explained, this has been fixed by #15052, which is in Bazel 6. Just a nitpick: "... files that are declared as default outputs shouldn't be declared in runfiles." should be "... files that are declared as default outputs don't have to be declared in runfiles."

@sgowroji Could you close this issue as completed?

@sgowroji
Copy link
Member

We are closing this issue w.r.t above comment. Feel free to reach us for any further queries. Thanks !

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-Documentation Documentation improvements that cannot be directly linked to other team labels team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: documentation (cleanup)
Projects
None yet
Development

No branches or pull requests