         let fileB = path.appending(component: "fileB")
         let lockFile = path.appending(component: "lockfile")
 
+        try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile)
+    }
+
+    func testLocalFileSystemFileLock() throws {
+        try withTemporaryDirectory { tempDir in
+            let fileA = tempDir.appending(component: "fileA")
+            let fileB = tempDir.appending(component: "fileB")
+            let lockFile = tempDir.appending(component: "lockfile")
+
+            try _testFileSystemFileLock(fileSystem: localFileSystem, fileA: fileA, fileB: fileB, lockFile: lockFile)
+        }
+    }
+
+    func testRerootedFileSystemViewFileLock() throws {
+        let inMemoryFS = InMemoryFileSystem()
+        let rootPath = AbsolutePath("/tmp")
+        try inMemoryFS.createDirectory(rootPath)
+
+        let fs = RerootedFileSystemView(inMemoryFS, rootedAt: rootPath)
+        let path = AbsolutePath("/")
+        try fs.createDirectory(path)
+
+        let fileA = path.appending(component: "fileA")
+        let fileB = path.appending(component: "fileB")
+        let lockFile = path.appending(component: "lockfile")
+
+        try _testFileSystemFileLock(fileSystem: fs, fileA: fileA, fileB: fileB, lockFile: lockFile)
+    }
+
+    private func _testFileSystemFileLock(fileSystem fs: FileSystem, fileA: AbsolutePath, fileB: AbsolutePath, lockFile: AbsolutePath) throws {
+        // write initial value, since reader may start before writers and files would not exist
+        try fs.writeFileContents(fileA, bytes: "0")
+        try fs.writeFileContents(fileB, bytes: "0")
+
         let writerThreads = (0..<100).map { _ in
             return Thread {
                 try! fs.withLock(on: lockFile, type: .exclusive) {
-                    // Get thr current contents of the file if any.
-                    let valueA: Int
-                    if fs.exists(fileA) {
-                        valueA = Int(try fs.readFileContents(fileA).description) ?? 0
-                    } else {
-                        valueA = 0
-                    }
+                    // Get the current contents of the file if any.
+                    let valueA = Int(try fs.readFileContents(fileA).description)!
+
                     // Sum and write back to file.
                     try fs.writeFileContents(fileA, bytes: ByteString(encodingAsUTF8: String(valueA + 1)))
 
                     Thread.yield()
 
-                    // Get thr current contents of the file if any.
-                    let valueB: Int
-                    if fs.exists(fileB) {
-                        valueB = Int(try fs.readFileContents(fileB).description) ?? 0
-                    } else {
-                        valueB = 0
-                    }
+                    // Get the current contents of the file if any.
+                    let valueB = Int(try fs.readFileContents(fileB).description)!
+
                     // Sum and write back to file.
                     try fs.writeFileContents(fileB, bytes: ByteString(encodingAsUTF8: String(valueB + 1)))
                 }

    }

    func testInMemoryFileSystemFileLock() throws {
        // Disabled until rdar://71560894 is fixed.
        try XCTSkipIf(true)
        
        let fs = InMemoryFileSystem()
        let path = AbsolutePath("/")
        try fs.createDirectory(path)

        try XCTAssertEqual(fs.readFileContents(fileA), "100")
        try XCTAssertEqual(fs.readFileContents(fileB), "100")
    }
}

/// Helper method to test file tree removal method on the given file system.
