cli-common: Remove unused findOwnDir and validate findOwnRootDir

Addresses review feedback from PR #32939:
- Remove the `findOwnDir` function which is no longer needed
- Add validation to `findOwnRootDir` to verify the resolved path
  actually contains a monorepo root with a `package.json` that has
  a `workspaces` field

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2026-02-24 13:21:41 +01:00
parent 1176eb0540
commit e44b6a9079
3 changed files with 27 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli-common': patch
---
The `findOwnRootDir` utility now searches for the monorepo root by traversing up the directory tree looking for a `package.json` with `workspaces`, instead of assuming a fixed `../..` relative path.
+5 -5
View File
@@ -16,18 +16,18 @@
/* eslint-disable no-restricted-syntax */
import { resolve as resolvePath } from 'node:path';
import { findPaths, findRootPath, findOwnDir, findOwnRootDir } from './paths';
import { findPaths, findRootPath, findOwnRootDir, findOwnPaths } from './paths';
describe('paths', () => {
afterEach(() => {
jest.restoreAllMocks();
});
it('findOwnDir and findOwnRootDir should find owns paths', () => {
const dir = findOwnDir(__dirname);
const root = findOwnRootDir(dir);
it('findOwnPaths and findOwnRootDir should find own paths', () => {
const own = findOwnPaths(__dirname);
const root = findOwnRootDir(own.dir);
expect(dir).toBe(resolvePath(__dirname, '..'));
expect(own.dir).toBe(resolvePath(__dirname, '..'));
expect(root).toBe(resolvePath(__dirname, '../../..'));
});
+17 -6
View File
@@ -119,7 +119,23 @@ export function findOwnRootDir(ownDir: string) {
);
}
return resolvePath(ownDir, '../..');
const rootDir = findRootPath(ownDir, pkgJsonPath => {
try {
const content = fs.readFileSync(pkgJsonPath, 'utf8');
const data = JSON.parse(content);
return Boolean(data.workspaces);
} catch (error) {
throw new Error(
`Failed to read package.json at '${pkgJsonPath}', ${error}`,
);
}
});
if (!rootDir) {
throw new Error(`No monorepo root found when searching from '${ownDir}'`);
}
return rootDir;
}
// Hierarchical directory cache shared across all OwnPathsImpl instances.
@@ -199,11 +215,6 @@ class OwnPathsImpl implements OwnPaths {
};
}
// Finds the root of a given package
export function findOwnDir(searchDir: string) {
return OwnPathsImpl.findDir(searchDir);
}
// Used by the test utility in testUtils.ts to override targetPaths
export let targetPathsOverride: TargetPaths | undefined;