Skip to content

Commit 4ab455b

Browse files
philikonFacebook Github Bot 0
authored andcommitted
packager: dedupe symlinks if they point to already covered paths
Summary: If the packager is already watching `/path/to/MyProject`, and it finds symlinks inside `/path/to/MyProject/node_modules` that point to `/path/to/MyProject/path/to/somewhere/else`, there's no need to add the latter to the project roots. **Test Plan:** replicate an aforementioned project set-up, and verify that only `/path/to/MyProject` is a project root for the packager, while files in `/path/to/MyProject/path/to/somewhere/else` can still be imported so long as they're part of an npm-style package (e.g. `/path/to/MyProject/path/to/somewhere/else/package.json` exists). Closes facebook#9819 Differential Revision: D3852591 Pulled By: mkonicek fbshipit-source-id: 558ab3f835ee3d2bf6174c31595e242992f75601
1 parent d14eb2e commit 4ab455b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

local-cli/server/findSymlinksPaths.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11
const path = require('path');
22
const fs = require('fs');
33

4-
module.exports = function findSymlinksPaths(lookupFolder) {
4+
function isSubPathOfPath(parentPath, subPath) {
5+
return !path.relative(parentPath, subPath).startsWith('..' + path.sep);
6+
}
7+
8+
function isSubPathOfPaths(parentPaths, subPath) {
9+
return parentPaths.some(parentPath => isSubPathOfPath(parentPath, subPath));
10+
}
11+
12+
/**
13+
* Find and resolve symlinks in `lookupFolder`, filtering out any
14+
* paths that are subpaths of `existingSearchPaths`.
15+
*/
16+
module.exports = function findSymlinksPaths(lookupFolder, existingSearchPaths) {
517
const timeStart = Date.now();
618
const folders = fs.readdirSync(lookupFolder);
719
const resolvedSymlinks = folders.map(folder => path.resolve(lookupFolder, folder))
820
.filter(folderPath => fs.lstatSync(folderPath).isSymbolicLink())
9-
.map(symlink => path.resolve(process.cwd(), fs.readlinkSync(symlink)));
21+
.map(symlink => path.resolve(process.cwd(), fs.readlinkSync(symlink)))
22+
.filter(symlinkPath => !isSubPathOfPaths(existingSearchPaths, symlinkPath));
1023
const timeEnd = Date.now();
1124

1225
console.log(`Scanning ${folders.length} folders for symlinks in ${lookupFolder} (${timeEnd - timeStart}ms)`);

local-cli/server/server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const NODE_MODULES = path.resolve(__dirname, '..', '..', '..');
2121
function server(argv, config, args) {
2222
args.projectRoots = args.projectRoots.concat(
2323
args.root,
24-
findSymlinksPaths(NODE_MODULES)
24+
findSymlinksPaths(NODE_MODULES, args.projectRoots)
2525
);
2626

2727
console.log(formatBanner(

0 commit comments

Comments
 (0)