Skip to content

Commit

Permalink
repository mapping lookup: convert to canonical name first
Browse files Browse the repository at this point in the history
By the way we propagate the information in which repository we are in,
we always have a repository name that is valid in the main repository;
however, it is not necessarily a canonical name. Our map of the repository
mappings, however, is keyed by canonical names. So, add the missing indirection
and first look up the canonical name for the repository name we're given
by looking it up in the mapping of the main repository (for which we know
its canonical name) and only then use that name as key.

Fixes a bug related to #7130.
Prerequisite to flip --incompatible_remap_main_repo.
Therefore, consider for Bazel 0.29.0 (#8572).

Change-Id: Icd426a18aaa406b614f4948a8122177463a72959
PiperOrigin-RevId: 265012106
  • Loading branch information
aehlig authored and copybara-github committed Aug 23, 2019
1 parent 5697da9 commit 76ed014
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,16 @@ public ImmutableMap<RepositoryName, RepositoryName> getRepositoryMapping(
throw new UnsupportedOperationException("Can only access the external package repository"
+ "mappings from the //external package");
}
return externalPackageRepositoryMappings.getOrDefault(repository, ImmutableMap.of());

// We are passed a repository name as seen from the main repository, not necessarily
// a canonical repository name. So, we first have to find the canonical name for the
// repository in question before we can look up the mapping for it.
RepositoryName actualRepositoryName =
externalPackageRepositoryMappings
.getOrDefault(RepositoryName.MAIN, ImmutableMap.of())
.getOrDefault(repository, repository);

return externalPackageRepositoryMappings.getOrDefault(actualRepositoryName, ImmutableMap.of());
}

/** Get the repository mapping for this package. */
Expand Down
60 changes: 60 additions & 0 deletions src/test/shell/bazel/workspace_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -994,5 +994,65 @@ EOF

}

test_remap_toolchains_from_qualified_load() {
cat > WORKSPACE <<'EOF'
workspace(name = "my_ws")
register_toolchains("@my_ws//toolchains:sample_toolchain")
EOF
mkdir toolchains
cat > toolchains/toolchain.bzl <<'EOF'
def _impl(ctx):
return [platform_common.ToolchainInfo()]
mytoolchain = rule(
implementation = _impl,
attrs = {},
)
EOF
cat > toolchains/rule.bzl <<'EOF'
def _impl(ctx):
# Ensure the toolchain is available under the requested (non-canonical)
# name
print("toolchain is %s" %
(ctx.toolchains["@my_ws//toolchains:my_toolchain_type"],))
pass
testrule = rule(
implementation = _impl,
attrs = {},
toolchains = ["@my_ws//toolchains:my_toolchain_type"],
)
EOF
cat > toolchains/BUILD <<'EOF'
load("@my_ws//toolchains:toolchain.bzl", "mytoolchain")
load("@my_ws//toolchains:rule.bzl", "testrule")
toolchain_type(name = "my_toolchain_type")
mytoolchain(name = "thetoolchain")
toolchain(
name = "sample_toolchain",
toolchain = "@my_ws//toolchains:thetoolchain",
toolchain_type = "@my_ws//toolchains:my_toolchain_type",
)
testrule(
name = "emptytoolchainconsumer",
)
EOF

bazel build --incompatible_remap_main_repo=true @my_ws//toolchains/... \
|| fail "expected success"

# Additionally check, that nothing goes wrong flipping the remapping
# off and on again.
bazel build --incompatible_remap_main_repo=false @my_ws//toolchains/... \
|| fail "expected success"

bazel build --incompatible_remap_main_repo=true @my_ws//toolchains/... \
|| fail "expected success"
}


run_suite "workspace tests"

0 comments on commit 76ed014

Please sign in to comment.