diff --git a/packages/build-info/src/detect-build-system.ts b/packages/build-info/src/detect-build-system.ts index 04cf9d195f..494a32fd3b 100644 --- a/packages/build-info/src/detect-build-system.ts +++ b/packages/build-info/src/detect-build-system.ts @@ -169,5 +169,11 @@ const lookFor = ( } const getPkgJson = (configPath: string): PackageJson => { - return JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8')) + let pkgJson: PackageJson = {} + try { + pkgJson = JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8')) + } catch { + // noop + } + return pkgJson } diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index 224d403a70..67a71dc64b 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -15,19 +15,23 @@ export type Info = { export const getBuildInfo = async (opts: ContextOptions) => { const context = await getContext(opts) let frameworks: any[] = [] - let buildSystems: BuildSystem[] = [] try { - // if the framework or buildSystem detection is crashing we should not crash the build info and package-manager + // if the framework detection is crashing we should not crash the build info and package-manager // detection frameworks = await listFrameworks({ projectDir: context.projectDir }) - buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) } catch { // TODO: build reporting to buildbot see: https://github.com/netlify/pillar-workflow/issues/1001 // noop } - const info: Info = { frameworks, buildSystems } + const info: Info = { frameworks } + + try { + info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) + } catch (error) { + // noop + } // only if we find a root package.json we know this is a javascript workspace if (Object.keys(context.rootPackageJson).length > 0) { diff --git a/packages/build-info/tests/detect-build-systems.test.ts b/packages/build-info/tests/detect-build-systems.test.ts index 69db367d21..cca0926d2b 100644 --- a/packages/build-info/tests/detect-build-systems.test.ts +++ b/packages/build-info/tests/detect-build-systems.test.ts @@ -125,3 +125,13 @@ test('detects multiple build systems in a monorepo setup', async () => { const buildSystems = await detectBuildSystems(path.join(cwd, 'packages/website'), cwd) expect(buildSystems).toEqual([{ name: 'lerna', version: '^2.5.3' }, { name: 'gradle' }]) }) + +test('invalid package json handled gracefully', async () => { + const cwd = mockFileSystem({ + 'package.json': "{ 'devDependencies': { moon: '^0.5.1' } }", + '.moon/toolchain.yml': '', + }) + + const buildSystems = await detectBuildSystems(cwd) + expect(buildSystems[0]).toEqual({ name: 'moon', version: undefined }) +})