From 6dfeb9cdf8c305bb9d771c14bf3b8ec6c64ed2ff Mon Sep 17 00:00:00 2001 From: Marcin Szczepanski Date: Tue, 7 May 2024 16:58:45 +1000 Subject: [PATCH] Ensure empty extended config throws a diagnostic (#9701) * Ensure empty extended config throws a diagnostic * Fix linting issue --- .../core/core/src/requests/ParcelConfigRequest.js | 14 ++++++++++++-- .../core/core/test/ParcelConfigRequest.test.js | 9 ++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/core/core/src/requests/ParcelConfigRequest.js b/packages/core/core/src/requests/ParcelConfigRequest.js index bb521c209f3..c9ce9b48244 100644 --- a/packages/core/core/src/requests/ParcelConfigRequest.js +++ b/packages/core/core/src/requests/ParcelConfigRequest.js @@ -27,6 +27,7 @@ import ThrowableDiagnostic, { generateJSONCodeHighlights, escapeMarkdown, md, + errorToDiagnostic, } from '@parcel/diagnostic'; import {parse} from 'json5'; import path from 'path'; @@ -467,7 +468,7 @@ export async function processConfigChain( if (errors.length > 0) { throw new ThrowableDiagnostic({ - diagnostic: errors.flatMap(e => e.diagnostics), + diagnostic: errors.flatMap(e => e.diagnostics ?? errorToDiagnostic(e)), }); } } @@ -574,7 +575,16 @@ export function validateConfigFile( config: RawParcelConfig | ResolvedParcelConfigFile, relativePath: FilePath, ) { - validateNotEmpty(config, relativePath); + try { + validateNotEmpty(config, relativePath); + } catch (e) { + throw new ThrowableDiagnostic({ + diagnostic: { + message: e.message, + origin: '@parcel/core', + }, + }); + } validateSchema.diagnostic( ParcelConfigSchema, diff --git a/packages/core/core/test/ParcelConfigRequest.test.js b/packages/core/core/test/ParcelConfigRequest.test.js index 1b2b168ece3..b1ad8e7eb58 100644 --- a/packages/core/core/test/ParcelConfigRequest.test.js +++ b/packages/core/core/test/ParcelConfigRequest.test.js @@ -309,9 +309,12 @@ describe('ParcelConfigRequest', () => { }); it('should throw error on empty config file', () => { - assert.throws(() => { - validateConfigFile({}, '.parcelrc'); - }, /.parcelrc can't be empty/); + assert.throws( + () => { + validateConfigFile({}, '.parcelrc'); + }, + {name: 'Error', message: ".parcelrc can't be empty"}, + ); }); });