diff --git a/.changeset/log-package-resolution-errors.md b/.changeset/log-package-resolution-errors.md new file mode 100644 index 00000000..24b55dd4 --- /dev/null +++ b/.changeset/log-package-resolution-errors.md @@ -0,0 +1,5 @@ +--- +"react-native-node-api": patch +--- + +Log the original package resolution error during dependency scanning so invalid package entry points include the missing path. diff --git a/packages/host/src/node/path-utils.test.ts b/packages/host/src/node/path-utils.test.ts index 85d76e3c..7d8e62fd 100644 --- a/packages/host/src/node/path-utils.test.ts +++ b/packages/host/src/node/path-utils.test.ts @@ -2,6 +2,7 @@ import assert from "node:assert/strict"; import { describe, it } from "node:test"; import path from "node:path"; import fs from "node:fs"; +import { createRequire } from "node:module"; import fswin from "fswin"; import { @@ -9,6 +10,7 @@ import { findNodeApiModulePaths, findNodeAddonForBindings, findPackageDependencyPaths, + resolvePackageRoot, getLibraryName, isNodeApiModule, stripExtension, @@ -355,6 +357,34 @@ describe("findPackageDependencyPaths", () => { }); }); +describe("resolvePackageRoot", () => { + it("logs the original package resolution error", (context) => { + const tempDir = setupTempDirectory(context, { + "node_modules/broken-package/package.json": JSON.stringify({ + name: "broken-package", + main: "missing.js", + }), + }); + const requireFromRoot = createRequire(path.join(tempDir, "noop.js")); + const debug = context.mock.method(console, "debug", () => {}); + + assert.equal( + resolvePackageRoot(requireFromRoot, "broken-package"), + undefined, + ); + assert.equal(debug.mock.callCount(), 1); + + const call = debug.mock.calls[0]; + assert(call); + assert.equal(call.arguments.length, 1); + assert.equal(typeof call.arguments[0], "string"); + assert.match( + String(call.arguments[0]), + /Failed to resolve package root for "broken-package": Cannot find module .*broken-package[\\/]missing\.js/, + ); + }); +}); + describe("findNodeApiModulePaths", () => { it("should find .apple.node paths", async (context) => { const tempDir = setupTempDirectory(context, { diff --git a/packages/host/src/node/path-utils.ts b/packages/host/src/node/path-utils.ts index 0cb4506a..79f08e86 100644 --- a/packages/host/src/node/path-utils.ts +++ b/packages/host/src/node/path-utils.ts @@ -260,8 +260,11 @@ export function resolvePackageRoot( try { const resolvedPath = requireFromPackageRoot.resolve(packageName); return packageDirectorySync({ cwd: resolvedPath }); - } catch { - // TODO: Add a debug log here + } catch (error) { + const message = error instanceof Error ? error.message : String(error); + console.debug( + `Failed to resolve package root for "${packageName}": ${message}`, + ); return undefined; } }