Skip to content

Commit

Permalink
[7.0.0] Make bind use repo mapping (bazelbuild#20025)
Browse files Browse the repository at this point in the history
Fixes bazelbuild#19973.

Closes bazelbuild#20012.

Commit
bazelbuild@c7e391c

PiperOrigin-RevId: 578864227
Change-Id: I8a454446adbdd2b6ad248a7f1d657d2aed151bb7

Co-authored-by: Xdng Yng <[email protected]>
  • Loading branch information
bazel-io and Wyverald authored Nov 2, 2023
1 parent 78057b6 commit 7e7a4ec
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.cmdline.BazelModuleContext;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.Label.RepoContext;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.LabelValidator;
import com.google.devtools.build.lib.cmdline.RepositoryMapping;
Expand Down Expand Up @@ -71,23 +72,19 @@ public void workspace(
.addRepositoryMappingEntry(RepositoryName.MAIN, name, RepositoryName.MAIN);
}

private static RepositoryName getRepositoryName(@Nullable Label label) {
if (label == null) {
// registration happened directly in the main WORKSPACE
private static RepositoryName getCurrentRepoName(StarlarkThread thread) {
@Nullable // moduleContext is null if we're called directly from a WORKSPACE file.
BazelModuleContext moduleContext =
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread));
if (moduleContext == null) {
return RepositoryName.MAIN;
}

// registration happened in a loaded bzl file
return label.getRepository();
return moduleContext.label().getRepository();
}

private static ImmutableList<TargetPattern> parsePatterns(
List<String> patterns, Package.Builder builder, StarlarkThread thread) throws EvalException {
@Nullable // moduleContext is null if we're called directly from a WORKSPACE file.
BazelModuleContext moduleContext =
BazelModuleContext.of(Module.ofInnermostEnclosingStarlarkFunction(thread));
RepositoryName myName =
getRepositoryName((moduleContext != null ? moduleContext.label() : null));
RepositoryName myName = getCurrentRepoName(thread);
RepositoryMapping renaming = builder.getRepositoryMappingFor(myName);
TargetPattern.Parser parser =
new TargetPattern.Parser(PathFragment.EMPTY_FRAGMENT, myName, renaming);
Expand Down Expand Up @@ -132,11 +129,16 @@ public void bind(String name, Object actual, StarlarkThread thread)
try {
Package.Builder builder = PackageFactory.getContext(thread).pkgBuilder;
RuleClass ruleClass = ruleClassMap.get("bind");
RepositoryName currentRepo = getCurrentRepoName(thread);
WorkspaceFactoryHelper.addBindRule(
builder,
ruleClass,
nameLabel,
actual == NONE ? null : Label.parseCanonical((String) actual),
actual == NONE
? null
: Label.parseWithRepoContext(
(String) actual,
RepoContext.of(currentRepo, builder.getRepositoryMappingFor(currentRepo))),
thread.getCallStack());
} catch (InvalidRuleException | Package.NameConflictException | LabelSyntaxException e) {
throw Starlark.errorf("%s", e.getMessage());
Expand Down
16 changes: 16 additions & 0 deletions src/test/shell/bazel/external_integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,22 @@ EOF
expect_log "No repository visible as '@foo' from main repository"
}

function test_bind_repo_mapping() {
cat >> $(create_workspace_with_default_repos WORKSPACE myws) <<'EOF'
load('//:foo.bzl', 'foo')
foo()
bind(name='bar', actual='@myws//:something')
EOF
cat > foo.bzl <<'EOF'
def foo():
native.bind(name='foo', actual='@myws//:something')
EOF
cat > BUILD <<'EOF'
filegroup(name='something', visibility=["//visibility:public"])
EOF
bazel build //external:foo //external:bar &> "$TEST_log" || fail "don't fail!"
}

function test_flip_flopping() {
REPO_PATH=$TEST_TMPDIR/repo
mkdir -p "$REPO_PATH"
Expand Down

0 comments on commit 7e7a4ec

Please sign in to comment.