Skip to content

Commit 664fb8a

Browse files
fix(expo): Reduce waning messages spam when a property in configuration is missing (#3631)
1 parent 9148964 commit 664fb8a

File tree

7 files changed

+75
-40
lines changed

7 files changed

+75
-40
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- You should not set the auth token in the plugin config except for local testing. Instead, use the `SENTRY_AUTH_TOKEN` env variable, as pointed out in our [docs](https://docs.sentry.io/platforms/react-native/manual-setup/expo/).
1010
- In addition to showing a warning, we are now actively removing an `authToken` from the plugin config if it was set.
1111
- If you had set the auth token in the plugin config previously, **and** built and published an app with that config, you should [rotate your token](https://docs.sentry.io/product/accounts/auth-tokens/).
12+
- Reduce waning messages spam when a property in Expo plugin configuration is missing ([#3631](https://github.com/getsentry/sentry-react-native/pull/3631))
1213

1314
## 5.19.0
1415

plugin/src/utils.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ const sdkPackage: {
1515
// eslint-disable-next-line @typescript-eslint/no-var-requires
1616
} = require('../../package.json');
1717

18-
const SDK_PACKAGE_NAME = sdkPackage.name;
18+
const SDK_PACKAGE_NAME = `${sdkPackage.name}/expo`;
19+
20+
const warningMap = new Map<string, boolean>();
21+
export function warnOnce(message: string): void {
22+
if (!warningMap.has(message)) {
23+
warningMap.set(message, true);
24+
// eslint-disable-next-line no-console
25+
console.warn(yellow(`${logPrefix()} ${message}`));
26+
}
27+
}
28+
29+
export function logPrefix(): string {
30+
return `› ${bold('[@sentry/react-native/expo]')}`;
31+
}
32+
33+
/**
34+
* The same as `chalk.yellow`
35+
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
36+
*/
37+
export function yellow(message: string): string {
38+
return `\x1b[33m${message}\x1b[0m`;
39+
}
40+
41+
/**
42+
* The same as `chalk.bold`
43+
* This code is part of the SDK, we don't want to introduce a dependency on `chalk` just for this.
44+
*/
45+
export function bold(message: string): string {
46+
return `\x1b[1m${message}\x1b[22m`;
47+
}
1948

2049
export { sdkPackage, SDK_PACKAGE_NAME };

plugin/src/withSentry.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { ConfigPlugin } from 'expo/config-plugins';
2-
import { createRunOncePlugin, WarningAggregator } from 'expo/config-plugins';
2+
import { createRunOncePlugin } from 'expo/config-plugins';
33

4-
import { SDK_PACKAGE_NAME, sdkPackage } from './utils';
4+
import { bold, sdkPackage, warnOnce } from './utils';
55
import { withSentryAndroid } from './withSentryAndroid';
66
import { withSentryIOS } from './withSentryIOS';
77

@@ -25,18 +25,12 @@ const withSentryPlugin: ConfigPlugin<PluginProps | void> = (config, props) => {
2525
try {
2626
cfg = withSentryAndroid(cfg, sentryProperties);
2727
} catch (e) {
28-
WarningAggregator.addWarningAndroid(
29-
SDK_PACKAGE_NAME,
30-
`There was a problem configuring sentry-expo in your native Android project: ${e}`,
31-
);
28+
warnOnce(`There was a problem with configuring your native Android project: ${e}`);
3229
}
3330
try {
3431
cfg = withSentryIOS(cfg, sentryProperties);
3532
} catch (e) {
36-
WarningAggregator.addWarningIOS(
37-
SDK_PACKAGE_NAME,
38-
`There was a problem configuring sentry-expo in your native iOS project: ${e}`,
39-
);
33+
warnOnce(`There was a problem with configuring your native iOS project: ${e}`);
4034
}
4135
}
4236

@@ -54,11 +48,17 @@ export function getSentryProperties(props: PluginProps | void): string | null {
5448
const missingProperties = ['organization', 'project'].filter(each => !props?.hasOwnProperty(each));
5549

5650
if (missingProperties.length) {
57-
const warningMessage = `Missing Sentry configuration properties: ${missingProperties.join(
58-
', ',
59-
)} in config plugin. Builds will fall back to environment variables. See: https://docs.sentry.io/platforms/react-native/manual-setup/.`;
60-
WarningAggregator.addWarningAndroid(SDK_PACKAGE_NAME, warningMessage);
61-
WarningAggregator.addWarningIOS(SDK_PACKAGE_NAME, warningMessage);
51+
const missingPropertiesString = bold(missingProperties.join(', '));
52+
const warningMessage = `Missing config for ${missingPropertiesString}. Environment variables will be used as a fallback during the build. https://docs.sentry.io/platforms/react-native/manual-setup/`;
53+
warnOnce(warningMessage);
54+
}
55+
56+
if (authToken) {
57+
warnOnce(
58+
`Detected unsecure use of 'authToken' in Sentry plugin configuration. To avoid exposing the token use ${bold(
59+
'SENTRY_AUTH_TOKEN',
60+
)} environment variable instead. https://docs.sentry.io/platforms/react-native/manual-setup/`,
61+
);
6262
}
6363

6464
return `defaults.url=${url}

plugin/src/withSentryAndroid.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ConfigPlugin } from 'expo/config-plugins';
2-
import { WarningAggregator, withAppBuildGradle, withDangerousMod } from 'expo/config-plugins';
2+
import { withAppBuildGradle, withDangerousMod } from 'expo/config-plugins';
33
import * as path from 'path';
44

5-
import { SDK_PACKAGE_NAME, writeSentryPropertiesTo } from './utils';
5+
import { warnOnce, writeSentryPropertiesTo } from './utils';
66

77
export const withSentryAndroid: ConfigPlugin<string> = (config, sentryProperties: string) => {
88
const cfg = withAppBuildGradle(config, config => {
@@ -39,8 +39,7 @@ export function modifyAppBuildGradle(buildGradle: string): string {
3939
const pattern = /^android {/m;
4040

4141
if (!buildGradle.match(pattern)) {
42-
WarningAggregator.addWarningAndroid(
43-
SDK_PACKAGE_NAME,
42+
warnOnce(
4443
'Could not find `^android {` in `android/app/build.gradle`. Please open a bug report at https://github.com/getsentry/sentry-react-native.',
4544
);
4645
return buildGradle;

plugin/src/withSentryIOS.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
22
import type { ConfigPlugin, XcodeProject } from 'expo/config-plugins';
3-
import { WarningAggregator, withDangerousMod, withXcodeProject } from 'expo/config-plugins';
3+
import { withDangerousMod, withXcodeProject } from 'expo/config-plugins';
44
import * as path from 'path';
55

6-
import { SDK_PACKAGE_NAME, writeSentryPropertiesTo } from './utils';
6+
import { warnOnce, writeSentryPropertiesTo } from './utils';
77

88
type BuildPhase = { shellScript: string };
99

@@ -46,14 +46,24 @@ export const withSentryIOS: ConfigPlugin<string> = (config, sentryProperties: st
4646
};
4747

4848
export function modifyExistingXcodeBuildScript(script: BuildPhase): void {
49-
if (
50-
!script.shellScript.match(/(packager|scripts)\/react-native-xcode\.sh\b/) ||
51-
script.shellScript.includes('sentry-xcode.sh') ||
52-
script.shellScript.includes('@sentry')
53-
) {
54-
WarningAggregator.addWarningIOS(
55-
SDK_PACKAGE_NAME,
56-
"Unable to modify build script 'Bundle React Native code and images'. Please open a bug report at https://github.com/expo/sentry-expo.",
49+
if (!script.shellScript.match(/(packager|scripts)\/react-native-xcode\.sh\b/)) {
50+
warnOnce(
51+
`'react-native-xcode.sh' not found in 'Bundle React Native code and images'.
52+
Please open a bug report at https://github.com/getsentry/sentry-react-native`,
53+
);
54+
return;
55+
}
56+
57+
if (script.shellScript.includes('sentry-xcode.sh')) {
58+
warnOnce("The latest 'sentry-xcode.sh' script already exists in 'Bundle React Native code and images'.");
59+
return;
60+
}
61+
62+
if (script.shellScript.includes('@sentry')) {
63+
warnOnce(
64+
`Outdated or custom Sentry script found in 'Bundle React Native code and images'.
65+
Regenerate the native project to use the latest script.
66+
Run npx expo prebuild --clean`,
5767
);
5868
return;
5969
}

test/expo-plugin/modifyAppBuildGradle.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { WarningAggregator } from 'expo/config-plugins';
2-
1+
import { warnOnce } from '../../plugin/src/utils';
32
import { modifyAppBuildGradle } from '../../plugin/src/withSentryAndroid';
43

5-
jest.mock('expo/config-plugins');
4+
jest.mock('../../plugin/src/utils');
65

76
const buildGradleWithSentry = `
87
apply from: new File(["node", "--print", "require('path').dirname(require.resolve('@sentry/react-native/package.json'))"].execute().text.trim(), "sentry.gradle")
@@ -49,8 +48,7 @@ describe('Configures Android native project correctly', () => {
4948
});
5049

5150
it('Warns to file a bug report if no react.gradle is found', () => {
52-
(WarningAggregator.addWarningAndroid as jest.Mock).mockImplementationOnce(jest.fn());
5351
modifyAppBuildGradle(buildGradleWithOutReactGradleScript);
54-
expect(WarningAggregator.addWarningAndroid).toHaveBeenCalled();
52+
expect(warnOnce).toHaveBeenCalled();
5553
});
5654
});

test/expo-plugin/modifyXcodeProject.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { WarningAggregator } from 'expo/config-plugins';
2-
1+
import { warnOnce } from '../../plugin/src/utils';
32
import { modifyExistingXcodeBuildScript } from '../../plugin/src/withSentryIOS';
43

5-
jest.mock('expo/config-plugins');
4+
jest.mock('../../plugin/src/utils');
65

76
const buildScriptWithoutSentry = {
87
shellScript: JSON.stringify(`"
@@ -74,8 +73,7 @@ describe('Configures iOS native project correctly', () => {
7473
});
7574

7675
it("Warns to file a bug report if build script isn't what we expect to find", () => {
77-
(WarningAggregator.addWarningAndroid as jest.Mock).mockImplementationOnce(jest.fn());
7876
modifyExistingXcodeBuildScript(buildScriptWeDontExpect);
79-
expect(WarningAggregator.addWarningIOS).toHaveBeenCalled();
77+
expect(warnOnce).toHaveBeenCalled();
8078
});
8179
});

0 commit comments

Comments
 (0)