From de059211b629acf73b57db03cedc8d5fca357487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Go=C5=82=C4=99biowski-Owczarek?= Date: Wed, 13 Mar 2024 15:54:36 +0100 Subject: [PATCH] Fix the shouldUseGitHubOverride logic for ESM imports The `shouldUseGitHubOverride` util is called with `Module._load` parameters. So far, it assumed it will get a string `request` and a `NodeModule` `parent` parameters. However, for ESM imports `parent` is actually `undefined`. Account for this in code by making `parent` an optional argument; add a test. Fixes gh-1421 --- source/platforms/github/_tests/_customGitHubRequire.test.ts | 6 ++++++ source/platforms/github/customGitHubRequire.ts | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/platforms/github/_tests/_customGitHubRequire.test.ts b/source/platforms/github/_tests/_customGitHubRequire.test.ts index 8bdb9fc86..7bcc6c0ed 100644 --- a/source/platforms/github/_tests/_customGitHubRequire.test.ts +++ b/source/platforms/github/_tests/_customGitHubRequire.test.ts @@ -31,6 +31,12 @@ describe("shouldUseGitHubOverride", () => { const parent: any = { filename: dangerPrefix + "./my-import" } expect(shouldUseGitHubOverride(module, parent)).toBeTruthy() }) + + it("ignores modules without a parent", () => { + const module = "./peril" + const parent: any = undefined + expect(shouldUseGitHubOverride(module, parent)).toBeFalsy() + }) }) describe("customGitHubResolveRequest", () => { diff --git a/source/platforms/github/customGitHubRequire.ts b/source/platforms/github/customGitHubRequire.ts index 106851e48..5f6b4c134 100644 --- a/source/platforms/github/customGitHubRequire.ts +++ b/source/platforms/github/customGitHubRequire.ts @@ -86,9 +86,9 @@ export async function getGitHubFileContents( // Setup a callback used to determine whether a specific `require` invocation // needs to be overridden. -export const shouldUseGitHubOverride = (request: string, parent: NodeModule): boolean => { +export const shouldUseGitHubOverride = (request: string, parent?: NodeModule): boolean => { // Is it a from a file we're handling, and is it relative? - if (parent.filename.startsWith(dangerPrefix) && request.startsWith(".")) { + if (parent?.filename.startsWith(dangerPrefix) && request.startsWith(".")) { return true } // Basically any import that's not a relative import from a Dangerfile