Skip to content

Commit 98283fc

Browse files
committed
fix(tools-node): limit package dependency search scope
1 parent 339f02c commit 98283fc

3 files changed

Lines changed: 49 additions & 1 deletion

File tree

.changeset/curly-needles-rest.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rnx-kit/tools-node": patch
3+
---
4+
5+
Add a `stopAt` option to limit `findPackageDependencyDir` searches.

packages/tools-node/src/package.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ export type FindPackageDependencyOptions = {
170170
*/
171171
startDir?: string;
172172

173+
/**
174+
* Optional directory where the search should stop.
175+
*/
176+
stopAt?: string;
177+
173178
/**
174179
* Optional flag controlling whether symlinks can be found. Defaults to `true`.
175180
* When `false`, and the package dependency directory is a symlink, it will not
@@ -201,10 +206,12 @@ export function findPackageDependencyDir(
201206
): string | undefined {
202207
const pkgName =
203208
typeof ref === "string" ? ref : path.join(ref.scope ?? "", ref.name);
209+
const startDir = options?.startDir ?? process.cwd();
204210
const packageDir = findUp(
205211
path.join("node_modules", pkgName),
206212
{
207-
startDir: options?.startDir,
213+
startDir,
214+
stopAt: options?.stopAt,
208215
type: "directory",
209216
allowSymlinks: options?.allowSymlinks,
210217
},

packages/tools-node/test/package.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,40 @@ describe("Node > Package", () => {
212212
);
213213
equal(pkgDir, undefined);
214214
});
215+
216+
it("findPackageDependencyDir() does not search beyond stopAt", () => {
217+
const rootDir = path.join(testTempDir, "repo");
218+
const workspaceDir = path.join(rootDir, "packages", "app");
219+
const externalDir = path.join(testTempDir, "node_modules", "external");
220+
const localDir = path.join(
221+
rootDir,
222+
"packages",
223+
"node_modules",
224+
"local"
225+
);
226+
const blockedLocalDir = path.join(rootDir, "node_modules", "blocked");
227+
228+
fs.mkdirSync(workspaceDir, { recursive: true });
229+
fs.mkdirSync(externalDir, { recursive: true });
230+
fs.mkdirSync(localDir, { recursive: true });
231+
fs.mkdirSync(blockedLocalDir, { recursive: true });
232+
233+
const blockedPkgDir = findPackageDependencyDir("external", {
234+
startDir: workspaceDir,
235+
stopAt: rootDir,
236+
});
237+
equal(blockedPkgDir, undefined);
238+
239+
const localPkgDir = findPackageDependencyDir("local", {
240+
startDir: workspaceDir,
241+
stopAt: rootDir,
242+
});
243+
equal(localPkgDir, localDir);
244+
245+
const blockedLocalPkgDir = findPackageDependencyDir("blocked", {
246+
startDir: workspaceDir,
247+
stopAt: rootDir,
248+
});
249+
equal(blockedLocalPkgDir, undefined);
250+
});
215251
});

0 commit comments

Comments
 (0)