Skip to content
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
45 changes: 35 additions & 10 deletions packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,23 +566,48 @@ export default class TreeFS implements MutableFileSystem {
changeListener?: FileSystemListener,
): void {
const normalPath = this.#normalizePath(mixedPath);
const normalDirname = path.dirname(normalPath);
// Walk the tree to find the *real* path of the parent node, creating
// directories as we need.
const parentDirNode = this.#lookupByNormalPath(path.dirname(normalPath), {
...LOOKUP_OPTIONS,
changeListener,
makeDirectories: true,
});
const parentDirNode = this.#lookupByNormalPath(
normalDirname === '.' ? '' : normalDirname,
{
...LOOKUP_OPTIONS,
changeListener,
makeDirectories: true,
},
);
if (!parentDirNode.exists) {
throw new Error(
`TreeFS: Failed to make parent directory entry for ${mixedPath}`,
);
}
// Normalize the resulting path to account for the parent node being root.
const canonicalPath = this.#normalizePath(
parentDirNode.canonicalPath + path.sep + path.basename(normalPath),
);
this.bulkAddOrModify(new Map([[canonicalPath, metadata]]), changeListener);
// As bulkAddOrModify does for each file, without walking to the parent
Comment thread
vzaidman marked this conversation as resolved.
Resolved
// directory a second time.
const {canonicalPath: dirname, node: directoryNode} = parentDirNode;
const basename = path.basename(normalPath);
const canonicalPath =
dirname === '' ? basename : dirname + path.sep + basename;
if (!isDirectory(directoryNode)) {
throw new Error(
`TreeFS: Could not add directory ${dirname}, adding ${canonicalPath}. ` +
`${dirname} already exists in the file map as a file.`,
);
}
if (changeListener != null) {
const existingNode = directoryNode.get(basename);
if (existingNode != null) {
invariant(
!isDirectory(existingNode),
'Detected addition or modification of file %s, but it is tracked as a non-empty directory',
canonicalPath,
);
changeListener.fileModified(canonicalPath, existingNode, metadata);
} else {
changeListener.fileAdded(canonicalPath, metadata);
}
}
directoryNode.set(basename, metadata);
}

bulkAddOrModify(
Expand Down
22 changes: 22 additions & 0 deletions packages/metro-file-map/src/lib/__tests__/TreeFS-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,28 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
});
});

test('adds a file through a symlink to the root at its canonical path', () => {
tfs.addOrModify(p('link-to-root'), [0, 0, 0, null, '', null]);
const fileAdded = jest.fn();
tfs.addOrModify(p('link-to-root/new.js'), [1, 1, 0, null, 0, null], {
directoryAdded: jest.fn(),
directoryRemoved: jest.fn(),
fileAdded,
fileModified: jest.fn(),
fileRemoved: jest.fn(),
});
expect(fileAdded).toHaveBeenCalledWith(p('new.js'), [
1,
1,
0,
null,
0,
null,
]);
expect(tfs.getAllFiles()).toContain(p('/project/new.js'));
expect(tfs.getAllFiles()).not.toContain(p('/new.js'));
});

describe('bulkAddOrModify', () => {
test('adds new files and modifies existing, new symlinks work', () => {
tfs.bulkAddOrModify(
Expand Down
Loading