Repository navigation
fix(cli): fix name collisions in swift-package-manager #8739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f495cc5
ddb7ac0
6afdc88
cf2e857
e9b0104
f718fb3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,9 +7,17 @@ import { fatal } from '../errors'; | |
| import { convertToUnixPath } from './fs'; | ||
| import { resolveNode } from './node'; | ||
|
|
||
| const SOURCE_PACKAGE_URL = 'https://github.com/ionic-team/capacitor'; | ||
| export const SOURCE_PACKAGE_IDENTITY = 'capacitor'; | ||
| const SOURCE_PACKAGE_NAME = new RegExp(`name:\\s*"${SOURCE_PACKAGE_IDENTITY}"`); | ||
| export const SOURCE_PACKAGE_IDENTITY = 'capacitor-ios'; | ||
|
|
||
| /** Matches the source package's git URL, with or without `.git`, but not repos sharing its prefix. */ | ||
| const SOURCE_PACKAGE_URL = /url:\s*"https:\/\/github\.com\/ionic-team\/capacitor(?:\.git)?\/?"/i; | ||
|
|
||
| /** | ||
| * Names a path dependency on Capacitor may carry: the current identity, and the one syncs before | ||
| * the `capacitor-ios` rename pinned. A plugin rewritten by such a sync keeps the old name until | ||
| * it is synced again. | ||
| */ | ||
| const PINNED_PACKAGE_NAMES = [SOURCE_PACKAGE_IDENTITY, 'capacitor']; | ||
|
|
||
| /** Product providing the Cordova compatibility layer. */ | ||
| export const CORDOVA_PRODUCT = 'CapacitorCordova'; | ||
|
|
@@ -20,6 +28,12 @@ const PLATFORM_PACKAGE = '@capacitor/ios'; | |
| /** Plugins published before Capacitor 9 depend on the platform as a prebuilt xcframework. */ | ||
| const BINARY_PACKAGE_IDENTITY = 'capacitor-swift-pm'; | ||
|
|
||
| /** | ||
| * Cordova plugins that ship their own `Package.swift` depend on `apache/cordova-ios` for the | ||
| * `Cordova` product, which the Capacitor source package also vends. | ||
| */ | ||
| const CORDOVA_PACKAGE_URL = /url:\s*"https:\/\/github\.com\/apache\/cordova-ios(?:\.git)?\/?"/i; | ||
|
|
||
| /** | ||
| * Resolves the Capacitor Swift package generated `Package.swift` files should depend on: the | ||
| * `@capacitor/ios` npm already installed in the app, which ships the package alongside the sources. | ||
|
|
@@ -97,9 +111,73 @@ export function renderCapacitorPackage(absolutePath: string, fromDir: string): s | |
| * copy of the framework. Rewriting keeps the whole graph on one identity. | ||
| */ | ||
| export function rewriteCapacitorDependency(content: string, absolutePath: string, fromDir: string): string { | ||
| const rewritten = replacePackageCalls(content, renderCapacitorPackage(absolutePath, fromDir)); | ||
| const capacitorCalls = findPackageCalls(content).filter((call) => referencesCapacitor(call.text)); | ||
| if (capacitorCalls.length === 0) { | ||
| return content; | ||
| } | ||
|
|
||
| // Collected before the calls are replaced: once they are, every one reads `capacitor-ios` and | ||
| // the name each target's `.product(package:)` was written against is gone. The pinned names are | ||
| // included because a sync that renamed only the `.package(...)` entry left products on the old | ||
| // name behind. | ||
| const previousNames = new Set( | ||
| [...capacitorCalls.flatMap((call) => packageReferenceNames(call.text)), ...PINNED_PACKAGE_NAMES].filter( | ||
| (name) => name !== SOURCE_PACKAGE_IDENTITY, | ||
|
markemer marked this conversation as resolved.
Resolved
|
||
| ), | ||
| ); | ||
|
|
||
| let rewritten = replacePackageCalls(content, renderCapacitorPackage(absolutePath, fromDir)); | ||
|
|
||
| // A name some other dependency still answers to belongs to that dependency, not Capacitor. SPM | ||
| // resolves `.product(package:)` against a dependency's pinned name and its derived identity alike, | ||
| // so both are reserved. | ||
| const remainingNames = new Set( | ||
| findPackageCalls(rewritten) | ||
| .filter((call) => !referencesCapacitor(call.text)) | ||
| .flatMap((call) => packageReferenceNames(call.text)), | ||
| ); | ||
|
|
||
| for (const name of previousNames) { | ||
| if (remainingNames.has(name)) { | ||
| continue; | ||
|
markemer marked this conversation as resolved.
Resolved
|
||
| } | ||
| rewritten = rewritten.replace( | ||
| new RegExp(`(package:\\s*)"${escapeRegExp(name)}"`, 'gi'), | ||
| `$1"${SOURCE_PACKAGE_IDENTITY}"`, | ||
| ); | ||
| } | ||
|
Comment on lines
+140
to
+148
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could edit text inside comments and strings, not sure if that's intended or not.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good question. I think it's unlikely that these will be in comments? So we're probably ok? |
||
|
|
||
| return rewritten.split(`package: "${BINARY_PACKAGE_IDENTITY}"`).join(`package: "${SOURCE_PACKAGE_IDENTITY}"`); | ||
| return rewritten; | ||
| } | ||
|
|
||
| /** | ||
| * The names a `.product(package:)` may use to refer to the dependency a `.package(...)` call | ||
| * declares, lowercased: its explicit `name:` when it pins one, and the identity SPM derives from | ||
| * the last component of its URL or path. SPM accepts either, so a pinned name does not retire the | ||
| * derived identity. | ||
| */ | ||
| function packageReferenceNames(call: string): string[] { | ||
| const names = new Set<string>(); | ||
|
|
||
| const explicit = call.match(/name:\s*"([^"]+)"/); | ||
| if (explicit) { | ||
| names.add(explicit[1].toLowerCase()); | ||
| } | ||
|
|
||
| const location = call.match(/(?:url|path):\s*"([^"]+)"/); | ||
| if (location) { | ||
| const lastComponent = location[1].replace(/\/+$/, '').split('/').pop() ?? ''; | ||
| const identity = lastComponent.replace(/\.git$/i, '').toLowerCase(); | ||
| if (identity) { | ||
| names.add(identity); | ||
| } | ||
| } | ||
|
|
||
| return [...names]; | ||
| } | ||
|
|
||
| function escapeRegExp(value: string): string { | ||
| return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | ||
| } | ||
|
|
||
| /** Reads the version an existing `Package.swift` pins Capacitor to, if it pins one. */ | ||
|
|
@@ -163,7 +241,12 @@ export function patchPackageSwiftDeploymentTarget( | |
| * the app, or the package it points at, has moved. | ||
| */ | ||
| function referencesCapacitor(call: string): boolean { | ||
| return call.includes(BINARY_PACKAGE_IDENTITY) || call.includes(SOURCE_PACKAGE_URL) || SOURCE_PACKAGE_NAME.test(call); | ||
| if (call.includes(BINARY_PACKAGE_IDENTITY) || SOURCE_PACKAGE_URL.test(call) || CORDOVA_PACKAGE_URL.test(call)) { | ||
| return true; | ||
| } | ||
|
|
||
| const pinned = call.match(/name:\s*"([^"]+)"/); | ||
| return pinned !== null && PINNED_PACKAGE_NAMES.includes(pinned[1].toLowerCase()); | ||
| } | ||
|
|
||
| interface PackageCall { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated to this PR specifically: we should tweak the docs entry with this new identity https://github.com/ionic-team/capacitor-docs/blob/f5de151948208d132c1a76f5eee3f2cd44d45a27/docs/main/updating/9-0.md#capacitor-builds-from-node_modules
I suppose this can be done in ionic-team/capacitor-docs#639
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'll do it in ionic-team/capacitor-docs#639, but good catch.