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

Starlark runfiles merging silently drops --legacy_external_runfiles external runfiles links #17415

Open
rickeylev opened this issue Feb 3, 2023 · 2 comments
Labels
P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) team-Core Skyframe, bazel query, BEP, options parsing, bazelrc team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug

Comments

@rickeylev
Copy link
Contributor

rickeylev commented Feb 3, 2023

(Mostly filing this for posterity, as its a subtle behavior change that can be hard to debug)

Description of the bug:

When merging runfiles with the Starlark APIs (e.g. ctx.runfiles().merge() or ctx.runfiles().merge_all()), certain invocations will silently drop the "external/" paths that --legacy_external_runfiles (default true) creates.

ctx.runfiles(files) # keeps external
ctx.runfiles().merge(ctx.runfiles(files)) # keeps external
ctx.runfiles(files).merge(ctx.runfiles()) # keeps external

# The buggy call
ctx.runfiles(files).merge(ctx.runfiles(files)) #drops external

Looking at Runfiles.java, I see legacyExternalRunfiles is hard-coded to false in various spots.

I think the generalized case is: merging any two non-empty runfiles will drop legacyExternalRunfiles. This is because the Runfiles.Builder ctor argument legacyExternalRunfiles determines the final value (i.e. any intermediate
Runfiles objects being merged in don't have their legacyExternalRunfiles value considered).

The Runfiles.merge() logic is this:

def merge(this, other):
  if this is empty: return other
  if other is empty: return this

  builder = Builder(legacy_external_runfiles=False)  # We now have lost the setting
  builder = builder.merge(this)
  builder = builder.merge(other)
  return builder.build()

The Runfiles.mergeAll logic is harder to follow, but I think basically the same: all its builder calls force legacyExternalRunfiles=false.

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

# WORKSPACE
local_repository(
  name = "repo2",
  path = "repo2"
)
EOF

# repo2/BUILD
filegroup(name="r2files", srcs=["r2.txt"])

# BUILD
load(":foo.bzl", "foo_binary")
foo(name="foo", data=["@repo2//:r2files"])

# foo.bzl
def impl(ctx):
  files = ctx.files.data
  rf = ctx.runfiles(files).merge(ctx.runfiles(files)
  return [DefaultInfo(runfiles=rf)]

foo = rule(implementation=impl, attrs={"data": attr.label_list()})

# Run
bazel build //:foo
find bazel-bin/foo

Expected: the find command lists external/repo2/r2.txt

Actual: It does not

Which operating system are you running Bazel on?

Linux

What is the output of bazel info release?

dev

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

bazel build //src:bazel-dev

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

git@github.com:rickeylev/bazel.git
fd9bc99165104c41d19b16f3ef2ce8d48cbd330d
6172922d4f8f357393bdca435f7f0d4b5331dd51

Have you found anything relevant by searching the web?

flag flip for legacy_external_runfiles: #12821

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

No response

@rickeylev
Copy link
Contributor Author

Oh, I forgot add: IMHO this should just be left alone. The goal is to switch to --legacy_external_runfiles=false, right? This bug effectively means that Starlark code is effectively acting that way already -- in order to not trigger this bug, you have to never merge with another runfiles through Starlark.

It's probably just native rules being rewritten to Starlark that are affected, since they were able to use Java APIs that propagated legacy_external_runfiles.

@ShreeM01 ShreeM01 added type: bug untriaged team-Core Skyframe, bazel query, BEP, options parsing, bazelrc labels Feb 3, 2023
@rickeylev rickeylev changed the title Particular runfiles merge calls silently drop --legacy_external_runfiles external runfiles links Starlark runfiles merging silently drops --legacy_external_runfiles external runfiles links Feb 3, 2023
copybara-service bot pushed a commit that referenced this issue Feb 6, 2023
This was found by way of the upb downstream tests, which currently rely on the
legacy external runfiles format for runfiles (this is when files that come from
other repositories are put in `$runfilesRoot/$mainRepo/external/$otherRepo`).

What happens is a regular `runfiles.merge(other)` call will, under the hood,
create a new Runfiles.Builder object with legacyExternalRunfiles=false, thus
losing the value from the flag. To fix, we just have to create a builder with
the value from the flag so that the created Runfiles object keeps it.

Workaround for #17415

PiperOrigin-RevId: 507503578
Change-Id: I961f50f73ae1ee9980da92eff1b333cbc3b82104
@haxorz haxorz added the team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts label Feb 6, 2023
hvadehra pushed a commit that referenced this issue Feb 14, 2023
This was found by way of the upb downstream tests, which currently rely on the
legacy external runfiles format for runfiles (this is when files that come from
other repositories are put in `$runfilesRoot/$mainRepo/external/$otherRepo`).

What happens is a regular `runfiles.merge(other)` call will, under the hood,
create a new Runfiles.Builder object with legacyExternalRunfiles=false, thus
losing the value from the flag. To fix, we just have to create a builder with
the value from the flag so that the created Runfiles object keeps it.

Workaround for #17415

PiperOrigin-RevId: 507503578
Change-Id: I961f50f73ae1ee9980da92eff1b333cbc3b82104
@comius comius added P4 This is either out of scope or we don't have bandwidth to review a PR. (No assignee) and removed untriaged labels Aug 22, 2023
@comius
Copy link
Contributor

comius commented Aug 22, 2023

P4, I share the opinion to leave this alone :)

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-Core Skyframe, bazel query, BEP, options parsing, bazelrc team-Rules-API API for writing rules/aspects: providers, runfiles, actions, artifacts type: bug
Projects
None yet
Development

No branches or pull requests

4 participants