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

[8.0.1] Handle access to files under invalid repo names gracefully #24771

Merged
merged 1 commit into from
Dec 26, 2024
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 @@ -38,6 +38,7 @@
import com.google.devtools.build.lib.bazel.repository.downloader.HttpUtils;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelConstants;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.ExtendedEventHandler.FetchProgress;
Expand Down Expand Up @@ -1553,11 +1554,16 @@ protected RepoCacheFriendlyPath toRepoCacheFriendlyPath(Path path, ShouldWatch s
PathFragment relPath = path.relativeTo(outputBaseExternal);
if (!relPath.isEmpty()) {
// The file is under a repo root.
String repoName = relPath.getSegment(0);
RepositoryName repoName;
try {
repoName = RepositoryName.create(relPath.getSegment(0));
} catch (LabelSyntaxException e) {
throw Starlark.errorf(
"attempted to watch path under external repository directory: %s", e.getMessage());
}
PathFragment repoRelPath =
relPath.relativeTo(PathFragment.createAlreadyNormalized(repoName));
return RepoCacheFriendlyPath.createInsideWorkspace(
RepositoryName.createUnvalidated(repoName), repoRelPath);
relPath.relativeTo(PathFragment.createAlreadyNormalized(repoName.getName()));
return RepoCacheFriendlyPath.createInsideWorkspace(repoName, repoRelPath);
}
}
// The file is just under a random absolute path.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.cmdline.LabelConstants;
import com.google.devtools.build.lib.cmdline.LabelSyntaxException;
import com.google.devtools.build.lib.cmdline.RepositoryName;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.packages.NoSuchPackageException;
Expand Down Expand Up @@ -404,8 +405,15 @@ public static void addExternalFilesDependencies(
// repository path.
return;
}
String repositoryName = repositoryPath.getSegment(0);
env.getValue(RepositoryDirectoryValue.key(RepositoryName.createUnvalidated(repositoryName)));
RepositoryName repositoryName;
try {
repositoryName = RepositoryName.create(repositoryPath.getSegment(0));
} catch (LabelSyntaxException ignored) {
// The directory of a repository with an invalid name can never exist, so we don't need to
// add a dependency on it.
return;
}
env.getValue(RepositoryDirectoryValue.key(repositoryName));
}

/** Sets up a mapping of environment variables to use. */
Expand Down
18 changes: 18 additions & 0 deletions src/test/shell/bazel/starlark_repository_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3572,4 +3572,22 @@ EOF
@repo//... &> $TEST_log || fail "expected Bazel to succeed"
}

function test_dependency_on_repo_with_invalid_name() {
cat >> $(setup_module_dot_bazel) <<'EOF'
my_repo = use_repo_rule("//:repo.bzl", "my_repo")
my_repo(name="repo")
EOF
touch BUILD
cat > repo.bzl <<'EOF'
def _impl(ctx):
ctx.read("../@invalid_name@/file")

my_repo = repository_rule(_impl)
EOF

bazel build @repo//... &> $TEST_log && fail "expected Bazel to fail"
expect_not_log "Unrecoverable error"
expect_log "attempted to watch path under external repository directory: invalid repository name '@invalid_name@'"
}

run_suite "local repository tests"
Loading