From 7c18ed908fef034d62a876cbe469d39851b3bf00 Mon Sep 17 00:00:00 2001 From: Willy Antunez Date: Thu, 5 Nov 2026 19:18:30 -0600 Subject: [PATCH 1/2] fix: copy every header the bare project references --- src/generate/bareSources.ts | 16 ++++++-- src/project/pbxproj.ts | 43 ++++++++++++++++------ tests/generate.test.ts | 29 +++++++++++++++ tests/project/pbxproj.test.ts | 69 +++++++++++++++++++++++++++++++++-- 4 files changed, 138 insertions(+), 19 deletions(-) diff --git a/src/generate/bareSources.ts b/src/generate/bareSources.ts index 1be9125..3de48ac 100644 --- a/src/generate/bareSources.ts +++ b/src/generate/bareSources.ts @@ -1,3 +1,4 @@ +import { existsSync } from "node:fs"; import { cp, mkdir, writeFile } from "node:fs/promises"; import path from "node:path"; import { compiledName, type BareTargetContent, type TargetFile } from "../project/index.js"; @@ -38,9 +39,9 @@ async function copyListed(file: TargetFile, dest: string): Promise { * target folder (`ios//Native/X.m` → `Native/X.m`; files elsewhere in ios/ keep their * path from ios/), with one forwarding header per header in `include/` so `#import "X.h"` resolves * from any folder as Xcode's header maps allow, plus `-Swift.h` forwarding to `App-Swift.h` - * in a mixed target. A C-family `main.m` (any `main.`) is copied as `AppMain.` in its folder, so SwiftPM keeps - * App a library target. Resources are staged flat in `resourcesDir`, laid - * out as Xcode copies them to the bundle root (xtool.yml lists each top-level entry). + * in a mixed target. A header the project references that does not exist is skipped with a note. A C-family `main.m` + * (any `main.`) is copied as `AppMain.` in its folder, so SwiftPM keeps App a library target. Resources are + * staged flat in `resourcesDir`, laid out as Xcode copies them to the bundle root (xtool.yml lists each top-level entry). */ export async function copyBareTarget( bare: BareTargetContent, @@ -74,8 +75,14 @@ export async function copyBareTarget( const code = [...bare.swiftSources, ...bare.cSources, ...bare.headers, ...(bare.bridgingHeader ? [bare.bridgingHeader] : [])]; const cSources = new Set(bare.cSources); + const headers = new Set(bare.headers); const codeDest = new Map(); for (const file of code) { + // Xcode builds with a referenced header missing until a source imports it; so does the generated target. + if (headers.has(file) && !existsSync(file.absPath)) { + log(`ℹ Skipped ${file.relPath}: the Xcode project references this header, but the file does not exist`); + continue; + } let dest = place(file); const base = path.posix.basename(dest); const name = cSources.has(file) ? compiledName(dest) : base; @@ -95,7 +102,8 @@ export async function copyBareTarget( const forwarded = new Map(); for (const header of bare.headers) { - const dest = codeDest.get(header)!; + const dest = codeDest.get(header); + if (dest === undefined) continue; const base = path.posix.basename(dest); const first = forwarded.get(base); if (first !== undefined) { diff --git a/src/project/pbxproj.ts b/src/project/pbxproj.ts index ff6afa1..fcc0521 100644 --- a/src/project/pbxproj.ts +++ b/src/project/pbxproj.ts @@ -208,6 +208,9 @@ const SWIFT = /\.swift$/i; const C_SOURCE = /\.(m|mm|c|cc|cxx|cpp)$/i; const HEADER = /\.(h|hpp)$/i; const SKIPPED_RESOURCE = /\.(storyboard|xcassets|xib)$/i; +/** Folders of ios/ that hold no app headers: CocoaPods' and build output. */ +const NOT_APP_DIRS = new Set(["Pods", "build", "DerivedData"]); +const FRAMEWORK_DIR = /\.(xc)?framework$/i; const IGNORABLE_FLAGS = ["OTHER_CFLAGS", "OTHER_LDFLAGS", "OTHER_SWIFT_FLAGS"]; /** Flags the template sets that the generated project already provides. */ const BENIGN_FLAGS = new Set(["$(inherited)", "-ObjC", "-lc++"]); @@ -386,19 +389,35 @@ export function readTargetContent(p: Pbxproj, iosDir: string): BareTargetContent (SKIPPED_RESOURCE.test(file.relPath) ? skippedResources : resources).push(file); } - // Headers are rarely in a build phase: take every header under the app target's folder group. - const infoPlist = resolveSetting(p, "INFOPLIST_FILE"); - const folder = infoPlist ? path.posix.dirname(infoPlist) : undefined; - if (folder && folder !== ".") { - const group = - [...groupPaths.entries()].find(([uuid, g]) => uuid !== mainGroup && g.path === folder) ?? - [...groupPaths.entries()].find(([uuid, g]) => uuid !== mainGroup && g.path === "" && g.name === folder); - for (const uuid of group?.[1].files ?? []) { - const file = fileOf(uuid); - if (!HEADER.test(file.relPath)) continue; - if (!headers.some((h) => h.relPath === file.relPath)) headers.push(file); - } + // Headers are rarely in a build phase. Xcode's header map makes every header the project references importable as + // "X.h" from any source of the target: take each one under ios/, outside Pods, build/, frameworks and the Products + // group. Headers under the app folder come first, so they win a basename shared with another header. + const firstProject = p.project.getFirstProject().firstProject; + const mainChildren = ((objs.PBXGroup?.[mainGroup]?.children ?? []) as ({ value: string } | string)[]).map(ref); + const skippedGroups = [ + String(firstProject.productRefGroup ?? ""), + ...mainChildren.filter((uuid) => { + const g = groupPaths.get(uuid); + return g !== undefined && (g.path === "Pods" || g.name === "Pods"); + }), + ]; + const skippedRefs = new Set(skippedGroups.flatMap((uuid) => groupPaths.get(uuid)?.files ?? [])); + const referenced: TargetFile[] = []; + const taken = new Set(headers.map((h) => h.relPath)); + for (const [uuid, placed] of files) { + if (placed === undefined || skippedRefs.has(uuid) || !HEADER.test(placed)) continue; + const file = toFile(placed); + const parts = file.relPath.split("/"); + if (path.posix.isAbsolute(file.relPath) || parts[0] === ".." || NOT_APP_DIRS.has(parts[0]!)) continue; + if (parts.some((part) => FRAMEWORK_DIR.test(part))) continue; + if (taken.has(file.relPath)) continue; + taken.add(file.relPath); + referenced.push(file); } + const infoPlist = resolveSetting(p, "INFOPLIST_FILE"); + const folder = infoPlist ? path.posix.dirname(infoPlist) : "."; + const inFolder = (f: TargetFile): boolean => folder !== "." && f.relPath.startsWith(`${folder}/`); + headers.push(...referenced.filter(inFolder), ...referenced.filter((f) => !inFolder(f))); const seen = new Map(); for (const f of [...swiftSources, ...cSources]) { diff --git a/tests/generate.test.ts b/tests/generate.test.ts index 445f194..8a02ca5 100644 --- a/tests/generate.test.ts +++ b/tests/generate.test.ts @@ -987,6 +987,35 @@ describe("writeIosXtool", () => { await expect(writeIosXtool(ctx)).rejects.toThrow(/HelloWorld\/include\/Api\.h/); }); + /** The with-objc pbxproj with `file` (relative to ios/) a direct child of the main group, in no build phase. */ + const withMainGroupFile = (src: string, file: string): string => + src + .replace( + "/* End PBXFileReference section */", + `\t\tD10000000000000000000003 = {isa = PBXFileReference; path = "${file}"; sourceTree = ""; };\n/* End PBXFileReference section */`, + ) + .replace(/(83CBB9F61A601CBA00E9B192 = \{\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = \(\n)/, "$1\t\t\t\tD10000000000000000000003,\n"); + + it("copies a header of the main group with its forward, so the app's main.m finds it", async () => { + const { app, paths, ctx } = await bareProject({ pbxproj: (src) => withMainGroupFile(withAppSource(src, "main.m"), "AppDelegate.h") }); + expect(ctx.project.bare!.headers.map((f) => f.relPath)).toContain("AppDelegate.h"); + await writeFile(join(app, "main.m"), '#import "AppDelegate.h"\nint main(int argc, char *argv[]) { return 0; }\n'); + await writeFile(join(app, "..", "AppDelegate.h"), "@interface AppDelegate : NSObject\n@end\n"); + await writeIosXtool(ctx); + expect(await readFile(join(paths.sources, "AppDelegate.h"), "utf8")).toBe("@interface AppDelegate : NSObject\n@end\n"); + expect(await readFile(join(paths.sources, "include", "AppDelegate.h"), "utf8")).toBe('#import "../AppDelegate.h"\n'); + expect(await readFile(join(paths.sources, "AppMain.m"), "utf8")).toContain('#import "AppDelegate.h"'); + }); + + it("skips a header the project references but that does not exist, saying so", async () => { + const { paths, ctx, logs } = await bareProject({ pbxproj: (src) => withMainGroupFile(src, "Gone.h") }); + await writeIosXtool(ctx); + expect(logs).toContain("ℹ Skipped Gone.h: the Xcode project references this header, but the file does not exist"); + await expect(readFile(join(paths.sources, "Gone.h"), "utf8")).rejects.toThrow(); + await expect(readFile(join(paths.sources, "include", "Gone.h"), "utf8")).rejects.toThrow(); + expect(await readFile(join(paths.sources, "include", "NativeModule.h"), "utf8")).toBe('#import "../Native/NativeModule.h"\n'); + }); + it("keeps .bundle folders and files inside a .bundle together", async () => { const { app, paths, ctx } = await bareProject(); await mkdir(join(app, "Assets", "Maps.bundle", "tiles"), { recursive: true }); diff --git a/tests/project/pbxproj.test.ts b/tests/project/pbxproj.test.ts index 8164a80..48bb17d 100644 --- a/tests/project/pbxproj.test.ts +++ b/tests/project/pbxproj.test.ts @@ -312,12 +312,75 @@ describe("readTargetContent", () => { expect((await read(derived)).content().productModuleName).toBe("Hello_App"); }); - it("includes app-folder headers that are in no build phase, and only those", async () => { + it("includes app-folder headers that are in no build phase", async () => { + const src = addFile(await withObjc(), { path: "Bridge.hpp", group: HELLO }); + const { content } = await read(src); + expect(rel(content().headers)).toEqual(["HelloWorld/Bridge.hpp", "HelloWorld/Native/NativeModule.h"]); + }); + + const PODS = "BBD78D7AC51CEA395F1C20DB"; + const PRODUCTS = "83CBBA001A601CBA00E9B192"; + + /** Adds a PBXGroup `uuid` (with `path` and/or `name`) as a child of `parent`. */ + function addGroup(pbxproj: string, o: { uuid: string; parent: string; path?: string; name?: string }): string { + const fields = `${o.name ? `\t\t\tname = "${o.name}";\n` : ""}${o.path ? `\t\t\tpath = "${o.path}";\n` : ""}`; + const re = new RegExp(`(${o.parent}[^=\\n]*= \\{\\n\\t\\t\\tisa = PBXGroup;\\n\\t\\t\\tchildren = \\(\\n)`); + expect(pbxproj).toMatch(re); + return pbxproj + .replace(re, `$1\t\t\t\t${o.uuid},\n`) + .replace( + "/* End PBXGroup section */", + `\t\t${o.uuid} = {\n\t\t\tisa = PBXGroup;\n\t\t\tchildren = (\n\t\t\t);\n${fields}\t\t\tsourceTree = "";\n\t\t};\n/* End PBXGroup section */`, + ); + } + + it("includes a header that is a direct child of the main group, after the app folder's", async () => { let src = await withObjc(); + src = addFile(src, { path: "AppDelegate.h", group: MAIN }); src = addFile(src, { path: "Bridge.hpp", group: HELLO }); - src = addFile(src, { path: "Outside.h", group: MAIN }); + const { ios, content } = await read(src); + const headers = content().headers; + expect(headers.map((h) => h.relPath)).toEqual(["HelloWorld/Bridge.hpp", "HelloWorld/Native/NativeModule.h", "AppDelegate.h"]); + expect(headers[2]!.absPath).toBe(path.join(ios, "AppDelegate.h")); + }); + + it("includes headers of any group of the project, in groups with and without a path", async () => { + let src = await withObjc(); + src = addGroup(src, { uuid: "C50000000000000000000001", parent: MAIN, path: "Native" }); + src = addFile(src, { path: "Helpers.h", group: "C50000000000000000000001" }); + src = addGroup(src, { uuid: "C50000000000000000000002", parent: MAIN, name: "Support" }); + src = addFile(src, { path: "Support/Keys.h", group: "C50000000000000000000002" }); + src = addFile(src, { path: "Shared/Util.hpp", sourceTree: "SOURCE_ROOT", group: "C50000000000000000000002" }); const { content } = await read(src); - expect(rel(content().headers)).toEqual(["HelloWorld/Bridge.hpp", "HelloWorld/Native/NativeModule.h"]); + expect(rel(content().headers)).toEqual([ + "HelloWorld/Native/NativeModule.h", + "Native/Helpers.h", + "Shared/Util.hpp", + "Support/Keys.h", + ]); + }); + + it("leaves out headers of Pods, build/, frameworks and the Products group", async () => { + let src = await withObjc(); + src = addFile(src, { path: "Headers/Public/React/RCTBridge.h", group: PODS }); + src = addFile(src, { path: "Pods/Headers/Public/Yoga/Yoga.h", group: MAIN }); + src = addFile(src, { path: "build/Generated/Codegen.h", group: MAIN }); + src = addFile(src, { path: "Vendor/Lib.xcframework/ios-arm64/Headers/Lib.h", group: MAIN }); + src = addFile(src, { path: "Vendor/Kit.framework/Headers/Kit.h", group: MAIN }); + src = addFile(src, { path: "Product.h", group: PRODUCTS }); + src = addGroup(src, { uuid: "C50000000000000000000003", parent: MAIN, name: "Pods" }); + src = addFile(src, { path: "Target Support Files/Pods-HelloWorld/Pods-HelloWorld-umbrella.h", group: "C50000000000000000000003" }); + const { content } = await read(src); + expect(rel(content().headers)).toEqual(["HelloWorld/Native/NativeModule.h"]); + }); + + it("leaves out headers outside ios/, without an error", async () => { + let src = await withObjc(); + src = addFile(src, { path: "../shared/Shared.h", group: MAIN }); + src = addFile(src, { path: "/opt/shared/Abs.h", sourceTree: "", group: MAIN }); + src = addFile(src, { path: "Gen.h", sourceTree: "BUILT_PRODUCTS_DIR", group: MAIN }); + const { content } = await read(src); + expect(rel(content().headers)).toEqual(["HelloWorld/Native/NativeModule.h"]); }); it("keeps an Objective-C main.m among the C sources", async () => { From 0ca156a752df216b03d42160328cd0711065ca46 Mon Sep 17 00:00:00 2001 From: Willy Antunez Date: Thu, 5 Nov 2026 19:19:21 -0600 Subject: [PATCH 2/2] docs: describe header copying in bare apps --- CHANGELOG.md | 4 ++++ docs/bare-react-native.md | 14 +++++++++++--- docs/troubleshooting.md | 8 ++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bcd3d5a..b06f347 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ versions follow [Semantic Versioning](https://semver.org/) with `alpha` prerelea as `AppMain.m` in its folder (any C-family `main.` as `AppMain.`), and the build says so in an `ℹ` line. A `main.swift` is refused early, by `build` and `doctor --project`, with the remedy: mark the app delegate class `@main` and remove `main.swift` from the app target. +- Bare app targets whose headers sit outside the app folder group (an Objective-C `AppDelegate.h` next to the project) + compile: every header the project references under `ios/`, outside `Pods/`, is copied with its forward in + `Sources/App/include`. A referenced header that does not exist is skipped with an `ℹ` line instead of stopping the + build. ## [0.1.0-alpha.6] - 2026-11-05 diff --git a/docs/bare-react-native.md b/docs/bare-react-native.md index 91bdb9e..8a5cf9a 100644 --- a/docs/bare-react-native.md +++ b/docs/bare-react-native.md @@ -218,9 +218,17 @@ list dictionary ([troubleshooting.md](troubleshooting.md#entitlements)). ## Sources The target's Sources phase is copied to `ios-xtool/Sources/App` keeping its layout: `.swift`, `.m`, `.mm`, `.c`, -`.cpp`/`.cc`/`.cxx` and headers (every header in the app folder too), with `.lproj` variant groups. Any other source -type (`.xcdatamodeld`, `.intentdefinition`, `.s`, ...) stops the build with `unsupported source type in the app target`. -Sources outside `ios/` and two compiled files with the same name are errors. +`.cpp`/`.cc`/`.cxx` and headers, with `.lproj` variant groups. Any other source type (`.xcdatamodeld`, +`.intentdefinition`, `.s`, ...) stops the build with `unsupported source type in the app target`. Sources outside +`ios/` and two compiled files with the same name are errors. + +Every header the Xcode project references under `ios/`, in any group and outside `Pods/`, `build/`, frameworks and the +Products group, is copied too, with a forward in `Sources/App/include`, so `#import "X.h"` resolves from any source of +the target as it does with Xcode's header map: an `AppDelegate.h` next to the `.xcodeproj` is found from +`/main.m`. When two headers share a name, a header of the Sources phase or of the app folder wins and the +build prints `ℹ Two app headers are named X.h (…)`. A referenced header that does not exist is skipped with `ℹ Skipped : the Xcode +project references this header, but the file does not exist`. Headers outside `ios/` are not copied: move them into +`ios/`. SwiftPM takes a target with a compiled file named `main.` (in any case) for an executable target, and xtool links the app's entry point from the App library, so a C-family `main` file is copied under another name in its folder: diff --git a/docs/troubleshooting.md b/docs/troubleshooting.md index 20c5c53..d1dd63f 100644 --- a/docs/troubleshooting.md +++ b/docs/troubleshooting.md @@ -867,6 +867,14 @@ an `ios/build/pod-binaries` from an earlier build stays on disk, unused ([Leavin before the patches with "The app target compiles /main.swift: …", which gives the remedy: mark the app delegate class `@main`, move any other top-level code into it, and remove `main.swift` from the app target. See [Sources](bare-react-native.md#sources). +- **"'AppDelegate.h' file not found"** (bare React Native, compiling `ios-xtool/Sources/App/AppMain.m` or another + source of the app target): the header sits outside the app folder's group in the Xcode project, typically next to + the `.xcodeproj` in the project's main group with `main.m` and `AppDelegate.mm`. rn-xtool 0.1.0-alpha.6 and earlier + copy only the headers of the Sources phase and of the app folder's group, so the header never reaches + `Sources/App`. Later releases copy every header the project references under `ios/` (outside `Pods/`), each with a + forward in `Sources/App/include`, so `#import "AppDelegate.h"` resolves as with Xcode's header map. A header outside + `ios/` is still not copied: move it into `ios/` and add it to the project. See + [Sources](bare-react-native.md#sources). - **"rn-xtool could not patch ":** your project has a different version of a package than the one this release of rn-xtool patches, and the patch does not apply to it (on SDK 57 or 58, a newer patch of the SDK that changed a patched file; on bare React Native, a newer patch of its line). The message names the versions. On SDK 58 (Expo