Skip to content

Commit f41fcaa

Browse files
committed
fix(ios): link embedded SwiftPM products into extensions without embedding
A package listed with `targets` is applied to each of those targets with the same config, so `embed` also copied the framework into widget and other app extension bundles. App Store validation rejects frameworks embedded in an extension (ITMS-90206); extensions load them from the host app's Frameworks folder through their `@executable_path/../../Frameworks` runpath. Extension targets now only link products listed in `embed`.
1 parent d4acd83 commit f41fcaa

4 files changed

Lines changed: 74 additions & 5 deletions

File tree

‎lib/definitions/ios.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ declare global {
5454
* Products built as dynamic libraries (`.library(type: .dynamic)`) must
5555
* also be copied into the app bundle. Lists the subset of `libs` to
5656
* embed; a dynamic product that is linked but not embedded fails in dyld
57-
* at launch on devices.
57+
* at launch on devices. Extension `targets` only link these products and
58+
* load them from the app's bundle.
5859
*/
5960
embed?: string[];
6061
}

‎lib/definitions/project.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ interface IOSSPMPackageBase {
115115
* Optional: products built as dynamic libraries (`.library(type: .dynamic)`)
116116
* must also be copied into the app bundle. List the subset of `libs` to
117117
* embed; a dynamic product that is linked but not embedded builds fine but
118-
* fails in dyld at launch on a device.
118+
* fails in dyld at launch on a device. Extensions listed in `targets` only
119+
* link these products and load them from the app's bundle.
119120
*
120121
* A `.library(...)` without a type is static and is compiled into the app
121122
* executable, so it needs no embedding.

‎lib/services/ios/spm-pbxproj-service.ts‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import * as semver from "semver";
33
import { injector } from "../../common/yok";
44
import { IFileSystem } from "../../common/declarations";
55

6+
/**
7+
* Extensions (widgets, watch extensions, ...) load embedded frameworks from the
8+
* host app's Frameworks folder through their `@executable_path/../../Frameworks`
9+
* runpath; App Store validation rejects a framework copied into an extension's
10+
* own bundle (ITMS-90206).
11+
*/
12+
function isExtensionTarget(target: any): boolean {
13+
return /extension/.test(String(target?.productType ?? ""));
14+
}
15+
616
/**
717
* Writes Swift Package references directly into an Xcode project's pbxproj.
818
*
@@ -18,9 +28,10 @@ import { IFileSystem } from "../../common/declarations";
1828
* `packageProductDependencies`.
1929
*
2030
* A product listed in `embed` additionally gets a second `PBXBuildFile` in the
21-
* target's "Embed Frameworks" copy phase, which is how Xcode itself records a
22-
* dynamic package product added through "Frameworks, Libraries, and Embedded
23-
* Content" — linking alone leaves the framework out of the app bundle.
31+
* "Embed Frameworks" copy phase of each non-extension target, which is how
32+
* Xcode itself records a dynamic package product added through "Frameworks,
33+
* Libraries, and Embedded Content" — linking alone leaves the framework out of
34+
* the app bundle.
2435
*
2536
* Every entry is keyed by its pbxproj comment (e.g. `XCRemoteSwiftPackageReference
2637
* "Auth0"`), and an existing entry is updated in place rather than duplicated —
@@ -215,6 +226,12 @@ export class SPMPbxprojService implements ISPMPbxprojService {
215226
);
216227
}
217228
}
229+
if (embedLibs.size && isExtensionTarget(target)) {
230+
this.$logger.trace(
231+
`SPM: linking ${[...embedLibs].join(", ")} into extension target ${target.name} without embedding; it loads them from the host app's bundle.`,
232+
);
233+
embedLibs.clear();
234+
}
218235
let embedBuildPhaseFiles: any[] | null = null;
219236
if (embedLibs.size) {
220237
const embedBuildPhaseObj = this.findOrCreateEmbedFrameworksBuildPhase(

‎test/services/ios/spm-pbxproj-service.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,23 @@ function parsePbxproj(projectRoot: string): any {
111111
return project;
112112
}
113113

114+
/** Adds an app extension target with its own Frameworks phase to the fixture, returning its uuid. */
115+
function addExtensionTarget(projectRoot: string, name: string): string {
116+
const project = parsePbxproj(projectRoot);
117+
const target = project.addTarget(name, "app_extension", name);
118+
project.addBuildPhase(
119+
[],
120+
"PBXFrameworksBuildPhase",
121+
"Frameworks",
122+
target.uuid,
123+
);
124+
writeFileSync(
125+
path.join(projectRoot, `${TARGET_NAME}.xcodeproj`, "project.pbxproj"),
126+
project.writeSync(),
127+
);
128+
return target.uuid;
129+
}
130+
114131
/** The uuids of the build files in the fixture target's "Embed Frameworks" phase. */
115132
function embedPhaseFileUuids(project: any): string[] {
116133
const targets = project.pbxNativeTargetSection();
@@ -461,6 +478,39 @@ describe("SPMPbxprojService", () => {
461478
assert.notInclude(readPbxproj(projectRoot), "Embed Frameworks");
462479
});
463480

481+
it("links an embedded product into an extension target without embedding it there", () => {
482+
const extensionId = addExtensionTarget(projectRoot, "Widget");
483+
484+
assert.isTrue(
485+
service.addPackages(projectRoot, [
486+
{ targetName: TARGET_NAME, package: dynamicPackage },
487+
{ targetName: "Widget", package: dynamicPackage },
488+
]),
489+
);
490+
491+
const project = parsePbxproj(projectRoot);
492+
assert.lengthOf(embedPhaseFileUuids(project), 1);
493+
494+
const extension = project.pbxNativeTargetSection()[extensionId];
495+
const phaseIds = (extension.buildPhases as any[]).map(
496+
(phase) => phase.value,
497+
);
498+
const objects = project.hash.project.objects;
499+
assert.isFalse(
500+
phaseIds.some((id) => objects["PBXCopyFilesBuildPhase"]?.[id]),
501+
"an extension target must not get a copy phase for package products",
502+
);
503+
const linked = phaseIds
504+
.map((id) => objects["PBXFrameworksBuildPhase"]?.[id])
505+
.filter(Boolean)
506+
.flatMap((phase: any) => phase.files ?? [])
507+
.map((file: any) => file.comment);
508+
assert.includeMembers(linked, [
509+
"DynamicProduct in Frameworks",
510+
"StaticProduct in Frameworks",
511+
]);
512+
});
513+
464514
it("warns about an embed entry that is not a linked lib, and skips it", () => {
465515
assert.isTrue(
466516
service.addPackages(projectRoot, [

0 commit comments

Comments
 (0)