Skip to content

Commit

Permalink
Test and fix root symlink edge case in runfiles library
Browse files Browse the repository at this point in the history
Due to incomplete test coverage for the case of a root symlink named like a Bazel module, the Bash runfiles libraries behaved incorrectly in this case. This commit fixes that and adds proper test coverage for all libraries by adding repo mappings that conflict with the existing rlocation testcase for `config.json`.

Closes bazelbuild#17317.

PiperOrigin-RevId: 505873412
Change-Id: I72019d329b72cab9cf893deb714da8889d371617
  • Loading branch information
fmeum authored and Copybara-Service committed Jan 31, 2023
1 parent 8e41dce commit dd24a00
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 21 deletions.
5 changes: 4 additions & 1 deletion tools/bash/runfiles/runfiles.bash
Expand Up @@ -142,7 +142,10 @@ function rlocation() {

if [[ -f "$RUNFILES_REPO_MAPPING" ]]; then
local -r target_repo_apparent_name=$(echo "$1" | cut -d / -f 1)
local -r remainder=$(echo "$1" | cut -d / -f 2-)
# Use -s to get an empty remainder if the argument does not contain a slash.
# The repo mapping should not be applied to single segment paths, which may
# be root symlinks.
local -r remainder=$(echo "$1" | cut -s -d / -f 2-)
if [[ -n "$remainder" ]]; then
if [[ -z "${2+x}" ]]; then
local -r source_repo=$(runfiles_current_repository 2)
Expand Down
8 changes: 8 additions & 0 deletions tools/bash/runfiles/runfiles_test.bash
Expand Up @@ -216,10 +216,12 @@ function test_directory_based_runfiles_with_repo_mapping_from_main() {
export RUNFILES_DIR=${tmpdir}/mock/runfiles
mkdir -p "$RUNFILES_DIR"
cat > "$RUNFILES_DIR/_repo_mapping" <<EOF
,config.json,config.json~1.2.3
,my_module,_main
,my_protobuf,protobuf~3.19.2
,my_workspace,_main
protobuf~3.19.2,protobuf,protobuf~3.19.2
protobuf~3.19.2,config.json,config.json~1.2.3
EOF
export RUNFILES_MANIFEST_FILE=
source "$runfiles_lib_path"
Expand Down Expand Up @@ -258,10 +260,12 @@ function test_directory_based_runfiles_with_repo_mapping_from_other_repo() {
export RUNFILES_DIR=${tmpdir}/mock/runfiles
mkdir -p "$RUNFILES_DIR"
cat > "$RUNFILES_DIR/_repo_mapping" <<EOF
,config.json,config.json~1.2.3
,my_module,_main
,my_protobuf,protobuf~3.19.2
,my_workspace,_main
protobuf~3.19.2,protobuf,protobuf~3.19.2
protobuf~3.19.2,config.json,config.json~1.2.3
EOF
export RUNFILES_MANIFEST_FILE=
source "$runfiles_lib_path"
Expand Down Expand Up @@ -296,10 +300,12 @@ function test_manifest_based_runfiles_with_repo_mapping_from_main() {
local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)"

cat > "$tmpdir/foo.repo_mapping" <<EOF
,config.json,config.json~1.2.3
,my_module,_main
,my_protobuf,protobuf~3.19.2
,my_workspace,_main
protobuf~3.19.2,protobuf,protobuf~3.19.2
protobuf~3.19.2,config.json,config.json~1.2.3
EOF
export RUNFILES_DIR=
export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest"
Expand Down Expand Up @@ -344,10 +350,12 @@ function test_manifest_based_runfiles_with_repo_mapping_from_other_repo() {
local tmpdir="$(mktemp -d $TEST_TMPDIR/tmp.XXXXXXXX)"

cat > "$tmpdir/foo.repo_mapping" <<EOF
,config.json,config.json~1.2.3
,my_module,_main
,my_protobuf,protobuf~3.19.2
,my_workspace,_main
protobuf~3.19.2,protobuf,protobuf~3.19.2
protobuf~3.19.2,config.json,config.json~1.2.3
EOF
export RUNFILES_DIR=
export RUNFILES_MANIFEST_FILE="$tmpdir/foo.runfiles_manifest"
Expand Down
50 changes: 30 additions & 20 deletions tools/cpp/runfiles/runfiles_test.cc
Expand Up @@ -586,10 +586,12 @@ TEST_F(RunfilesTest, PathsFromEnvVars) {

TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromMain) {
string uid = LINE_AS_STRING();
unique_ptr<MockFile> rm(MockFile::Create(
"foo" + uid + ".repo_mapping",
{",my_module,_main", ",my_protobuf,protobuf~3.19.2",
",my_workspace,_main", "protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
unique_ptr<MockFile> rm(
MockFile::Create("foo" + uid + ".repo_mapping",
{",config.json,config.json~1.2.3", ",my_module,_main",
",my_protobuf,protobuf~3.19.2", ",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
ASSERT_TRUE(rm != nullptr);
unique_ptr<MockFile> mf(MockFile::Create(
"foo" + uid + ".runfiles_manifest",
Expand Down Expand Up @@ -644,10 +646,12 @@ TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromMain) {

TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromOtherRepo) {
string uid = LINE_AS_STRING();
unique_ptr<MockFile> rm(MockFile::Create(
"foo" + uid + ".repo_mapping",
{",my_module,_main", ",my_protobuf,protobuf~3.19.2",
",my_workspace,_main", "protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
unique_ptr<MockFile> rm(
MockFile::Create("foo" + uid + ".repo_mapping",
{",config.json,config.json~1.2.3", ",my_module,_main",
",my_protobuf,protobuf~3.19.2", ",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
ASSERT_TRUE(rm != nullptr);
unique_ptr<MockFile> mf(MockFile::Create(
"foo" + uid + ".runfiles_manifest",
Expand Down Expand Up @@ -699,10 +703,12 @@ TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromOtherRepo) {

TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromMain) {
string uid = LINE_AS_STRING();
unique_ptr<MockFile> rm(MockFile::Create(
"foo" + uid + ".runfiles/_repo_mapping",
{",my_module,_main", ",my_protobuf,protobuf~3.19.2",
",my_workspace,_main", "protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
unique_ptr<MockFile> rm(
MockFile::Create("foo" + uid + ".runfiles/_repo_mapping",
{",config.json,config.json~1.2.3", ",my_module,_main",
",my_protobuf,protobuf~3.19.2", ",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
ASSERT_TRUE(rm != nullptr);
string dir = rm->DirName();
string argv0(dir.substr(0, dir.size() - string(".runfiles").size()));
Expand Down Expand Up @@ -746,10 +752,12 @@ TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromMain) {

TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromOtherRepo) {
string uid = LINE_AS_STRING();
unique_ptr<MockFile> rm(MockFile::Create(
"foo" + uid + ".runfiles/_repo_mapping",
{",my_module,_main", ",my_protobuf,protobuf~3.19.2",
",my_workspace,_main", "protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
unique_ptr<MockFile> rm(
MockFile::Create("foo" + uid + ".runfiles/_repo_mapping",
{",config.json,config.json~1.2.3", ",my_module,_main",
",my_protobuf,protobuf~3.19.2", ",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
ASSERT_TRUE(rm != nullptr);
string dir = rm->DirName();
string argv0(dir.substr(0, dir.size() - string(".runfiles").size()));
Expand Down Expand Up @@ -790,10 +798,12 @@ TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromOtherRepo) {
TEST_F(RunfilesTest,
DirectoryBasedRlocationWithRepoMapping_fromOtherRepo_withSourceRepo) {
string uid = LINE_AS_STRING();
unique_ptr<MockFile> rm(MockFile::Create(
"foo" + uid + ".runfiles/_repo_mapping",
{",my_module,_main", ",my_protobuf,protobuf~3.19.2",
",my_workspace,_main", "protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
unique_ptr<MockFile> rm(
MockFile::Create("foo" + uid + ".runfiles/_repo_mapping",
{",config.json,config.json~1.2.3", ",my_module,_main",
",my_protobuf,protobuf~3.19.2", ",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"}));
ASSERT_TRUE(rm != nullptr);
string dir = rm->DirName();
string argv0(dir.substr(0, dir.size() - string(".runfiles").size()));
Expand Down
12 changes: 12 additions & 0 deletions tools/java/runfiles/testing/RunfilesTest.java
Expand Up @@ -264,9 +264,11 @@ public void testManifestBasedRlocationWithRepoMapping_fromMain() throws Exceptio
tempFile(
"foo.repo_mapping",
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Path mf =
tempFile(
Expand Down Expand Up @@ -318,9 +320,11 @@ public void testManifestBasedRlocationUnmapped() throws Exception {
tempFile(
"foo.repo_mapping",
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Path mf =
tempFile(
Expand Down Expand Up @@ -367,9 +371,11 @@ public void testManifestBasedRlocationWithRepoMapping_fromOtherRepo() throws Exc
tempFile(
"foo.repo_mapping",
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Path mf =
tempFile(
Expand Down Expand Up @@ -419,9 +425,11 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromMain() throws Excepti
tempFile(
dir.resolve("_repo_mapping").toString(),
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString()).withSourceRepository("");

Expand Down Expand Up @@ -458,9 +466,11 @@ public void testDirectoryBasedRlocationUnmapped() throws Exception {
tempFile(
dir.resolve("_repo_mapping").toString(),
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Runfiles r = Runfiles.createDirectoryBasedForTesting(dir.toString()).unmapped();

Expand Down Expand Up @@ -497,9 +507,11 @@ public void testDirectoryBasedRlocationWithRepoMapping_fromOtherRepo() throws Ex
tempFile(
dir.resolve("_repo_mapping").toString(),
ImmutableList.of(
",config.json,config.json~1.2.3",
",my_module,_main",
",my_protobuf,protobuf~3.19.2",
",my_workspace,_main",
"protobuf~3.19.2,config.json,config.json~1.2.3",
"protobuf~3.19.2,protobuf,protobuf~3.19.2"));
Runfiles r =
Runfiles.createDirectoryBasedForTesting(dir.toString())
Expand Down

0 comments on commit dd24a00

Please sign in to comment.