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

[7.0.0] Make bind use repo mapping #20025

Merged
merged 2 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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