Skip to content

Commit

Permalink
Propagate errors on module resolution if required module has a bad re…
Browse files Browse the repository at this point in the history
…quire
  • Loading branch information
Lindsey Smith committed Nov 3, 2016
1 parent 9ed367d commit 9fb1bb2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
12 changes: 10 additions & 2 deletions packages/starspot-core/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,15 @@ export default class Resolver {

mod = require(absolutePath);
} catch (e) {
if (e.code === "MODULE_NOT_FOUND") { continue; }
if (e.code === "MODULE_NOT_FOUND") {
// MODULE_NOT_FOUND can be thrown if the file we required exists but required a non-existent module itself
// In that case, we want to throw an error so the user knows to fix their module
try {
require.resolve(absolutePath);
} catch (e) {
continue;
}
}

this.ui.veryVerbose({
name: "resolver-requiring-error",
Expand Down Expand Up @@ -167,4 +175,4 @@ export default class Resolver {

function stripFileExtension(path: string) {
return path.split(".").slice(0, -1).join(".");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

require("ugh-tom-dale");
11 changes: 10 additions & 1 deletion packages/starspot-core/test/resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,13 @@ describe("Resolver", function() {
}
});

});
it("throws if a module is found with an invalid import", function() {
let resolver = new Resolver({
rootPath: fixture("resolver")
});

expect(function() {
resolver.resolve(["controller", "bad-require"]);
}).to.throw();
});
});

0 comments on commit 9fb1bb2

Please sign in to comment.