Skip to content
Merged
8 changes: 7 additions & 1 deletion packages/build-info/src/detect-build-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 8 additions & 4 deletions packages/build-info/src/get-build-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions packages/build-info/tests/detect-build-systems.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})