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

Test and fix root symlink edge case in runfiles library #17317

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion tools/bash/runfiles/runfiles.bash
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
30 changes: 20 additions & 10 deletions tools/cpp/runfiles/runfiles_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,10 @@ 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"}));
{",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 @@ -646,8 +648,10 @@ 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"}));
{",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 @@ -701,8 +705,10 @@ 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"}));
{",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 @@ -748,8 +754,10 @@ 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"}));
{",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 @@ -792,8 +800,10 @@ TEST_F(RunfilesTest,
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"}));
{",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
Original file line number Diff line number Diff line change
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