Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 62 additions & 13 deletions bin/lib/native-plugin-discovery.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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("<pkg>/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)
}
45 changes: 29 additions & 16 deletions bin/lib/native.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
mkdtempSync,
readdirSync,
readFileSync,
realpathSync,
rmSync,
statSync,
writeFileSync,
Expand Down Expand Up @@ -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",
Expand All @@ -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))),
Expand Down Expand Up @@ -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. */
Expand Down
15 changes: 15 additions & 0 deletions src/native/installed-plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,21 @@ describe("pluginDir", () => {
)
})

it("finds a plugin whose exports map leaves out ./package.json", () => {
//`require.resolve("<pkg>/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(), {})
Expand Down
27 changes: 21 additions & 6 deletions src/native/installed-plugins.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Loading