Repository: bazelbuild/bazel
Issue: prelude ignored in remote repos

see: https://github.com/johnynek/bazel-deps/issues/13

this tool (johnynek/bazel-deps) puts the scala rules in the prelude:
https://github.com/johnynek/bazel-deps/blob/master/tools/build_rules/prelude_bazel

but another repo that does not do this and uses the above as an external repo gets:

```
$ bazel run @com_github_johnynek_bazel_deps//src/scala/com/github/johnynek/bazel_deps:parseproject
..
WARNING: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/WORKSPACE:1: Workspace name in /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/WORKSPACE (@com_github_pgr0ss_bazel_deps) does not match the name given in the repository's definition (@com_github_johnynek_bazel_deps); this will cause a build error in future versions.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:1:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:5:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:9:1: name 'scala_repl' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:12:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:27:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:35:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:42:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:52:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:67:1: name 'scala_binary' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:81:1: name 'scala_library' is not defined.
ERROR: /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD:93:1: name 'scala_library' is not defined.
ERROR: no such target '@com_github_johnynek_bazel_deps//src/scala/com/github/johnynek/bazel_deps:parseproject': target 'parseproject' not declared in package 'src/scala/com/github/johnynek/bazel_deps' defined by /private/var/tmp/_bazel_David/aca004537b75fd97c67330379e89470b/external/com_github_johnynek_bazel_deps/src/scala/com/github/johnynek/bazel_deps/BUILD.
INFO: Elapsed time: 0.715s
ERROR: Build failed. Not running target.
```

It seems to me preludes in remote repos should be in effect for the targets in those repos.


Issue comments:

Pull request review comments:

Patch that resolved the issue:
diff --git a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
index f873feac503b83..7ecd70606981f7 100644
--- a/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
+++ b/src/main/java/com/google/devtools/build/lib/skyframe/PackageFunction.java
@@ -541,7 +541,13 @@ public SkyValue compute(SkyKey key, Environment env) throws PackageFunctionExcep
       return null;
     }
 
-    SkyKey astLookupKey = ASTFileLookupValue.key(preludeLabel);
+    // Load the prelude from the same repository as the package being loaded.  Can't use
+    // Label.resolveRepositoryRelative because preludeLabel is in the main repository, not the
+    // default one, so it is resolved to itself.
+    Label pkgPreludeLabel = Label.createUnvalidated(
+        PackageIdentifier.create(packageId.getRepository(), preludeLabel.getPackageFragment()),
+        preludeLabel.getName());
+    SkyKey astLookupKey = ASTFileLookupValue.key(pkgPreludeLabel);
     ASTFileLookupValue astLookupValue = null;
     try {
       astLookupValue = (ASTFileLookupValue) env.getValueOrThrow(astLookupKey,


Test patch:
diff --git a/src/test/shell/integration/BUILD b/src/test/shell/integration/BUILD
index 1d9b55d76c1bd3..0547a794fd1fe8 100644
--- a/src/test/shell/integration/BUILD
+++ b/src/test/shell/integration/BUILD
@@ -302,6 +302,13 @@ cc_binary(
     malloc = "//base:system_malloc",
 )
 
+sh_test(
+    name = "prelude_test",
+    size = "medium",
+    srcs = ["prelude_test.sh"],
+    data = [":test-deps"],
+)
+
 ########################################################################
 # Test suites.
 
diff --git a/src/test/shell/integration/prelude_test.sh b/src/test/shell/integration/prelude_test.sh
new file mode 100755
index 00000000000000..de4e1e49c48161
--- /dev/null
+++ b/src/test/shell/integration/prelude_test.sh
@@ -0,0 +1,107 @@
+#!/bin/bash
+#
+# Copyright 2015 The Bazel Authors. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# An end-to-end test of the behavior of tools/build_rules/prelude_bazel.
+
+# Load the test setup defined in the parent directory
+CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
+source "${CURRENT_DIR}/../integration_test_setup.sh" \
+  || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
+
+#### SETUP #############################################################
+
+set -e
+
+#### TESTS #############################################################
+
+function test_prelude() {
+  touch WORKSPACE
+
+  mkdir -p tools/build_rules
+  touch tools/build_rules/BUILD
+
+  cat > tools/build_rules/prelude_bazel << EOF
+PRELUDE_VAR = 'from test_prelude'
+EOF
+
+  cat > BUILD << EOF
+genrule(
+  name = 'gr',
+  srcs = [],
+  outs = ['gr.out'],
+  cmd = 'echo "%s" > \$(location gr.out)' % PRELUDE_VAR,
+)
+EOF
+
+  bazel build :gr >&$TEST_log 2>&1 || fail "build failed"
+
+  output=$(cat bazel-genfiles/gr.out)
+  check_eq "from test_prelude" "$output" "unexpected output in gr.out"
+}
+
+function test_prelude_external_repository() {
+  cat > WORKSPACE << EOF
+local_repository(
+  name = "imported_workspace",
+  path = "$PWD/imported_workspace",
+)
+EOF
+
+  mkdir -p tools/build_rules
+  touch tools/build_rules/BUILD
+
+  cat > tools/build_rules/prelude_bazel << EOF
+PRELUDE_VAR = 'from test_prelude_external_repository, outer workspace'
+EOF
+
+  cat > BUILD << EOF
+genrule(
+  name = 'gr',
+  srcs = [],
+  outs = ['gr.out'],
+  cmd = 'echo "outer %s" > \$(location gr.out)' % PRELUDE_VAR,
+)
+EOF
+
+  mkdir -p imported_workspace
+  touch imported_workspace/WORKSPACE
+
+  mkdir -p imported_workspace/tools/build_rules
+  touch imported_workspace/tools/build_rules/BUILD
+
+  cat > imported_workspace/tools/build_rules/prelude_bazel << EOF
+PRELUDE_VAR = 'from test_prelude_external_repository, inner workspace'
+EOF
+
+  cat > imported_workspace/BUILD << EOF
+genrule(
+  name = 'gr_inner',
+  srcs = [],
+  outs = ['gr_inner.out'],
+  cmd = 'echo "inner %s" > \$(location gr_inner.out)' % PRELUDE_VAR,
+)
+EOF
+
+  bazel build :gr @imported_workspace//:gr_inner >&$TEST_log 2>&1 || fail "build failed"
+
+  output=$(cat bazel-genfiles/gr.out)
+  check_eq "outer from test_prelude_external_repository, outer workspace" "$output" "unexpected output in gr.out"
+
+  output=$(cat bazel-genfiles/external/imported_workspace/gr_inner.out)
+  check_eq "inner from test_prelude_external_repository, inner workspace" "$output" "unexpected output in gr_inner.out"
+}
+
+run_suite "prelude"