From 464f84d55f2472b5d8fe2ec8409d9c65f1e29d38 Mon Sep 17 00:00:00 2001 From: jobala Date: Tue, 13 Dec 2022 16:43:29 +0000 Subject: [PATCH 1/8] chore: get buildSystems separately --- packages/build-info/src/get-build-info.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index 224d403a70..57fea65bb4 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -21,12 +21,17 @@ export const getBuildInfo = async (opts: ContextOptions) => { // if the framework or buildSystem 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 } + try { + buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) + } catch { + // noop + } + const info: Info = { frameworks, buildSystems } // only if we find a root package.json we know this is a javascript workspace From a5ca9231499ad8af2b0ddbf7d4a449834614306b Mon Sep 17 00:00:00 2001 From: jobala Date: Tue, 13 Dec 2022 17:01:53 +0000 Subject: [PATCH 2/8] fix: get build system in different try...catch --- packages/build-info/src/get-build-info.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index 57fea65bb4..f7e0a8b0ac 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -27,6 +27,8 @@ export const getBuildInfo = async (opts: ContextOptions) => { } try { + // if buildSystem detection is crashing we should not crash the build info and package-manager + // detection buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) } catch { // noop From cb50c6a62daf7cd444ba689b83c7c9cd0c8ad6e5 Mon Sep 17 00:00:00 2001 From: jobala Date: Tue, 13 Dec 2022 21:10:17 +0000 Subject: [PATCH 3/8] chore: gracefully handle invalid package.json --- packages/build-info/src/detect-build-system.ts | 8 +++++++- packages/build-info/src/get-build-info.ts | 14 +++----------- .../build-info/tests/detect-build-systems.test.ts | 11 +++++++++++ packages/build-info/tests/get-build-info.test.ts | 1 - 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/packages/build-info/src/detect-build-system.ts b/packages/build-info/src/detect-build-system.ts index 04cf9d195f..0089f8aa69 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 { + pkgJson = JSON.parse('{}') + } + return pkgJson } diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index f7e0a8b0ac..84d950b123 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -15,10 +15,9 @@ 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 }) } catch { @@ -26,19 +25,12 @@ export const getBuildInfo = async (opts: ContextOptions) => { // noop } - try { - // if buildSystem detection is crashing we should not crash the build info and package-manager - // detection - buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) - } catch { - // noop - } - - const info: Info = { frameworks, buildSystems } + const info: Info = { frameworks } // only if we find a root package.json we know this is a javascript workspace if (Object.keys(context.rootPackageJson).length > 0) { info.packageManager = await detectPackageManager(context.projectDir, context.rootDir) + info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) const workspaceInfo = await getWorkspaceInfo(info.packageManager, context) if (workspaceInfo) { info.jsWorkspaces = workspaceInfo diff --git a/packages/build-info/tests/detect-build-systems.test.ts b/packages/build-info/tests/detect-build-systems.test.ts index 69db367d21..05a776fd1d 100644 --- a/packages/build-info/tests/detect-build-systems.test.ts +++ b/packages/build-info/tests/detect-build-systems.test.ts @@ -125,3 +125,14 @@ 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) + console.log(buildSystems) + expect(buildSystems[0]).toEqual({ name: 'moon', version: undefined }) +}) diff --git a/packages/build-info/tests/get-build-info.test.ts b/packages/build-info/tests/get-build-info.test.ts index e09d2ab4cd..ceaa9ceb93 100644 --- a/packages/build-info/tests/get-build-info.test.ts +++ b/packages/build-info/tests/get-build-info.test.ts @@ -29,7 +29,6 @@ describe('Golang', () => { }) expect(info).toMatchInlineSnapshot(` { - "buildSystems": [], "frameworks": [], } `) From 56fe29004fe246a2e98990a7b1eaa92a029ebd7e Mon Sep 17 00:00:00 2001 From: Japheth Obala Date: Wed, 14 Dec 2022 08:55:20 +0000 Subject: [PATCH 4/8] Update packages/build-info/tests/detect-build-systems.test.ts Co-authored-by: Lukas Holzer --- packages/build-info/tests/detect-build-systems.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/build-info/tests/detect-build-systems.test.ts b/packages/build-info/tests/detect-build-systems.test.ts index 05a776fd1d..cca0926d2b 100644 --- a/packages/build-info/tests/detect-build-systems.test.ts +++ b/packages/build-info/tests/detect-build-systems.test.ts @@ -133,6 +133,5 @@ test('invalid package json handled gracefully', async () => { }) const buildSystems = await detectBuildSystems(cwd) - console.log(buildSystems) expect(buildSystems[0]).toEqual({ name: 'moon', version: undefined }) }) From b33b2660127f4d5e1b679eee5e7823e722d60542 Mon Sep 17 00:00:00 2001 From: Japheth Obala Date: Wed, 14 Dec 2022 08:56:37 +0000 Subject: [PATCH 5/8] chore: initialize empty package.json Co-authored-by: Lukas Holzer --- packages/build-info/src/detect-build-system.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/build-info/src/detect-build-system.ts b/packages/build-info/src/detect-build-system.ts index 0089f8aa69..83f08fb49c 100644 --- a/packages/build-info/src/detect-build-system.ts +++ b/packages/build-info/src/detect-build-system.ts @@ -169,11 +169,11 @@ const lookFor = ( } const getPkgJson = (configPath: string): PackageJson => { - let pkgJson: PackageJson + let pkgJson: PackageJson = {}; try { pkgJson = JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8')) } catch { - pkgJson = JSON.parse('{}') + // noop } return pkgJson } From 0589d62e501cc63aa4b97b3378d75f7636733e40 Mon Sep 17 00:00:00 2001 From: jobala Date: Wed, 14 Dec 2022 09:02:29 +0000 Subject: [PATCH 6/8] chore: fix formatting error --- packages/build-info/src/detect-build-system.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/build-info/src/detect-build-system.ts b/packages/build-info/src/detect-build-system.ts index 83f08fb49c..494a32fd3b 100644 --- a/packages/build-info/src/detect-build-system.ts +++ b/packages/build-info/src/detect-build-system.ts @@ -169,7 +169,7 @@ const lookFor = ( } const getPkgJson = (configPath: string): PackageJson => { - let pkgJson: PackageJson = {}; + let pkgJson: PackageJson = {} try { pkgJson = JSON.parse(readFileSync(path.join(path.dirname(configPath), 'package.json'), 'utf-8')) } catch { From cc8f195db84ce16b9b90590b8e72c17f753bb47c Mon Sep 17 00:00:00 2001 From: jobala Date: Wed, 14 Dec 2022 09:12:09 +0000 Subject: [PATCH 7/8] chore: return an empty list if no build system detected --- packages/build-info/src/get-build-info.ts | 3 ++- packages/build-info/tests/get-build-info.test.ts | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index 84d950b123..5ae8bfd656 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -27,10 +27,11 @@ export const getBuildInfo = async (opts: ContextOptions) => { const info: Info = { frameworks } + info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) + // only if we find a root package.json we know this is a javascript workspace if (Object.keys(context.rootPackageJson).length > 0) { info.packageManager = await detectPackageManager(context.projectDir, context.rootDir) - info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) const workspaceInfo = await getWorkspaceInfo(info.packageManager, context) if (workspaceInfo) { info.jsWorkspaces = workspaceInfo diff --git a/packages/build-info/tests/get-build-info.test.ts b/packages/build-info/tests/get-build-info.test.ts index ceaa9ceb93..e09d2ab4cd 100644 --- a/packages/build-info/tests/get-build-info.test.ts +++ b/packages/build-info/tests/get-build-info.test.ts @@ -29,6 +29,7 @@ describe('Golang', () => { }) expect(info).toMatchInlineSnapshot(` { + "buildSystems": [], "frameworks": [], } `) From 1d7035df528258a162dec70e4f81be33e5f91366 Mon Sep 17 00:00:00 2001 From: jobala Date: Wed, 14 Dec 2022 15:07:07 +0000 Subject: [PATCH 8/8] chore: wrap build system detection in a try...catch --- packages/build-info/src/get-build-info.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/build-info/src/get-build-info.ts b/packages/build-info/src/get-build-info.ts index 5ae8bfd656..67a71dc64b 100644 --- a/packages/build-info/src/get-build-info.ts +++ b/packages/build-info/src/get-build-info.ts @@ -27,7 +27,11 @@ export const getBuildInfo = async (opts: ContextOptions) => { const info: Info = { frameworks } - info.buildSystems = await detectBuildSystems(context.projectDir, context.rootDir) + 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) {