Skip to content

Commit cf3704f

Browse files
authored
Mark SwiftPM remote-package mode as planned in the docs
## Summary The SwiftPM docs describe remote-package mode as a feature that works today. In remote mode, React Native is fetched by URL (`RN_SPM_REMOTE_URL`). This mode is planned and not supported. The text made library authors think that their `Package.swift` needs a remote form. It also made them think that relative paths to the app's folders are a workaround. This PR changes only docs: 1. The "Remote-package mode" section is now "Remote-package mode (planned)". A note at the top says that the mode is not supported. The design text stays below the note. 2. The path table no longer lists remote alternatives. 3. Rule 1 of the hand-authored library contract now shows the supported local form. A library refers to `../../../../xcframeworks` (React Native) and `../../../ios` (the app's `React-GeneratedCode` package) from its link folder `build/generated/autolinking/libs/<Name>`. 4. The docs say that these relative paths are the intended design and that the folders they point to are stable. 5. A new rule 2 says that each library target defines `RCT_NEW_ARCH_ENABLED=1` in `cSettings` and `cxxSettings`. SwiftPM mode supports only the New Architecture, and nothing else sets this define for a SwiftPM target. Libraries keep their `#ifdef` gates, because the same sources still build with CocoaPods. 6. The old rule 2 (the library ships its own generated code) is now rule 3, and it is optional. 7. `spm-scripts.md`, `spm-autolinking-plugins.md` and the docs index mark the remote environment variables, the remote build error and the remote `packageRef` shape as planned. The `RN_SPM_REMOTE_URL` code does not change. ## Changelog: [INTERNAL] [CHANGED] - Mark SwiftPM remote-package mode as planned in the SwiftPM docs ## Test Plan: - `npx prettier --check packages/react-native/scripts/spm/__docs__/*.md` prints "All matched files use Prettier code style!". - `git grep remote-package-mode` finds only links to the new `#remote-package-mode-planned` anchor. - I compared the package names, products and relative paths in rule 1 with `scripts/spm/spm-utils.js`, `generate-spm-autolinking.js` and `scaffold-package-swift.js`. Differential Revision: D129354597 Pull Request resolved: #59631
1 parent 5423038 commit cf3704f

4 files changed

Lines changed: 77 additions & 32 deletions

File tree

‎packages/react-native/scripts/spm/__docs__/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Three documents cover the design, each owning one area:
5050
| Document | Covers |
5151
| -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
5252
| [spm-scripts.md](./spm-scripts.md) | The tool itself: CLI surface, the six-step pipeline, [every file it creates or modifies](./spm-scripts.md#files-the-tool-touches), the two auto-sync hooks, and how Debug/Release flavor selection works. |
53-
| [spm-header-paths-contract.md](./spm-header-paths-contract.md) | How headers and package references resolve. The contract is **zero-`-I`**: no generated manifest carries a search path into React Native's own headers. Also covers remote mode. |
53+
| [spm-header-paths-contract.md](./spm-header-paths-contract.md) | How headers and package references resolve. The contract is **zero-`-I`**: no generated manifest carries a search path into React Native's own headers. Also covers the planned remote mode. |
5454
| [spm-autolinking-plugins.md](./spm-autolinking-plugins.md) | The extension seam for frameworks with their own module system (Expo is the first consumer): discovery, the full context/return contract including `flavoredFrameworks`, `watchPaths` and `scriptPhases`, and failure behavior. |
5555

5656
Two ideas explain most of the architecture:

‎packages/react-native/scripts/spm/__docs__/spm-autolinking-plugins.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,14 @@ only, simulator only, …), the script must check `$CONFIGURATION` /
305305
#### `context.react` — depending on React
306306

307307
A plugin that emits its own `Package.swift` must declare React as a dependency.
308-
React Native's package path, identity, and product names differ between local
309-
and remote mode, and they **move as RN repackages**. So take them from
310-
`context.react` rather than re-deriving them:
308+
React Native's package path, identity, and product names **move as RN
309+
repackages**. So take them from `context.react` rather than re-deriving them:
311310

312311
```js
313312
react: {
314313
packageRef:
315314
{name: 'ReactNative', path: '<absolute>', relPath: '<relative-to-outputDir>'} // local
316-
| {name: '<identity>', url: '<url>', version: '<version>'}, // remote (SPM-resolved)
315+
| {name: '<identity>', url: '<url>', version: '<version>'}, // remote (planned, unsupported)
317316
products: [
318317
{name: 'ReactHeaders', package: 'ReactNative'},
319318
{name: 'ReactNativeHeaders', package: 'ReactNative'},
@@ -323,7 +322,11 @@ react: {
323322
}
324323
```
325324

326-
- The `packageRef` keys signal local or remote mode: `path` xor `url`+`version`.
325+
- `packageRef` has the local `path` shape by default. It has the `url`+`version`
326+
shape when remote settings are present: `RN_SPM_REMOTE_URL`, or a `url`
327+
persisted in `spm-remote.json`. Remote mode is planned, not supported, but
328+
plugins must handle both shapes.
329+
- `context.react` can be `null` (see the table above). Check it before use.
327330
- `packageRef.path` is **absolute**, so it is correct in any subdirectory of
328331
`outputDir` the plugin writes its manifest into. The generated manifests are
329332
gitignored and regenerated every sync, so this has no portability cost.

‎packages/react-native/scripts/spm/__docs__/spm-header-paths-contract.md‎

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,15 @@ walk-up, no JSON, no `import Foundation`.
5959
| ------------------------ | ---------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
6060
| Autolinked aggregator | `build/generated/autolinking/Package.swift` | Neither: it has no inline targets, and references only its dependencies' packages (`packages/<Name>`, `libs/<Name>`, plugin packages) |
6161
| Local-module wrapper | `build/generated/autolinking/packages/<Name>/` | `.package(path: "../../../../xcframeworks")` + `"../../../ios"` (only for `swiftpmConfig.modules` entries) |
62-
| Codegen template | `build/generated/ios/Package.swift` | `.package(path: "../../xcframeworks")` (or the remote url) |
63-
| App target (pbxproj) | `<App>.xcodeproj` | local `XCLocalSwiftPackageReference` (or `XCRemoteSwiftPackageReference` in remote mode) |
64-
| Scaffolded community lib | `node_modules/<dep>/Package.swift` | scaffold-time relative paths to the app's xcframeworks + codegen packages (or `.package(url:exact:)` in remote mode) |
62+
| Codegen template | `build/generated/ios/Package.swift` | `.package(path: "../../xcframeworks")` |
63+
| App target (pbxproj) | `<App>.xcodeproj` | local `XCLocalSwiftPackageReference` |
64+
| Scaffolded community lib | `node_modules/<dep>/Package.swift` | scaffold-time relative paths to the app's xcframeworks + codegen packages |
6565

66-
## Remote-package mode
66+
## Remote-package mode (planned)
67+
68+
> **Planned, not supported.** The `RN_SPM_REMOTE_URL` code path exists, but it
69+
> is unsupported. Do not use it. Libraries and apps do not need a remote form.
70+
> The text below records the design intent.
6771
6872
A **URL alone** turns remote mode on: `RN_SPM_REMOTE_URL`, or the persisted
6973
`url`. The whole app graph then uses a single remote React Native package
@@ -106,26 +110,62 @@ tooling never silently pins an unpublished tag.
106110
## Hand-authored community library contract
107111

108112
The tooling leaves untouched a library that ships its own `Package.swift` (no
109-
scaffolder or autolinker marker). That library needs only two things, and **no
110-
discovery code**:
111-
112-
1. Depend on the React Native SPM package and its products — in remote mode
113-
`.package(url: "<repo>", exact: "<version>")` +
114-
`.product(name: "ReactHeaders", package: "<identity>")`,
115-
`.product(name: "ReactNativeHeaders", package: "<identity>")`, and
116-
`.product(name: "ReactNativeDependenciesHeaders", package: "<identity>")`,
117-
where `<identity>` is the URL tail, lowercased, without `.git`. A scaffolded
118-
manifest depends on the same React Native products. Libraries should declare
119-
a version RANGE in production; the consuming app pins EXACT.
120-
2. Ship its own generated code: set `codegenConfig.includesGeneratedCode: true`
121-
and generate with
113+
scaffolder or autolinker marker). That library needs two things, and **no
114+
discovery code**. A third thing is optional:
115+
116+
1. Depend on the React Native package and the app's codegen package by relative
117+
path. SwiftPM reaches the library's manifest through React Native's
118+
per-library link folder, `build/generated/autolinking/libs/<SwiftName>`,
119+
named after the library's Swift name, not its npm name. The paths are
120+
relative to that folder:
121+
`.package(name: "ReactNative", path: "../../../../xcframeworks")` and
122+
`.package(name: "React-GeneratedCode", path: "../../../ios")`. The target
123+
then depends on `.product(name: "ReactHeaders", package: "ReactNative")`,
124+
`.product(name: "ReactNativeHeaders", package: "ReactNative")`,
125+
`.product(name: "ReactNativeDependenciesHeaders", package: "ReactNative")`,
126+
and `.product(name: "ReactAppHeaders", package: "React-GeneratedCode")`. A
127+
scaffolded manifest depends on the same products. `React-GeneratedCode`
128+
declares `.iOS(.v15)`, so the library must declare `.iOS(.v15)` or higher.
129+
2. Set the React Native defines on each target. SwiftPM mode supports only the
130+
New Architecture, and nothing sets `RCT_NEW_ARCH_ENABLED` for a hand-authored
131+
manifest. Without it, the preprocessor drops all code behind
132+
`#ifdef RCT_NEW_ARCH_ENABLED`. Keep those `#ifdef` gates in the sources: the
133+
same sources still build under CocoaPods. C++ and Objective-C++ sources also
134+
need the config-gated `DEBUG` and `NDEBUG` defines that React Native's own
135+
targets get. Without `NDEBUG` in Release, the `DebugStringConvertible` vtable
136+
and the `ShadowNode` layout differ from the prebuilt React framework, and the
137+
link fails. A library with Swift sources sets the define in `swiftSettings`
138+
too, as CocoaPods does through `OTHER_SWIFT_FLAGS`:
139+
140+
```swift
141+
cSettings: [.define("RCT_NEW_ARCH_ENABLED", to: "1")],
142+
cxxSettings: [
143+
.define("RCT_NEW_ARCH_ENABLED", to: "1"),
144+
.define("DEBUG", .when(configuration: .debug)),
145+
.define("NDEBUG", .when(configuration: .release)),
146+
],
147+
swiftSettings: [.define("RCT_NEW_ARCH_ENABLED")],
148+
```
149+
150+
3. Optionally, ship its own generated code. Set
151+
`codegenConfig.includesGeneratedCode: true` and generate with
122152
`generate-codegen-artifacts.js --path . --targetPlatform ios --source library`.
123153
The output lands at `<outputDir>/build/generated/ios/ReactCodegen/`. The
124154
manifest reaches it with one safe `.headerSearchPath(...)` into the library's
125155
own tree. The app-side codegen then skips the library's spec, so there are no
126156
duplicate symbols.
127157

128-
The library is then self-contained. It carries no app-layout knowledge and needs
129-
no per-app codegen headers from the consuming app.
130-
`@chrfalch/react-native-calculator` (a hand-authored Fabric/TurboModule library)
131-
proves this.
158+
This is how a hand-authored library links today. The folders these relative
159+
paths reach, `build/xcframeworks` and `build/generated/ios` under the app's
160+
`ios/` folder, are generated output: `spm` recreates them on each machine, and
161+
they are gitignored. The paths resolve only when SwiftPM reaches the library
162+
through its `libs/<SwiftName>` link folder, so the library cannot build on its
163+
own (for example, `swift build` in its own repository). The relative paths
164+
`../../../ios` and `../../../../xcframeworks` assume the default layout. Two
165+
options of `generate-spm-autolinking.js` change it: `--output` moves the link
166+
folder, and `--xcframeworks-path` moves the React Native package. With either
167+
option set, the hardcoded paths do not resolve.
168+
169+
The normal pattern is rules 1 and 2 alone: the library uses the app's generated
170+
code from `React-GeneratedCode`. A library that takes rule 3 carries its own
171+
generated code instead.

‎packages/react-native/scripts/spm/__docs__/spm-scripts.md‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ value, so **members you added to it by hand are lost**.
374374
| `RN_DEPS_HEADERS_TARBALL_PATH` | Local `ReactNativeDependenciesHeaders` tarball, used instead of the copy inside the dependencies tarball. |
375375
| `RCT_REACT_NATIVE_MAVEN_MIRROR_ENABLED` | `false` or `0` turns off the React Native Maven mirror. The mirror is on by default and is tried before Maven Central. |
376376
| `ENTERPRISE_REPOSITORY` | Maven repository URL. When set, it is the only repository consulted for releases; the snapshot fallback still uses Maven Central snapshots. |
377-
| `RN_SPM_REMOTE_URL`, `RN_SPM_REMOTE_VERSION` | Turn on remote-package mode — see [Remote-package mode](./spm-header-paths-contract.md#remote-package-mode). |
377+
| `RN_SPM_REMOTE_URL`, `RN_SPM_REMOTE_VERSION` | Reserved for the planned remote-package mode. Not supported. See [Remote-package mode (planned)](./spm-header-paths-contract.md#remote-package-mode-planned). |
378378
| `RN_DEP_VERSION` | Version of the `ReactNativeDependencies` artifact, or `nightly`. Defaults to the React Native version. |
379379
| `HERMES_VERSION` | Version of the Hermes artifact, `nightly`, or `latest-v1`. Defaults to the locally pinned `hermes-compiler` version, else `latest-v1`. |
380380

@@ -880,8 +880,10 @@ script branches on the exit code:
880880
[Community packages without a Package.swift](#community-packages-without-a-packageswift));
881881
- the autolinking config command fails (see
882882
[The autolinking config command is remembered](#the-autolinking-config-command-is-remembered));
883-
- remote mode is on but no usable React Native version resolves (see
884-
[Remote-package mode](./spm-header-paths-contract.md#remote-package-mode));
883+
- `RN_SPM_REMOTE_URL`, or a persisted `url`, is set but no usable React Native
884+
version resolves. This happens only in the unsupported, planned remote mode
885+
(see
886+
[Remote-package mode (planned)](./spm-header-paths-contract.md#remote-package-mode-planned));
885887
- The precompiled frameworks that autolinking plugins provide differ from the
886888
frameworks that the Xcode project links (see
887889
[Troubleshooting](#troubleshooting)). Sync writes the stamp and records the
@@ -898,5 +900,5 @@ script branches on the exit code:
898900

899901
These errors have their own exit code because a transient sync hiccup should not
900902
break a build that could still succeed, while a missing manifest, an out-of-date
901-
project, a broken config command, or an unresolvable remote version should not
902-
pass silently.
903+
project, a broken config command, or an unresolvable remote version (planned
904+
remote mode) should not pass silently.

0 commit comments

Comments
 (0)