Index: lib/Basic/VirtualFileSystem.cpp =================================================================== --- lib/Basic/VirtualFileSystem.cpp +++ lib/Basic/VirtualFileSystem.cpp @@ -527,13 +527,19 @@ ++I; if (!Node) { if (I == E) { - // End of the path, create a new file. + // End of the path, create a new file or directory. Status Stat(P.str(), getNextVirtualUniqueID(), llvm::sys::toTimePoint(ModificationTime), ResolvedUser, ResolvedGroup, Buffer->getBufferSize(), ResolvedType, ResolvedPerms); - Dir->addChild(Name, llvm::make_unique( - std::move(Stat), std::move(Buffer))); + std::unique_ptr child; + if (ResolvedType == sys::fs::file_type::directory_file) { + child.reset(new detail::InMemoryDirectory(std::move(Stat))); + } else { + child.reset(new detail::InMemoryFile(std::move(Stat), + std::move(Buffer))); + } + Dir->addChild(Name, std::move(child)); return true; } Index: unittests/Basic/VirtualFileSystemTest.cpp =================================================================== --- unittests/Basic/VirtualFileSystemTest.cpp +++ unittests/Basic/VirtualFileSystemTest.cpp @@ -829,6 +829,19 @@ Stat->getPermissions()); } +TEST_F(InMemoryFileSystemTest, AddDirectoryThenAddChild) { + FS.addFile("/a", 0, MemoryBuffer::getMemBuffer(""), /*User=*/None, + /*Group=*/None, sys::fs::file_type::directory_file); + FS.addFile("/a/b", 0, MemoryBuffer::getMemBuffer("abc"), /*User=*/None, + /*Group=*/None, sys::fs::file_type::regular_file); + auto Stat = FS.status("/a"); + ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); + ASSERT_TRUE(Stat->isDirectory()); + Stat = FS.status("/a/b"); + ASSERT_FALSE(Stat.getError()) << Stat.getError() << "\n" << FS.toString(); + ASSERT_TRUE(Stat->isRegularFile()); +} + // NOTE: in the tests below, we use '//root/' as our root directory, since it is // a legal *absolute* path on Windows as well as *nix. class VFSFromYAMLTest : public ::testing::Test {