From af3ff83dc41c7736b7e57f2a02fd94ac03b76a4c Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Mon, 15 Jan 2024 22:43:29 -0800 Subject: [PATCH] fix: report missing import properly in loadSync (#1960) --- src/root.js | 4 ++-- tests/api_root.js | 23 +++++++++++++++++++++++ tests/data/badimport.proto | 7 +++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/data/badimport.proto diff --git a/src/root.js b/src/root.js index 9441a7fc3..2776f96e4 100644 --- a/src/root.js +++ b/src/root.js @@ -98,10 +98,10 @@ Root.prototype.load = function load(filename, options, callback) { /* istanbul ignore if */ if (!callback) return; - var cb = callback; - callback = null; if (sync) throw err; + var cb = callback; + callback = null; cb(err, root); } diff --git a/tests/api_root.js b/tests/api_root.js index 6e9814ddb..9125d3aa8 100644 --- a/tests/api_root.js +++ b/tests/api_root.js @@ -67,6 +67,29 @@ tape.test("reflected roots", function(test) { }); }); + test.test(test.name + " - missing import", function(test) { + var root = new Root(); + test.plan(2); + root.load("tests/data/badimport.proto", function(err) { + test.ok(err, "should return an error when an imported file does not exist"); + test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found"); + test.end(); + }); + }); + + test.test(test.name + " - missing import, sync load", function(test) { + var root = new Root(); + test.plan(2); + try { + root.loadSync("tests/data/badimport.proto"); + root.resolveAll(); + } catch (err) { + test.ok(err, "should return an error when an imported file does not exist"); + test.match(err.toString(), /nonexistent\.proto/, "should mention the file name which was not found"); + } + test.end(); + }); + test.test(test.name + " - skipped", function(test) { var root = new Root(); root.resolvePath = function() { diff --git a/tests/data/badimport.proto b/tests/data/badimport.proto new file mode 100644 index 000000000..f11caaf6b --- /dev/null +++ b/tests/data/badimport.proto @@ -0,0 +1,7 @@ +syntax = "proto3"; + +import "nonexistent.proto"; + +message Message { + NonExistent field = 1; +}