Skip to content
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

fix(expo): Do not run the SDK during static routes generation #3730

Merged
merged 4 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Fixes

- Do not enable NativeFramesTracking when native is not available ([#3705](https://github.com/getsentry/sentry-react-native/pull/3705))
- Do not initialize the SDK during `expo-router` static routes generation ([#3730](https://github.com/getsentry/sentry-react-native/pull/3730))

### Dependencies

Expand Down
6 changes: 5 additions & 1 deletion src/js/sdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { TouchEventBoundary } from './touchevents';
import { ReactNativeProfiler, ReactNativeTracing } from './tracing';
import { DEFAULT_BUFFER_SIZE, makeNativeTransportFactory } from './transports/native';
import { makeUtf8TextEncoder } from './transports/TextEncoder';
import { getDefaultEnvironment, isExpoGo } from './utils/environment';
import { getDefaultEnvironment, isExpoGo, isRunningInMetroDevServer } from './utils/environment';
import { safeFactory, safeTracesSampler } from './utils/safe';
import { NATIVE } from './wrapper';

Expand All @@ -44,6 +44,10 @@ const DEFAULT_OPTIONS: ReactNativeOptions = {
* Inits the SDK and returns the final options.
*/
export function init(passedOptions: ReactNativeOptions): void {
if (isRunningInMetroDevServer()) {
lucas-zimerman marked this conversation as resolved.
Show resolved Hide resolved
return;
}

const reactNativeHub = new Hub(undefined, new ReactNativeScope());
makeMain(reactNativeHub);

Expand Down
14 changes: 14 additions & 0 deletions src/js/tools/metroconfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { MetroConfig, MixedOutput, Module, ReadOnlyGraph } from 'metro';
import { env } from 'process';

import { createSentryMetroSerializer, unstable_beforeAssetSerializationPlugin } from './sentryMetroSerializer';
import type { DefaultConfigOptions } from './vendor/expo/expoconfig';
Expand All @@ -12,6 +13,8 @@ export * from './sentryMetroSerializer';
* Collapses Sentry frames from the stack trace view in LogBox.
*/
export function withSentryConfig(config: MetroConfig): MetroConfig {
setSentryMetroDevServerEnvFlag();

let newConfig = config;

newConfig = withSentryDebugId(newConfig);
Expand All @@ -27,6 +30,8 @@ export function getSentryExpoConfig(
projectRoot: string,
options: DefaultConfigOptions & { getDefaultConfig?: typeof getSentryExpoConfig } = {},
): MetroConfig {
setSentryMetroDevServerEnvFlag();

const getDefaultConfig = options.getDefaultConfig || loadExpoMetroConfigModule().getDefaultConfig;
const config = getDefaultConfig(projectRoot, {
...options,
Expand Down Expand Up @@ -120,3 +125,12 @@ export function withSentryFramesCollapsed(config: MetroConfig): MetroConfig {
},
};
}

/**
* Sets the `___SENTRY_METRO_DEV_SERVER___` environment flag.
* This is used to determine if the SDK is running in Node in Metro Dev Server.
* For example during static routes generation in `expo-router`.
*/
function setSentryMetroDevServerEnvFlag(): void {
env.___SENTRY_METRO_DEV_SERVER___ = 'true';
}
12 changes: 12 additions & 0 deletions src/js/utils/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,15 @@ export function getHermesVersion(): string | undefined {
export function getDefaultEnvironment(): 'development' | 'production' {
return typeof __DEV__ !== 'undefined' && __DEV__ ? 'development' : 'production';
}

/** Check if SDK runs in Metro Dev Server side */
export function isRunningInMetroDevServer(): boolean {
if (
typeof RN_GLOBAL_OBJ.process !== 'undefined' &&
RN_GLOBAL_OBJ.process.env &&
RN_GLOBAL_OBJ.process.env.___SENTRY_METRO_DEV_SERVER___ === 'true'
) {
return true;
}
return false;
}
5 changes: 5 additions & 0 deletions src/js/utils/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export interface ReactNativeInternalGlobal extends InternalGlobal {
ErrorUtils?: ErrorUtils;
expo?: ExpoGlobalObject;
XMLHttpRequest?: typeof XMLHttpRequest;
process?: {
env?: {
___SENTRY_METRO_DEV_SERVER___?: string;
};
};
}

/** Get's the global object for the current JavaScript runtime */
Expand Down