Fix feature discovery crash with packages using modern Node.js exports fields

When using packages that employ modern Node.js "exports" fields without exporting ./package.json (such as OpenTelemetry packages), the feature discovery service crashes with ERR_PACKAGE_PATH_NOT_EXPORTED errors. This happens because these packages restrict which files can be accessed via require(), and many don't expose their package.json files.

Wraps the package.json resolution with error handling to gracefully skip packages that don't export their package.json. Since these packages can't be Backstage plugins anyway (we need access to the backstage.role field).

Signed-off-by: Brian Hudson <brian.r.hudson@gmail.com>
This commit is contained in:
Brian Hudson
2025-08-13 07:17:45 -04:00
parent 9ee3038983
commit 1c1347a827
@@ -125,9 +125,22 @@ export class PackageDiscoveryService {
const features: BackendFeature[] = [];
for (const name of dependencyNames) {
const depPkg = require(require.resolve(`${name}/package.json`, {
paths: [packageDir],
})) as BackstagePackageJson;
let depPkg: BackstagePackageJson;
try {
const packageJsonPath = require.resolve(`${name}/package.json`, {
paths: [packageDir],
});
depPkg = require(packageJsonPath) as BackstagePackageJson;
} catch (error) {
// Handle packages with "exports" field that don't export ./package.json
if (
error instanceof Error &&
(error as any).code === 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
continue; // Skip packages that don't export package.json - they can't be Backstage packages
}
throw error;
}
if (
!depPkg?.backstage?.role ||
!DETECTED_PACKAGE_ROLES.includes(depPkg.backstage.role)