From f674f354f0260c57d885c181e16c9ce23ac252a8 Mon Sep 17 00:00:00 2001 From: Daniel Stockman Date: Thu, 7 Jun 2018 17:29:36 -0700 Subject: [PATCH] fix(project): Report syntax errors in root package.json Fixes #1452 --- core/command/__tests__/command.test.js | 17 +++++++++++++++++ .../pkg-prop-syntax-error/package.json | 14 ++++++++++++++ core/project/__tests__/project.test.js | 13 +++++++++++++ core/project/index.js | 6 +++++- 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 core/project/__fixtures__/pkg-prop-syntax-error/package.json diff --git a/core/command/__tests__/command.test.js b/core/command/__tests__/command.test.js index 760c8b5d2e..df87faab5e 100644 --- a/core/command/__tests__/command.test.js +++ b/core/command/__tests__/command.test.js @@ -448,6 +448,23 @@ describe("core-command", () => { } }); + it("throws JSONError when root package.json has syntax error", async () => { + expect.assertions(1); + + const cwd = await initFixture("basic"); + + await fs.writeFile( + path.join(cwd, "package.json"), // trailing comma ...v + '{ "name": "invalid", "lerna": { "version": "1.0.0" }, }' + ); + + try { + await testFactory({ cwd }); + } catch (err) { + expect(err.prefix).toBe("JSONError"); + } + }); + it("throws ENOLERNA when lerna.json is not found", async () => { expect.assertions(1); diff --git a/core/project/__fixtures__/pkg-prop-syntax-error/package.json b/core/project/__fixtures__/pkg-prop-syntax-error/package.json new file mode 100644 index 0000000000..2c32271ce4 --- /dev/null +++ b/core/project/__fixtures__/pkg-prop-syntax-error/package.json @@ -0,0 +1,14 @@ +{ + "name": "pkg-prop-syntax-error", + "version": "0.0.0-root", + "private": true, + "lerna": { + "loglevel": "success", + "command": { + "publish": { + "loglevel": "verbose" + } + }, + "version": "1.0.0" + },, +} diff --git a/core/project/__tests__/project.test.js b/core/project/__tests__/project.test.js index 3d35227007..e007aa71bd 100644 --- a/core/project/__tests__/project.test.js +++ b/core/project/__tests__/project.test.js @@ -85,6 +85,19 @@ describe("Project", () => { }); }); + it("errors when root package.json is not valid JSON", async () => { + expect.assertions(2); + + const cwd = await initFixture("pkg-prop-syntax-error"); + + try { + const project = new Project(cwd); // eslint-disable-line no-unused-vars + } catch (err) { + expect(err.name).toBe("ValidationError"); + expect(err.prefix).toBe("JSONError"); + } + }); + it("extends local shared config", async () => { const cwd = await initFixture("extends"); const project = new Project(cwd); diff --git a/core/project/index.js b/core/project/index.js index 2a369f7b85..3db8e6cc5a 100644 --- a/core/project/index.js +++ b/core/project/index.js @@ -111,7 +111,11 @@ class Project { value: manifest, }); } catch (err) { - // syntax errors are already caught and reported by constructor + // redecorate JSON syntax errors, avoid debug dump + if (err.name === "JSONError") { + throw new ValidationError(err.name, err.message); + } + // try again next time }