From dd24a0022a0ff959598da2c9bc097d27083be1a0 Mon Sep 17 00:00:00 2001 From: Fabian Meumertzheim Date: Mon, 30 Jan 2023 20:12:04 -0800 Subject: [PATCH] Test and fix root symlink edge case in runfiles library 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 #17317. PiperOrigin-RevId: 505873412 Change-Id: I72019d329b72cab9cf893deb714da8889d371617 --- tools/bash/runfiles/runfiles.bash | 5 +- tools/bash/runfiles/runfiles_test.bash | 8 +++ tools/cpp/runfiles/runfiles_test.cc | 50 +++++++++++-------- tools/java/runfiles/testing/RunfilesTest.java | 12 +++++ 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/tools/bash/runfiles/runfiles.bash b/tools/bash/runfiles/runfiles.bash index 6607317c759ab8..540eb90d21461d 100644 --- a/tools/bash/runfiles/runfiles.bash +++ b/tools/bash/runfiles/runfiles.bash @@ -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) diff --git a/tools/bash/runfiles/runfiles_test.bash b/tools/bash/runfiles/runfiles_test.bash index 1e1c096b29e9cd..d9c384787a9462 100755 --- a/tools/bash/runfiles/runfiles_test.bash +++ b/tools/bash/runfiles/runfiles_test.bash @@ -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" < "$RUNFILES_DIR/_repo_mapping" < "$tmpdir/foo.repo_mapping" < "$tmpdir/foo.repo_mapping" < 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 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 mf(MockFile::Create( "foo" + uid + ".runfiles_manifest", @@ -644,10 +646,12 @@ TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromMain) { TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromOtherRepo) { string uid = LINE_AS_STRING(); - unique_ptr 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 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 mf(MockFile::Create( "foo" + uid + ".runfiles_manifest", @@ -699,10 +703,12 @@ TEST_F(RunfilesTest, ManifestBasedRlocationWithRepoMapping_fromOtherRepo) { TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromMain) { string uid = LINE_AS_STRING(); - unique_ptr 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 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())); @@ -746,10 +752,12 @@ TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromMain) { TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromOtherRepo) { string uid = LINE_AS_STRING(); - unique_ptr 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 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())); @@ -790,10 +798,12 @@ TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromOtherRepo) { TEST_F(RunfilesTest, DirectoryBasedRlocationWithRepoMapping_fromOtherRepo_withSourceRepo) { string uid = LINE_AS_STRING(); - unique_ptr 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 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())); diff --git a/tools/java/runfiles/testing/RunfilesTest.java b/tools/java/runfiles/testing/RunfilesTest.java index 8b88ce28bd4e3d..92d420f77ce7bf 100644 --- a/tools/java/runfiles/testing/RunfilesTest.java +++ b/tools/java/runfiles/testing/RunfilesTest.java @@ -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( @@ -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( @@ -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( @@ -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(""); @@ -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(); @@ -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())