Merge pull request #30875 from hudsonb/bhudson/feature-discovery-fix

Fix feature discovery crash with packages using exports fields
This commit is contained in:
Patrik Oldsberg
2025-08-16 22:32:07 +02:00
committed by GitHub
2 changed files with 19 additions and 3 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
Fixed bug in PackageDiscoveryService where packages with "exports" field caused ERR_PACKAGE_PATH_NOT_EXPORTED error during backend startup.
@@ -23,6 +23,7 @@ import {
RootLoggerService,
} from '@backstage/backend-plugin-api';
import { BackstagePackageJson } from '@backstage/cli-node';
import { isError } from '@backstage/errors';
const DETECTED_PACKAGE_ROLES = [
'node-library',
@@ -125,9 +126,19 @@ 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 (isError(error) && error.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)