-
Notifications
You must be signed in to change notification settings - Fork 3
fix(react-native): remove import cycles that crash under Metro's import transform and fix autolinking builds #397
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
Merged
KishanPRao
merged 4 commits into
main
from
bugfix/react-native-import-cycles-and-autolinking
Sep 29, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
72fe3dc
react-native: remove circular imports that break under Metro's import…
melekr d2f5515
react-native: make Android autolinking compile under Expo and React N…
melekr 1e773a4
react-native: support use_frameworks! with dynamic linkage
melekr edbd8c6
Merge branch 'main' into bugfix/react-native-import-cycles-and-autoli…
KishanPRao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "plugins": ["import"], | ||
| "settings": { | ||
| "import/ignore": ["node_modules"], | ||
| "import/resolver": { | ||
| "node": { | ||
| "extensions": [".js", ".jsx", ".ts", ".tsx"] | ||
| } | ||
| } | ||
| }, | ||
| "rules": { | ||
| "import/no-cycle": ["error", { "ignoreExternal": true }] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 2 additions & 76 deletions
78
packages/react-native/src/builder/BacktraceClientBuilder.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,76 +1,2 @@ | ||
| import { BacktraceCoreClientBuilder, warnFailure } from '@backtrace/sdk-core'; | ||
| import { Platform } from 'react-native'; | ||
| import { NativeAttributeProvider } from '../attributes/NativeAttributeProvider'; | ||
| import { ReactNativeAttributeProvider } from '../attributes/ReactNativeAttributeProvider'; | ||
| import { BacktraceClient } from '../BacktraceClient'; | ||
| import { AppStateBreadcrumbSubscriber } from '../breadcrumbs/events/AppStateBreadcrumbSubscriber'; | ||
| import { DimensionChangeBreadcrumbSubscriber } from '../breadcrumbs/events/DimensionChangeBreadcrumbSubscriber'; | ||
| import { WebRequestEventSubscriber } from '../breadcrumbs/events/WebRequestEventSubscriber'; | ||
| import { DebuggerHelper } from '../common/DebuggerHelper'; | ||
| import { ReactNativeFileSystem } from '../storage'; | ||
| import type { BacktraceClientSetup } from './BacktraceClientSetup'; | ||
|
|
||
| export class BacktraceClientBuilder extends BacktraceCoreClientBuilder<BacktraceClientSetup> { | ||
| constructor(clientSetup: BacktraceClientSetup) { | ||
| super(clientSetup); | ||
|
|
||
| this.addAttributeProvider(new ReactNativeAttributeProvider()); | ||
| if (!DebuggerHelper.isNativeBridgeEnabled()) { | ||
| return; | ||
| } | ||
|
|
||
| if (Platform.OS !== 'android' && Platform.OS !== 'ios') { | ||
| return; | ||
| } | ||
|
|
||
| const attributeProviders = Platform.select({ | ||
| ios: [ | ||
| new NativeAttributeProvider('BacktraceApplicationAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('BacktraceDeviceAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('BacktraceSystemAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('BacktraceMemoryUsageAttributeProvider', 'dynamic'), | ||
| new NativeAttributeProvider('BacktraceCpuAttributeProvider', 'dynamic'), | ||
| ], | ||
| android: [ | ||
| new NativeAttributeProvider('BacktraceApplicationAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('BacktraceDeviceAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('BacktraceSystemAttributeProvider', 'scoped'), | ||
| new NativeAttributeProvider('MemoryInformationAttributeProvider', 'dynamic'), | ||
| new NativeAttributeProvider('ProcessAttributeProvider', 'dynamic'), | ||
| ], | ||
| default: [], | ||
| }); | ||
|
|
||
| for (const provider of attributeProviders) { | ||
| this.addAttributeProvider(provider); | ||
| } | ||
|
|
||
| const fileSystem = this.createFileSystem(); | ||
| if (fileSystem) { | ||
| this.useFileSystem(fileSystem); | ||
| } | ||
| this.useBreadcrumbSubscriber(new AppStateBreadcrumbSubscriber()); | ||
| this.useBreadcrumbSubscriber(new DimensionChangeBreadcrumbSubscriber()); | ||
| this.useBreadcrumbSubscriber(new WebRequestEventSubscriber()); | ||
| } | ||
|
|
||
| public useFileSystem(fileSystem: ReactNativeFileSystem): this { | ||
| super.useFileSystem(fileSystem); | ||
| return this; | ||
| } | ||
|
|
||
| private createFileSystem(): ReactNativeFileSystem | undefined { | ||
| try { | ||
| return new ReactNativeFileSystem(); | ||
| } catch (err) { | ||
| warnFailure('native storage modules are missing, the database and native crash reporting are off', err); | ||
| return undefined; | ||
| } | ||
| } | ||
|
|
||
| public build(): BacktraceClient { | ||
| const instance = new BacktraceClient(this.clientSetup); | ||
| instance.initialize(); | ||
| return instance; | ||
| } | ||
| } | ||
| // `BacktraceClientBuilder` lives next to `BacktraceClient` (see the note there); this module keeps the import path. | ||
| export { BacktraceClientBuilder } from '../BacktraceClient'; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-native/src/handlers/UnhandledExceptionHandler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/react-native/src/handlers/android/AndroidUnhandledExceptionHandler.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/react-native/tests/clientInitializationTests.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { NativeModules } from 'react-native'; | ||
| import { promisify } from 'util'; | ||
| import { mockStreamFileSystem } from './_mocks/fileSystem'; | ||
|
|
||
| // This package's jest config replaces the react-native preset's setupFiles, so the real Platform throws. | ||
| jest.mock('react-native', () => ({ | ||
| NativeModules: {}, | ||
| Platform: { | ||
| OS: 'ios', | ||
| select: (options: Record<string, unknown>) => (options.ios !== undefined ? options.ios : options.default), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../src/common/platformHelper', () => ({ | ||
| version: () => '0.81.6', | ||
| })); | ||
|
|
||
| // Keep the builder away from the native modules: | ||
| // with the bridge reported as unavailable it registers neither native attribute providers nor the native file system, and the test injects a mocked file system instead. | ||
| NativeModules.UIManager = {}; | ||
| NativeModules.BacktraceDirectoryProvider = { applicationDirectory: () => '/' }; | ||
|
|
||
| // Loaded through the package entry point on purpose: this is the path applications take, | ||
| // it's where the circular imports between the client, the builder and the breadcrumbs storage used to surface. | ||
| /* eslint-disable @typescript-eslint/no-var-requires */ | ||
| const { BacktraceClient, BacktraceClientBuilder, BacktraceFileAttachment } = require('../src'); | ||
| /* eslint-enable @typescript-eslint/no-var-requires */ | ||
|
|
||
| const nextTick = promisify(process.nextTick); | ||
|
|
||
| const options = { | ||
| url: 'https://submit.backtrace.io/universe/token/json', | ||
| database: { enable: true, path: '/backtrace' }, | ||
| metrics: { enable: false }, | ||
| breadcrumbs: { enable: true, maximumBreadcrumbs: 4 }, | ||
| captureUnhandledErrors: false, | ||
| captureUnhandledPromiseRejections: false, | ||
| userAttributes: { application: 'clientInitialization', 'application.version': '1.0.0' }, | ||
| }; | ||
|
|
||
| describe('BacktraceClient initialization through the package entry point', () => { | ||
| afterEach(() => { | ||
| BacktraceClient.instance?.dispose(); | ||
| // initialize() stores the singleton on BacktraceClient itself, which dispose() does not clear. | ||
| (BacktraceClient as { _instance?: unknown })._instance = undefined; | ||
| }); | ||
|
|
||
| it('Should create the builder from the client', () => { | ||
| expect(BacktraceClient.builder(options)).toBeInstanceOf(BacktraceClientBuilder); | ||
| }); | ||
|
|
||
| it('Should initialize the client through the builder', () => { | ||
| const client = BacktraceClient.initialize(options, (builder: typeof BacktraceClientBuilder.prototype) => { | ||
| expect(builder).toBeInstanceOf(BacktraceClientBuilder); | ||
| builder.useFileSystem(mockStreamFileSystem()); | ||
| }); | ||
|
|
||
| expect(client).toBeInstanceOf(BacktraceClient); | ||
| expect(BacktraceClient.instance).toBe(client); | ||
| }); | ||
|
|
||
| it('Should expose breadcrumb files as file attachments', async () => { | ||
| const client = BacktraceClient.initialize(options, (builder: typeof BacktraceClientBuilder.prototype) => | ||
| builder.useFileSystem(mockStreamFileSystem()), | ||
| ); | ||
|
|
||
| // Enough breadcrumbs to rotate the breadcrumb files at least once. | ||
| for (let i = 0; i < 20; i++) { | ||
| client.breadcrumbs?.info(`breadcrumb-${i}`); | ||
| await nextTick(); | ||
| } | ||
| for (let i = 0; i < 10; i++) { | ||
| await nextTick(); | ||
| } | ||
|
|
||
| const attachments = client.attachments.filter((attachment) => attachment instanceof BacktraceFileAttachment); | ||
| expect(attachments.length).toBeGreaterThan(0); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
safe to delete.