Encode line breaks in SwiftPM scheme pre-action scriptText (#58826)

Summary:
The SwiftPM setup adds a "Sync SPM Autolinking" pre-action to the app's Xcode scheme. The pre-action never ran its sync step.

The script is many lines long. `escapeXmlAttribute` wrote it into the `scriptText` XML attribute with raw line breaks. An XML parser replaces each raw line break in an attribute value with a space (XML 1.0 §3.3.3). So Xcode read the script as one line. The second line of the script is a `#` comment, so everything after `set -euo pipefail` became part of that comment.

This PR makes two changes in `packages/react-native/scripts/spm/generate-spm-xcodeproj.js`:

- `escapeXmlAttribute` now also encodes `\r`, `\n` and `\t` as `&https://github.com/react/react-native/issues/13;`, `&https://github.com/react/react-native/issues/10;` and `&https://github.com/react/react-native/issues/9;`. Xcode itself writes the script this way.
- `deinit` deletes a scheme that RN created only when the scheme is still unchanged. This check now ignores the value of the script. Without this change, a scheme that an older version wrote with raw line breaks would no longer match, and `deinit` would leave it on disk. The XML decoder had no callers left after this change, so this PR removes it.

Existing projects get the fixed script when the user runs `npx react-native spm update` once. The build-time `sync` does not rewrite the scheme.

## Changelog:

[IOS] [FIXED] - SwiftPM: keep line breaks in the scheme pre-action script so that the "Sync SPM Autolinking" pre-action runs. Run `npx react-native spm update` once to rewrite an existing scheme.

Pull Request resolved: https://github.com/react/react-native/pull/58826

Test Plan:
New tests:

- `generate-spm-xcodeproj-test.js`: parses the generated scheme with `xmldom/xmldom` and checks that the script keeps its line breaks and tabs. A second test checks that a refresh of an older scheme with raw line breaks writes `&https://github.com/react/react-native/issues/10;`.
- `remove-spm-injection-test.js`: checks that `deinit` still deletes an RN-created scheme whose script has raw line breaks.

```
yarn jest --no-cache -i packages/react-native/scripts/spm/__tests__/generate-spm-xcodeproj-test.js packages/react-native/scripts/spm/__tests__/remove-spm-injection-test.js
```

- Before the fix: 2 failed, 124 passed (the two `generate-spm-xcodeproj` tests).
- With only the encoding change: the new `deinit` test and 4 existing `deinit` tests failed.
- With both changes: 2 suites passed, 126 tests passed.

ESLint and Prettier pass on the changed files. `flow check-contents` reports no errors for `generate-spm-xcodeproj.js`.

I did not build an app in Xcode for this PR. An Expo app had the problem: when the line breaks in its scheme were changed to `&https://github.com/react/react-native/issues/10;` by hand, the pre-action ran.

Reviewed By: christophpurrer

Differential Revision: D123382914

Pulled By: cipolleschi

fbshipit-source-id: 0c51cb7c6fe7a66567602d813a13381f6bf3704f
(cherry picked from commit b8df948fd4)
(cherry picked from commit 1d69b9084d)
This commit is contained in:
Christian Falch authored and t committed 2026-10-26 12:32:46 +00:00
1 parent 99d4ed3985
commit 90ba1173d9
3 files changed
+65 -24

No files matched your search

@@ -20,6 +20,7 @@ const {
generateXcscheme,
readScriptPhasesManifest,
} = require('../generate-spm-xcodeproj');
const {DOMParser} = require('@xmldom/xmldom');
const {execFileSync} = require('node:child_process');
const fs = require('node:fs');
const os = require('node:os');
@@ -137,6 +138,40 @@ describe('scheme pre-action', () => {
expect(result).toContain('<');
});
function parsedSyncScript(xml) {
return new DOMParser()
.parseFromString(xml, 'text/xml')
.getElementsByTagName('ActionContent')[0]
.getAttribute('scriptText');
}
const MULTILINE_SCRIPT = 'set -e\n\tif [ "$A" ]; then\r\n a && b < c\nfi';
it('keeps line breaks and tabs when an XML parser reads the script', () => {
const result = generateXcscheme(
'MyApp',
'TARGET_UUID',
'MyApp',
MULTILINE_SCRIPT,
);
expect(parsedSyncScript(result)).toBe(MULTILINE_SCRIPT);
});
it('encodes line breaks when it refreshes a script written with raw ones', () => {
const legacy = generateXcscheme(
'MyApp',
'TARGET_UUID',
'MyApp',
'OLD',
).replace('scriptText = "OLD"', 'scriptText = "old\n# line 2"');
const updated = addPreActionToScheme(
legacy,
'TARGET_UUID',
MULTILINE_SCRIPT,
);
expect(parsedSyncScript(updated)).toBe(MULTILINE_SCRIPT);
});
it('refreshes stale script text and is idempotent', () => {
const first = generateXcscheme(
'MyApp',
@@ -499,6 +499,21 @@ describe('deinit — scheme ownership', () => {
expect(deinit().status).toBe('removed');
expect(fs.existsSync(schemePath)).toBe(false);
});
it('deletes its own scheme written with raw line breaks in the script', () => {
const {sync, deinit, schemePath, readScheme} = setUp();
sync();
fs.writeFileSync(
schemePath,
readScheme().replace(/&#(9|10|13);/g, (_, c) =>
String.fromCharCode(Number(c)),
),
'utf8',
);
expect(deinit().status).toBe('removed');
expect(fs.existsSync(schemePath)).toBe(false);
});
});
// ---------------------------------------------------------------------------
+15 -24
View File
@@ -862,28 +862,21 @@ function buildSyncAutolinkingScript(
${syncStaleCheckAndDispatch()}
`;
}
// XML-attribute escape (the five named entities). The sync script uses `>`
// and `&` for redirection and bg/and chains, plus `<` for heredocs and
// comparisons — all of which break Xcode's scheme parser if left raw.
// XML-attribute escape. The sync script uses `>` and `&` for redirection and
// bg/and chains, plus `<` for heredocs and comparisons — all of which break
// Xcode's scheme parser if left raw. Raw line breaks and tabs are legal but an
// XML parser turns them into spaces (XML 1.0 §3.3.3), collapsing the script
// onto one line, so they are written as character references, as Xcode does.
function escapeXmlAttribute(s /*: string */) /*: string */ {
return s
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
// The inverse of escapeXmlAttribute. `&amp;` is expanded LAST so an entity that
// was itself escaped (`&lt;` → `&amp;lt;`) round-trips back to its own text
// rather than to `<`.
function unescapeXmlAttribute(s /*: string */) /*: string */ {
return s
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&apos;/g, "'")
.replace(/&amp;/g, '&');
.replace(/'/g, '&apos;')
.replace(/\r/g, '&#13;')
.replace(/\n/g, '&#10;')
.replace(/\t/g, '&#9;');
}
function generateXcscheme(
@@ -2525,8 +2518,9 @@ function injectSpmIntoExistingXcodeproj(
return {status: 'injected', target: plan.target.name};
}
/** The sync pre-action's script, unescaped, or null when the scheme has none. */
function schemePreActionScript(xml /*: string */) /*: ?string */ {
// The scheme with the sync pre-action's scriptText emptied, so the compare
// ignores how the script was encoded; null when there is no pre-action.
function withoutPreActionScript(xml /*: string */) /*: ?string */ {
const titleIdx = xml.indexOf('title = "Sync SPM Autolinking"');
if (titleIdx < 0) {
return null;
@@ -2540,9 +2534,7 @@ function schemePreActionScript(xml /*: string */) /*: ?string */ {
// escapeXmlAttribute maps a literal `"` to `&quot;`, so the next `"` is always
// the closing delimiter.
const valueEnd = xml.indexOf('"', valueStart);
return valueEnd < 0
? null
: unescapeXmlAttribute(xml.slice(valueStart, valueEnd));
return valueEnd < 0 ? null : xml.slice(0, valueStart) + xml.slice(valueEnd);
}
/**
@@ -2564,10 +2556,9 @@ function isGeneratedScheme(
targetUuid /*: string */,
projName /*: string */,
) /*: boolean */ {
const script = schemePreActionScript(xml);
return (
script != null &&
xml === generateXcscheme(appName, targetUuid, projName, script)
withoutPreActionScript(xml) ===
generateXcscheme(appName, targetUuid, projName, '')
);
}