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

Fix symlinked packages not respecting package.json redirections #1349

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
14 changes: 10 additions & 4 deletions packages/metro-file-map/src/lib/TreeFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ function isRegularFile(node: FileNode): boolean {
return node[H.SYMLINK] === 0;
}

type NormalizedSymlinkTarget = {ancestorOfRootIdx: ?number, normalPath: string};
type NormalizedSymlinkTarget = {
ancestorOfRootIdx: ?number,
normalPath: string,
startOfBasenameIdx: number,
};

/**
* OVERVIEW:
Expand Down Expand Up @@ -667,9 +671,10 @@ export default class TreeFS implements MutableFileSystem {
}

// For the purpose of collecting ancestors: Ignore the traversal to
// the symlink target, and start collecting ancestors only when we
// reach the remaining part of the path.
unseenPathFromIdx = normalSymlinkTarget.normalPath.length;
// the symlink target, and start collecting ancestors only
// from the target itself (ie, the basename of the normal target path)
// onwards.
unseenPathFromIdx = normalSymlinkTarget.startOfBasenameIdx;

if (seen == null) {
// Optimisation: set this lazily only when we've encountered a symlink
Expand Down Expand Up @@ -1122,6 +1127,7 @@ export default class TreeFS implements MutableFileSystem {
ancestorOfRootIdx:
this.#pathUtils.getAncestorOfRootIdx(normalSymlinkTarget),
normalPath: normalSymlinkTarget,
startOfBasenameIdx: normalSymlinkTarget.lastIndexOf(path.sep) + 1,
};
this.#cachedNormalSymlinkTargets.set(symlinkNode, result);
return result;
Expand Down
11 changes: 11 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 @@ -314,6 +314,10 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
p('a/b/c/d/link-to-A'),
['', 0, 0, 0, '', '', p('../../../../../..')],
],
[
p('n_m/workspace/link-to-pkg'),
['', 0, 0, 0, '', '', p('../../../workspace-pkg')],
],
].concat(
[
'a/package.json',
Expand All @@ -331,6 +335,7 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
'a/n_m/pkg/n_m/pkg2/package.json',
'../../package.json',
'../../../a/b/package.json',
'../workspace-pkg/package.json',
].map(posixPath => [p(posixPath), ['', 0, 0, 0, '', '', 0]]),
),
),
Expand Down Expand Up @@ -447,6 +452,12 @@ describe.each([['win32'], ['posix']])('TreeFS on %s', platform => {
['/A/B/C/a/n_m/pkg3/foo.js', null, null, ['/A/B/C/a/n_m/pkg3']],
// Does not look beyond n_m, if n_m does not exist
['/A/B/C/a/b/n_m/pkg/foo', null, null, ['/A/B/C/a/b/n_m']],
[
'/A/B/C/n_m/workspace/link-to-pkg/subpath',
'/A/B/workspace-pkg/package.json',
'subpath',
['/A/B/C/n_m/workspace/link-to-pkg', '/A/B/workspace-pkg/subpath'],
],
])(
'%s => %s (relative %s, invalidatedBy %s)',
(
Expand Down
Loading