Skip to content

Commit e9eac58

Browse files
chrfalchmeta-codesync[bot]
authored andcommitted
SPM: fix raw Flow type annotations in bare-node scripts (#57660)
Summary: The scripts under `packages/react-native/scripts/spm/` are executed as plain `node` (via `setup-apple-spm.js` during the SwiftPM Xcode build), with **no Babel** to strip Flow. They use Flow-in-comment syntax (`/*: T */`) throughout so they parse un-transpiled. A handful of uninitialized `let X: T;` declarations had slipped in with **raw** Flow annotations. Node parses the whole file on `require`, so each one throws `SyntaxError: Unexpected token ':'` at load time — taking the entire module down before it can run. The jest suites didn't catch it because jest runs these files through `react-native/babel-preset`, which strips raw and comment-form annotations alike. Only the bare-`node` shipped path (SwiftPM setup) hits the error. ## Fix Convert each offending declaration to Flow-comment form. Prettier's `flow` parser only keeps a comment type attached to the binding when the declaration is **initialized**, so each site gets a type-appropriate (inert) initializer — every variable is reassigned in the immediately-following `try`/branch before any use: | file | lines | form | |---|---|---| | `autolinking-plugins.js` | 120, 178 | `/*: unknown */ = undefined` | | `download-spm-artifacts.js` | 946, 973 | `/*: string */ = ''` | | `download-spm-artifacts.js` | 1352 | `/*: {...} */ = {}` | | `generate-spm-autolinking.js` | 429 | `/*: Array<...> */ = []` | | `generate-spm-xcodeproj.js` | 1802 | `/*: string */ = ''` | | `generate-spm-xcodeproj.js` | 1808 | `/*: unknown */ = undefined` | | `generate-spm-xcodeproj.js` | 1863 | `/*: Array<...> */ = []` | | `scaffold-package-swift.js` | 200, 1129 | `/*: Array<...> */ = []` | | `scaffold-package-swift.js` | 933 | annotation dropped — Flow infers `PodspecModel` from `readPodspec()` | ## Changelog: [INTERNAL] [FIXED] - Fix raw Flow type annotations that broke bare-node execution of the SwiftPM setup scripts Pull Request resolved: #57660 Test Plan: - `node --check` passes on all 14 `scripts/spm/*.js` (previously `autolinking-plugins.js` and `download-spm-artifacts.js` threw `SyntaxError` at parse time). - `flow focus-check` reports **0 errors** in the touched files. - Prettier: clean. - SPM jest suites: green. ## Changelog: [INTERNAL] [FIXED] - Fix raw Flow type annotations that broke bare-node execution of the SwiftPM setup scripts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: cortinico Differential Revision: D113554595 Pulled By: cipolleschi fbshipit-source-id: b7fa29dd54266b749e1671333fb0530f6b6a8aab
1 parent 43b44ed commit e9eac58

5 files changed

Lines changed: 12 additions & 12 deletions

File tree

packages/react-native/scripts/spm/autolinking-plugins.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ function discoverPlugins(
117117
);
118118
}
119119
const pluginPath = path.resolve(dep.root, rel);
120-
let fn: unknown;
120+
let fn /*: unknown */ = null;
121121
try {
122122
// $FlowFixMe[unsupported-syntax] dynamic require by computed path
123123
fn = require(pluginPath);
@@ -175,7 +175,7 @@ function invokePlugins(
175175
const seenFrameworkNames /*: Set<string> */ = new Set();
176176

177177
for (const {depName, pluginPath, plugin} of plugins) {
178-
let result: unknown;
178+
let result /*: unknown */ = null;
179179
try {
180180
result = plugin(context);
181181
} catch (e) {

packages/react-native/scripts/spm/download-spm-artifacts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ async function processArtifact(
943943
return localPath;
944944
};
945945

946-
let tarPath: string;
946+
let tarPath /*: string */ = '';
947947
let fromShared = false;
948948
if (isLocalTarball) {
949949
tarPath = url;
@@ -970,7 +970,7 @@ async function processArtifact(
970970
onProgress(xcframeworkName, 0, 0, 0, false, 0);
971971
}
972972
const tmpExtractDir = path.join(outputDir, '.extract-tmp', label);
973-
let xcfwPath: string;
973+
let xcfwPath /*: string */ = '';
974974
try {
975975
xcfwPath = extractXCFramework(tarPath, tmpExtractDir);
976976
} catch (e) {
@@ -1349,7 +1349,7 @@ function validateArtifactsCache(
13491349
if (!fs.existsSync(artifactsJsonPath)) {
13501350
return `artifacts.json missing in ${artifactsDir}`;
13511351
}
1352-
let json: {[string]: {xcframeworkPath: string, url: string}};
1352+
let json /*: {[string]: {xcframeworkPath: string, url: string}} */ = {};
13531353
try {
13541354
// $FlowFixMe[unclear-type] JSON.parse returns any
13551355
const parsed /*: any */ = JSON.parse(

packages/react-native/scripts/spm/generate-spm-autolinking.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function hasMixedLanguageSources(absSource /*: string */) /*: boolean */ {
426426
let hasClang = false;
427427
const walk = (dir /*: string */, depth /*: number */) => {
428428
if (depth > 6 || (hasSwift && hasClang)) return;
429-
let entries: Array<{name: string, isDirectory(): boolean}>;
429+
let entries /*: Array<{name: string, isDirectory(): boolean}> */ = [];
430430
try {
431431
// $FlowFixMe[incompatible-type] Dirent typing
432432
entries = fs.readdirSync(dir, {withFileTypes: true});

packages/react-native/scripts/spm/generate-spm-xcodeproj.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,13 +1799,13 @@ function readGeneratedSourcesManifest(
17991799
appRoot /*: string */,
18001800
) /*: Array<GeneratedSource> */ {
18011801
const manifestPath = path.join(appRoot, SPM_GENERATED_SOURCES_MANIFEST);
1802-
let raw: string;
1802+
let raw /*: string */ = '';
18031803
try {
18041804
raw = fs.readFileSync(manifestPath, 'utf8');
18051805
} catch {
18061806
return [];
18071807
}
1808-
let entries: unknown;
1808+
let entries /*: unknown */ = null;
18091809
try {
18101810
entries = JSON.parse(raw);
18111811
} catch {
@@ -1860,7 +1860,7 @@ function readMarker(
18601860
// build-time sync (sync-spm-autolinking.js, via readArtifactsVersionOverride
18611861
// below) to call without pulling in any pbxproj-editing machinery at runtime.
18621862
function findInjectedXcodeproj(appRoot /*: string */) /*: string | null */ {
1863-
let entries: Array<{name: string, isDirectory(): boolean}>;
1863+
let entries /*: Array<{name: string, isDirectory(): boolean}> */ = [];
18641864
try {
18651865
// $FlowFixMe[incompatible-type] Dirent typing
18661866
entries = fs.readdirSync(appRoot, {withFileTypes: true});

packages/react-native/scripts/spm/scaffold-package-swift.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ function collectSubdirs(
197197
]);
198198
const out /*: Array<string> */ = [];
199199
const walk = (absDir /*: string */, relDir /*: string */) => {
200-
let entries: Array<{name: string, isDirectory(): boolean}>;
200+
let entries /*: Array<{name: string, isDirectory(): boolean}> */ = [];
201201
try {
202202
// $FlowFixMe[incompatible-type] Dirent typing
203203
entries = fs.readdirSync(absDir, {withFileTypes: true});
@@ -930,7 +930,7 @@ function scaffoldPackageSwiftForDep(
930930
};
931931
}
932932

933-
let model: PodspecModel;
933+
let model;
934934
try {
935935
model = readPodspec(podspecPath);
936936
} catch (e) {
@@ -1126,7 +1126,7 @@ function scaffoldAll(
11261126
directDeps.push({name, root, platforms: {ios: iosPlatform}});
11271127
}
11281128

1129-
let allDeps: Array<AutolinkedDep>;
1129+
let allDeps /*: Array<AutolinkedDep> */ = [];
11301130
try {
11311131
allDeps = expandSpmDependencies(directDeps, {
11321132
readConfig: defaultReadConfig,

0 commit comments

Comments
 (0)