Skip to content

Commit

Permalink
Handle access to files under invalid repo names gracefully
Browse files Browse the repository at this point in the history
Fixes #24621

Closes #24666.

PiperOrigin-RevId: 708009380
Change-Id: I8706f1fdfba2282a27e84dff610e6511c1060e23
  • Loading branch information
fmeum authored and copybara-github committed Dec 19, 2024
1 parent 455ddb7 commit 7e7ae8c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
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 @@ -1554,11 +1555,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 @@ -3573,4 +3573,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"

0 comments on commit 7e7ae8c

Please sign in to comment.