From 8fd790c5c5c3ca44538bc11abda4a973d90c3968 Mon Sep 17 00:00:00 2001 From: "arrz.dev" <55147716+arrzdev@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:40:33 +0000 Subject: [PATCH] fix(native): a plugin whose exports map hides ./package.json is still compiled in and fingerprinted --- bin/lib/native-plugin-discovery.test.mjs | 75 ++++++++++++++++++++---- bin/lib/native.mjs | 45 +++++++++----- src/native/installed-plugins.test.ts | 15 +++++ src/native/installed-plugins.ts | 27 +++++++-- 4 files changed, 127 insertions(+), 35 deletions(-) diff --git a/bin/lib/native-plugin-discovery.test.mjs b/bin/lib/native-plugin-discovery.test.mjs index 25e8706f..c35fb4dd 100644 --- a/bin/lib/native-plugin-discovery.test.mjs +++ b/bin/lib/native-plugin-discovery.test.mjs @@ -8,12 +8,25 @@ // clean build log above it. `@capawesome/capacitor-live-update` — the OTA mechanism — // is the first plugin adaptv ships that the prefix would have dropped, so it is the // fixture here rather than a synthetic name. -import { existsSync, readFileSync } from "node:fs" -import { createRequire } from "node:module" +import { + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from "node:fs" +import { tmpdir } from "node:os" import path from "node:path" -import { describe, expect, it } from "vitest" -import { carriesNativeCode } from "#adaptv/native/installed-plugins.ts" -import { ADAPTV_ROOT, adaptvCapacitorNativePkgs } from "./native.mjs" +import { afterAll, beforeAll, describe, expect, it } from "vitest" +import { + carriesNativeCode, + pluginDir, +} from "#adaptv/native/installed-plugins.ts" +import { + ADAPTV_ROOT, + adaptvCapacitorNativePkgs, + pkgDirResolver, +} from "./native.mjs" const OTA_PLUGIN = "@capawesome/capacitor-live-update" @@ -97,13 +110,49 @@ describe("🔴 the CLI and the OTA fingerprint must answer the same question", ( }) }) -/** Resolve from adaptv, where its own dependencies live. */ +describe("a plugin whose exports map leaves out ./package.json", () => { + //`require.resolve("/package.json")` throws ERR_PACKAGE_PATH_NOT_EXPORTED for + //these, so a resolver built on it drops the plugin: compiled out of the binary by + //the CLI, and invisible to the OTA fingerprint + const SEALED = "@vendor/sealed" + let root + beforeAll(() => { + root = mkdtempSync(path.join(tmpdir(), "adaptv-sealed-")) + writeFileSync( + path.join(root, "package.json"), + JSON.stringify({ dependencies: { [SEALED]: "1.0.0" } }), + ) + const dir = path.join(root, "node_modules", SEALED) + mkdirSync(path.join(dir, "android"), { recursive: true }) + writeFileSync( + path.join(dir, "package.json"), + JSON.stringify({ + name: SEALED, + capacitor: {}, + exports: { ".": "./index.js" }, + }), + ) + writeFileSync(path.join(dir, "index.js"), "") + }) + afterAll(() => rmSync(root, { recursive: true, force: true })) + + it("is compiled in by the CLI", () => { + expect(adaptvCapacitorNativePkgs("android", root)).toContain(SEALED) + }) + + it("is found by the native injectors and preflight", () => { + expect(pkgDirResolver(root)(SEALED)).toBe( + path.join(root, "node_modules", SEALED), + ) + }) + + it("is counted by the fingerprint, in step with the CLI", () => { + expect(carriesNativeCode(pluginDir(SEALED, root, root))).toBe(true) + expect(adaptvCapacitorNativePkgs("ios", root)).toEqual([SEALED]) + }) +}) + +/** Resolve from adaptv, where its own dependencies live — the fingerprint's resolver. */ function dirOf(name) { - const req = createRequire(path.join(ADAPTV_ROOT, "package.json")) - try { - const dir = path.dirname(req.resolve(`${name}/package.json`)) - return existsSync(dir) ? dir : null - } catch { - return null - } + return pluginDir(name, ADAPTV_ROOT, ADAPTV_ROOT) } diff --git a/bin/lib/native.mjs b/bin/lib/native.mjs index 291b1d56..e77e0db9 100644 --- a/bin/lib/native.mjs +++ b/bin/lib/native.mjs @@ -12,6 +12,7 @@ import { mkdtempSync, readdirSync, readFileSync, + realpathSync, rmSync, statSync, writeFileSync, @@ -1138,9 +1139,9 @@ export function restoreGradleWrapperMode(appRoot) { * plugin from anywhere else — `@capawesome/capacitor-live-update`, the OTA mechanism — * compiled fine and then never registered, surfacing at runtime as "plugin is not * implemented" with nothing in the build to explain it. */ -export function adaptvCapacitorNativePkgs(platform) { +export function adaptvCapacitorNativePkgs(platform, root = ADAPTV_ROOT) { const pkg = JSON.parse( - readFileSync(path.join(ADAPTV_ROOT, "package.json"), "utf8"), + readFileSync(path.join(root, "package.json"), "utf8"), ) const skip = new Set([ "@capacitor/cli", @@ -1157,14 +1158,8 @@ export function adaptvCapacitorNativePkgs(platform) { // itself, so adding it here would declare the same Gradle module twice. const forced = platform === "ios" ? "@capacitor/ios" : null - const req = createRequire(path.join(ADAPTV_ROOT, "package.json")) - const dirOf = (name) => { - try { - return path.dirname(req.resolve(`${name}/package.json`)) - } catch { - return null - } - } + const from = path.join(root, "package.json") + const dirOf = (name) => packageDir(from, name) return Object.keys(pkg.dependencies ?? {}).filter( (n) => !skip.has(n) && (n === forced || carriesNativeCode(dirOf(n))), @@ -1213,18 +1208,36 @@ function carriesNativeCode(dir) { * (R39) — preflight refuses exactly the names an injector would have had to skip, so the * check can never come to a different conclusion than the code it is protecting. */ export function pkgDirResolver(appRoot) { - const reqAdaptv = createRequire(path.join(ADAPTV_ROOT, "package.json")) - const reqApp = createRequire(path.join(appRoot, "package.json")) + const roots = [appRoot, ADAPTV_ROOT].map((r) => + path.join(r, "package.json"), + ) return (name) => { - for (const req of [reqApp, reqAdaptv]) { - try { - return path.dirname(req.resolve(`${name}/package.json`)) - } catch {} + for (const from of roots) { + const dir = packageDir(from, name) + if (dir) return dir } return null } } +/** The directory of package `name` as seen from the file `from`, or null. + * + * IMPORTANT: mirrors `packageDir` in `src/native/installed-plugins.ts` (the OTA + * fingerprint's resolver) — same reason as `carriesNativeCode` below. + * + * Walks the `node_modules` chain node itself would search, rather than resolving + * `${name}/package.json`: a package whose `exports` map leaves out `./package.json` + * makes that resolve throw ERR_PACKAGE_PATH_NOT_EXPORTED, and the plugin silently + * dropped out of the build. Real path, as `require.resolve` returned. */ +function packageDir(from, name) { + for (const base of createRequire(from).resolve.paths(name) ?? []) { + const dir = path.join(base, name) + if (existsSync(path.join(dir, "package.json"))) + return realpathSync(dir) + } + return null +} + /** Scan a plugin package's iOS sources for its registered class name(s) — mirrors * @capacitor/cli's `findPluginClasses` (`@objc(Name)` for Swift, `CAP_PLUGIN(Name` for * ObjC). These are the entries the runtime needs in `packageClassList` to REGISTER a plugin. */ diff --git a/src/native/installed-plugins.test.ts b/src/native/installed-plugins.test.ts index 443ffe12..25a73fbd 100644 --- a/src/native/installed-plugins.test.ts +++ b/src/native/installed-plugins.test.ts @@ -198,6 +198,21 @@ describe("pluginDir", () => { ) }) + it("finds a plugin whose exports map leaves out ./package.json", () => { + //`require.resolve("/package.json")` throws ERR_PACKAGE_PATH_NOT_EXPORTED + //for these, and a null here drops the plugin out of the OTA fingerprint + const app = writePackage(tempDir(), {}) + const dir = writePackage( + path.join(app, "node_modules/@vendor/sealed"), + { + name: "@vendor/sealed", + exports: { ".": "./index.js" }, + }, + ) + writeFileSync(path.join(dir, "index.js"), "") + expect(pluginDir("@vendor/sealed", app, undefined)).toBe(dir) + }) + it("is null when neither root can see the package", () => { const app = writePackage(tempDir(), {}) const adaptvRoot = writePackage(tempDir(), {}) diff --git a/src/native/installed-plugins.ts b/src/native/installed-plugins.ts index 185644fb..accef2bf 100644 --- a/src/native/installed-plugins.ts +++ b/src/native/installed-plugins.ts @@ -1,4 +1,4 @@ -import { existsSync, readFileSync } from "node:fs" +import { existsSync, readFileSync, realpathSync } from "node:fs" import { createRequire } from "node:module" import path from "node:path" import { fileURLToPath } from "node:url" @@ -89,11 +89,26 @@ export function pluginDir( path.join(appRoot, "package.json"), adaptvResolveFrom(adaptvRoot), ]) { - try { - return path.dirname( - createRequire(from).resolve(`${pkg}/package.json`), - ) - } catch {} + const dir = packageDir(from, pkg) + if (dir) return dir + } + return null +} + +/** + * The directory of package `name` as seen from the file `from`, or null. + * + * Walks the `node_modules` chain node itself would search, rather than resolving + * `${name}/package.json`: a plugin whose `exports` map leaves out `./package.json` + * makes that resolve throw ERR_PACKAGE_PATH_NOT_EXPORTED, and the plugin silently + * dropped out of the fingerprint. Real path, as `require.resolve` returned. + * Mirrored by `packageDir` in `bin/lib/native.mjs`. + */ +function packageDir(from: string, name: string): string | null { + for (const base of createRequire(from).resolve.paths(name) ?? []) { + const dir = path.join(base, name) + if (existsSync(path.join(dir, "package.json"))) + return realpathSync(dir) } return null }