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

@ and @workspacename considered different repositories #3115

Closed
jayconrod opened this issue Jun 2, 2017 · 45 comments
Closed

@ and @workspacename considered different repositories #3115

jayconrod opened this issue Jun 2, 2017 · 45 comments
Assignees
Labels
P1 I'll work on this now. (Assignee required)

Comments

@jayconrod
Copy link
Contributor

Description of the problem / feature request / question:

Bazel evaluates the same Skylark file multiple times if it is loaded with a local path and an absolute path. This creates problems for rules, since everything gets defined twice. It is particularly problematic for named providers, since the duplicate definitions are neither equivalent nor compatible.

We ran into this issue when trying to migrate rules_go to use named providers. We load our .bzl files with local labels in our own repository, but repositories that depend on ours load using absolute labels, which causes errors.

Bazel should not evaluate the same .bzl file in the same repository more than once.

If possible, provide a minimal example to reproduce the problem:

Create an empty workspace with these files.

# WORKSPACE
workspace(name = "scratch")
# BUILD
load("//:def.bzl", "a")
load("@scratch//:def.bzl", "a")
# def.bzl
print("def.bzl evaluated")
a = 10

Run bazel query //... to evaluate the files. The message is printed twice.

$ bazel query //...
____Loading package: 
WARNING: /usr/local/google/home/jayconrod/Code/scratch/def.bzl:1:1: def.bzl evaluated.
WARNING: /usr/local/google/home/jayconrod/.cache/bazel/_bazel_jayconrod/1df8d8ad4cb35ee83522d81953b4d69f/external/scratch/def.bzl:1:1: def.bzl evaluated.
____Empty results

Environment info

  • Operating System: Linux
  • Bazel version (output of bazel info release): 0.5.0

cc @ianthehat

ianthehat added a commit to bazel-contrib/rules_go that referenced this issue Jun 2, 2017
Change all internal load statements to absolute paths otherwise we end up with
duplicate instances of the scripts, with thing like providers of the same name
that don't match.
This is a workaround for bazelbuild/bazel#3115
@jwnimmer-tri
Copy link
Contributor

This is acutely annoying for providers, since it both breaks things, and gives bad diagnostics ("does not have mandatory providers Foo" even though it has "Foo", just a different foo). If it is difficult to fix, it might be worth at least failing-fast in the meantime, even if only for providers.

@ittaiz
Copy link
Member

ittaiz commented May 16, 2018

@laurentlb this P1 will celebrate anniversary soon.
Is there an update here? I'm not hitting this exact issue but I think I have an adjacent problem (same target gets evaluated twice, once as external and once as internal)

@laurentlb
Copy link
Contributor

Klaus, are you aware of this issue?
I don't know exactly at which level we need to fix this (so assigning to both Jon and Klaus).

@brandjon
Copy link
Member

FYI if it's a local and absolute path within the same repo ("//" vs ":") it doesn't reproduce, so I believe it has to be a repository-specific issue.

@brandjon brandjon removed their assignment May 25, 2018
@jayconrod
Copy link
Contributor Author

Labels that include the repository name are still treated as different though, i.e., in the example above, @scratch//:def.bzl is treated as different from //:def.bzl, even they they refer to the same file.

This is an issue with rules and named providers. It's easy to get two incompatible copies of something with the same name.

@ittaiz
Copy link
Member

ittaiz commented May 25, 2018 via email

@jayconrod
Copy link
Contributor Author

@ittaiz In rules_go, we work around this by always loading .bzl files using absolute labels that include the repo name. So we always load @io_bazel_rules_go//go:def.bzl even from inside the repository.

@ittaiz
Copy link
Member

ittaiz commented May 25, 2018 via email

dhalperi referenced this issue in batfish/batfish May 31, 2018
* BUILD: use absolute load statements

Seems like this is a standard workaround:

  https://github.com/bazelbuild/bazel/issues/3115\#issuecomment-392072493
@aehlig
Copy link
Contributor

aehlig commented Jun 5, 2018

Two remarks.

  • The problem is the @scratch. Loading @//:def.bzl and //:def.bzl evaluates the file only once. So it seems that @ and @scratch are treated as different repositories.
  • That also fits with the debug message: the direct location of the file, and in bazel's output base under external/scratch---as if @scratch where an external repository (of type local_repository) located at the current working directory.

Added @dkelmer, as this has to be considered as part of the label renaming project as well.

@aehlig aehlig changed the title Bazel evaluates the same Skylark file multiple times @ and @workspacename considered different repositories Jun 5, 2018
@aehlig
Copy link
Contributor

aehlig commented Jun 5, 2018

A better way to demonstrate that @scratch is really treated as a different local_repository is shown below. Note that not only another action is carried out when building @scratch//:it after having built @//:it, but it also has a different output file.

/memfs/tmp/work>cat WORKSPACE 
workspace(name="scratch")
/memfs/tmp/work>cat BUILD 
genrule(
  name = "it",
  outs = ["it.txt"],
  cmd = "echo hello > $@",
)
/memfs/tmp/work>bazel clean --expunge
Starting local Bazel server and connecting to it...
........
(12:13:55) INFO: Starting clean.
/memfs/tmp/work>bazel build -s @//...
Starting local Bazel server and connecting to it...
........
(12:14:10) INFO: Current date is 2018-06-05
(12:14:10) INFO: Analysed target //:it (8 packages loaded).
(12:14:10) INFO: Found 1 target...
(12:14:11) SUBCOMMAND: # //:it [action 'Executing genrule //:it']
(cd /crypto/general/aehlig/.cache/bazel/_bazel_aehlig/ea3a7c72d5e8b43a0d4e9c691a291e8f/execroot/scratch && \
  exec env - \
    PATH=/home/aehlig/bin:/usr/local/bin:/home/aehlig/bin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/aehlig/bin:/sbin:/usr/sbin:/sbin:/usr/sbin \
  /usr/local/bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; echo hello > bazel-out/freebsd-fastbuild/genfiles/it.txt')
Target //:it up-to-date:
  /crypto/general/aehlig/.cache/bazel/_bazel_aehlig/ea3a7c72d5e8b43a0d4e9c691a291e8f/execroot/scratch/bazel-out/freebsd-fastbuild/genfiles/it.txt
(12:14:11) INFO: Elapsed time: 0.959s, Critical Path: 0.07s
(12:14:11) INFO: 1 process: 1 processwrapper-sandbox.
(12:14:11) INFO: Build completed successfully, 2 total actions
/memfs/tmp/work>bazel build -s @scratch//...
(12:14:27) INFO: Current date is 2018-06-05
(12:14:27) INFO: Analysed target @scratch//:it (1 packages loaded).
(12:14:27) INFO: Found 1 target...
(12:14:27) SUBCOMMAND: # @scratch//:it [action 'Executing genrule @scratch//:it']
(cd /crypto/general/aehlig/.cache/bazel/_bazel_aehlig/ea3a7c72d5e8b43a0d4e9c691a291e8f/execroot/scratch && \
  exec env - \
    PATH=/home/aehlig/bin:/usr/local/bin:/home/aehlig/bin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:/home/aehlig/bin:/sbin:/usr/sbin:/sbin:/usr/sbin \
  /usr/local/bin/bash -c 'source external/bazel_tools/tools/genrule/genrule-setup.sh; echo hello > bazel-out/freebsd-fastbuild/genfiles/external/scratch/it.txt')
Target @scratch//:it up-to-date:
  /crypto/general/aehlig/.cache/bazel/_bazel_aehlig/ea3a7c72d5e8b43a0d4e9c691a291e8f/execroot/scratch/bazel-out/freebsd-fastbuild/genfiles/external/scratch/it.txt
(12:14:27) INFO: Elapsed time: 0.199s, Critical Path: 0.04s
(12:14:27) INFO: 1 process: 1 processwrapper-sandbox.
(12:14:27) INFO: Build completed successfully, 2 total actions
/memfs/tmp/work> 

Profpatsch added a commit to tweag/rules_haskell that referenced this issue Nov 22, 2018
Adds a `provides` field to haskell_library and haskell_binary to tell
users about our custom providers (and possibly throw better error
messages).

As a related change, the `load` targets for `providers.bzl` are made
absolute to work around
bazelbuild/bazel#3115
Profpatsch added a commit to tweag/rules_haskell that referenced this issue Nov 22, 2018
Adds a `provides` field to haskell_library and haskell_binary to tell
users about our custom providers (and possibly throw better error
messages).

As a related change, the `load` targets for `providers.bzl` are made
absolute to work around
bazelbuild/bazel#3115
@sergiocampama
Copy link
Contributor

I'm still seeing these issues in Bazel 0.25.1. Was the --incompatible_remap_main_repo flag made the default?

@dhalperi
Copy link
Contributor

dhalperi commented May 9, 2019

@sergiocampama - yes, experimental_enable_repo_mapping has been the default behavior for a while, and the flag has even been removed in 0.24.0: c32c334

@sergiocampama
Copy link
Contributor

what about

? It is also a noop? Or was the experimental flag migrated to incompatible and never flipped?

@sergiocampama
Copy link
Contributor

I'm seeing the exact provider missing error described in this issue in https://buildkite.com/bazel/rules-apple-darwin/builds/827#5ef4767f-56d7-48b6-a07e-992c9fa26db9

@katre
Copy link
Member

katre commented May 9, 2019

The flag is now --incompatible_remap_main_repo: https://source.bazel.build/bazel/+/master:src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java;l=503?q=main_repo

It was scheduled to be flipped but there was missing functionality, and it was not made default in 0.26.

See the tracking issue: #7130.

@sergiocampama
Copy link
Contributor

Shouldn't this issue be open then until the incompatible flag is flipped?

@sergiocampama sergiocampama reopened this May 9, 2019
natansil pushed a commit to wix-incubator/exodus that referenced this issue May 16, 2019
…kspace names (#613)

bazelbuild/bazel#3115

GitOrigin-RevId: 32e685aa3cd32eae15938e9c0f6180ef4a19175c
aiuto added a commit to aiuto/bazel-skylib that referenced this issue Aug 21, 2019
@bazel_skylib. This works around not having flipped the
--incompatible_remap_main_repo flag. If that gets done
we can unwind this change.

The exact problem we are working around is namespace resolution
for providers which are global. Without the main repo remapping,
we see the equivalent of '//StarlarkLibraryInfo' from within
this library, but Stardoc sees '@bazel_skylib//StarlarkLibraryInfo'.

bazelbuild/bazel#3115
bazelbuild/bazel#7130
@aehlig
Copy link
Contributor

aehlig commented Aug 23, 2019

Interestingly, the issue is not fully solved by (the current implementation of) --incompatible_remap_main_repo (#7130). Consider the following test cases

The first test passes, whereas the second fails in the build with remapping; interestingly enough, that command passes if we build //:y instead of @foo//:y.

bazel-io pushed a commit that referenced this issue Sep 25, 2019
...by taking the applicable repository mapping into account, instead
of simply assuming it is empty.

Related to #7130, #3115.

Change-Id: I7f3274b7e238ec0a9ad00643b738fbb12b84a872
PiperOrigin-RevId: 271133642
@aehlig
Copy link
Contributor

aehlig commented Dec 3, 2019

#7130 is flipped now.

@aehlig aehlig closed this as completed Dec 3, 2019
@ittaiz
Copy link
Member

ittaiz commented Jul 20, 2020

@aehlig if there are test cases that are failing with the flag on, shouldn't they be fixed?

@katre
Copy link
Member

katre commented Jul 20, 2020

@ittaiz Klaus is no longer working at Google or on Bazel. As far as I know there are open issues for a few related problems, if there are any more, can you file them separately?

@ittaiz
Copy link
Member

ittaiz commented Jul 20, 2020

Didn't know that. Will do, thanks.

jmillikin added a commit to jmillikin/rules_m4 that referenced this issue May 15, 2023
The use of absolute labels in `load()` calls was a workaround for a
Bazel bug[0] that was fixed in 2018, before the Bazel v1.0 release.

Using either absolute or relative labels as appropriate makes it
easier to understand whether a given load is expected to happen in
the `@rules_m4` repository or in the root workspace.

[0] bazelbuild/bazel#3115

GitOrigin-RevId: 7e5120adf89230c32569c6d9eb281123f1d48b54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 I'll work on this now. (Assignee required)
Projects
None yet
Development

No branches or pull requests