Fix autolinking for libraries with multiple ReactPackage classes#55968
Fix autolinking for libraries with multiple ReactPackage classes#55968Summys wants to merge 4 commits intofacebook:mainfrom
Conversation
|
|
||
| // Use FQCN to avoid class name collisions between different packages | ||
| val fqcn = extractFqcnFromImport(interpolateDynamicValues(packageImportPath, packageName)) | ||
| // Use FQCNs to avoid class name collisions between different packages. |
There was a problem hiding this comment.
I don't understand this statement.
Why would a library want to register more than 1 package to avoid name collision?
The mechanism to avoid name collision is to use FQCN that are owned by the company that is releasing the software. I don't get how having 2 packages is related to this problem
| * Extracts all fully qualified class names from one or more import statements. E.g., "import | ||
| * com.foo.bar.A;\nimport com.foo.bar.B;" -> ["com.foo.bar.A", "com.foo.bar.B"] | ||
| */ | ||
| internal fun extractAllFqcnsFromImport(importStatements: String): List<String> = |
There was a problem hiding this comment.
I have to mention I'm not a huge fan of this approach. Instead of doing this we should probably have an extra field inside the CLI config output called packageImportPaths which accepts an Array.
Then RNGP can orchestrate between the 2, rather than having to parse a string containing multiple imports.
There was a problem hiding this comment.
I added the extra field, but it doesn’t fix the previous behavior, and it seems the maintainer pushed the kind of workaround I wanted to avoid
There was a problem hiding this comment.
I added the extra field, but it doesn’t fix the previous behavior, and it seems the maintainer pushed the kind of workaround I wanted to avoid
I'm not sure I follow you here. What's the workaround? Have they merged everything inside a single package?
`extractFqcnFromImport` used `Regex.find()` which only returned the first FQCN from `packageImportPath`. Libraries registering multiple ReactPackage classes (e.g. react-native-appsflyer with both RNAppsFlyerPackage and PCAppsFlyerPackage) would have only the first class qualified, leaving others as bare class names causing `cannot find symbol` compilation errors in the generated PackageList.java. Additionally, the replacement regex `\bClassName\b` could match inside already-qualified names (since `.` is not a word character), causing double-qualification if packageInstance already contained FQCNs. Changes: - Add `extractAllFqcnsFromImport` that uses `Regex.findAll()` to extract all FQCNs from import statements - Replace bare class names sequentially with a `(?<!\.)` negative lookbehind to prevent double-qualification - Add regression tests for multi-package libraries and pre-qualified packageInstance entries Made-with: Cursor
…ses" This reverts commit 71e4936.
Instead of regex-parsing Java import statements from the packageImportPath string, introduce a new packageImportPaths field that accepts a structured array of FQCNs directly from the CLI config. RNGP prefers packageImportPaths when present and falls back to the legacy extractFqcnFromImport path for backward compatibility with older CLI versions that don't yet emit the new field. Made-with: Cursor
71e4936 to
5f8dd41
Compare
Summary
Fixes Android autolinking for libraries that register multiple
ReactPackageclasses viareact-native.config.js(e.g.react-native-appsflyerwhich exposes bothRNAppsFlyerPackageandPCAppsFlyerPackage).The Bug
In
GeneratePackageListTask.kt,extractFqcnFromImport()usesRegex.find()which only returns the first match. When a library'spackageImportPathcontains multiple import statements:Only
RNAppsFlyerPackagegets its FQCN extracted and substituted inpackageInstance.PCAppsFlyerPackageis left as a bare class name in the generatedPackageList.java, causing:Additionally, the existing replacement regex
\bClassName\bcan match inside already-qualified names (since.is not a word character), causing double-qualification (e.g.com.foo.com.foo.ClassName) ifpackageInstancealready uses FQCNs.The Fix
extractAllFqcnsFromImport()that usesRegex.findAll()to extract all FQCNs from import statements.composePackageInstance()to iterate over all extracted FQCNs and replace each bare class name sequentially using a(?<!\.)negative lookbehind to prevent double-qualification of already-qualified names.Affected Libraries
react-native-appsflyer(registersRNAppsFlyerPackage+PCAppsFlyerPackage)ReactPackageclasses in theirreact-native.config.jsChangelog:
[ANDROID] [FIXED] - Fix autolinking for libraries registering multiple ReactPackage classes
Test Plan
Added unit tests covering:
extractAllFqcnsFromImportwith multiple imports, single import, and invalid inputcomposePackageInstancewith a multi-package library verifying both classes get fully qualifiedcomposePackageInstancewith pre-qualifiedpackageInstanceentries verifying no double-qualificationMade with Cursor