Skip to content

Commit eb5b0e3

Browse files
committed
[llvm] Use the VFS to make path absolute (llvm#161271)
For the redirecting VFS, the `'overlay-relative'` option controls whether external paths should be appended to the overlay directory. This didn't always work as expected: when the overlay file path itself was relative, its absolute path was decided by the real FS, not the underlying VFS, and the resulting external path didn't exist in the underlying VFS. This PR fixes this issue.
1 parent 3a8daa4 commit eb5b0e3

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm/lib/Support/VirtualFileSystem.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,7 +1909,12 @@ class llvm::vfs::RedirectingFileSystemParser {
19091909
FullPath = FS->getOverlayFileDir();
19101910
assert(!FullPath.empty() &&
19111911
"External contents prefix directory must exist");
1912-
llvm::sys::path::append(FullPath, Value);
1912+
SmallString<256> AbsFullPath = Value;
1913+
if (FS->makeAbsolute(FullPath, AbsFullPath)) {
1914+
error(N, "failed to make 'external-contents' absolute");
1915+
return nullptr;
1916+
}
1917+
FullPath = AbsFullPath;
19131918
} else {
19141919
FullPath = Value;
19151920
}
@@ -2205,7 +2210,7 @@ RedirectingFileSystem::create(std::unique_ptr<MemoryBuffer> Buffer,
22052210
// FS->OverlayFileDir => /<absolute_path_to>/dummy.cache/vfs
22062211
//
22072212
SmallString<256> OverlayAbsDir = sys::path::parent_path(YAMLFilePath);
2208-
std::error_code EC = llvm::sys::fs::make_absolute(OverlayAbsDir);
2213+
std::error_code EC = FS->makeAbsolute(OverlayAbsDir);
22092214
assert(!EC && "Overlay dir final path must be absolute");
22102215
(void)EC;
22112216
FS->setOverlayFileDir(OverlayAbsDir);

llvm/unittests/Support/VirtualFileSystemTest.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1954,6 +1954,29 @@ TEST_F(VFSFromYAMLTest, ReturnsExternalPathVFSHit) {
19541954
EXPECT_EQ(0, NumDiagnostics);
19551955
}
19561956

1957+
TEST_F(VFSFromYAMLTest, RelativeFileDirWithOverlayRelativeSetting) {
1958+
auto Lower = makeIntrusiveRefCnt<DummyFileSystem>();
1959+
Lower->addDirectory("//root/foo/bar");
1960+
Lower->addRegularFile("//root/foo/bar/a");
1961+
Lower->setCurrentWorkingDirectory("//root/foo");
1962+
IntrusiveRefCntPtr<vfs::FileSystem> FS =
1963+
getFromYAMLString("{\n"
1964+
" 'case-sensitive': false,\n"
1965+
" 'overlay-relative': true,\n"
1966+
" 'roots': [\n"
1967+
" { 'name': '//root/foo/bar/b', 'type': 'file',\n"
1968+
" 'external-contents': 'a'\n"
1969+
" }\n"
1970+
" ]\n"
1971+
"}",
1972+
Lower, "bar/overlay");
1973+
1974+
ASSERT_NE(FS.get(), nullptr);
1975+
ErrorOr<vfs::Status> S = FS->status("//root/foo/bar/b");
1976+
ASSERT_FALSE(S.getError());
1977+
EXPECT_EQ("//root/foo/bar/a", S->getName());
1978+
}
1979+
19571980
TEST_F(VFSFromYAMLTest, RootRelativeToOverlayDirTest) {
19581981
IntrusiveRefCntPtr<DummyFileSystem> Lower(new DummyFileSystem());
19591982
Lower->addDirectory("//root/foo/bar");

0 commit comments

Comments
 (0)