Skip to content

Commit 7fd512a

Browse files
fix(metro): Use env for default Babel transformer path (#4298)
1 parent e8464db commit 7fd512a

File tree

5 files changed

+27
-94
lines changed

5 files changed

+27
-94
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@
66
> make sure you follow our [migration guide](https://docs.sentry.io/platforms/react-native/migration/) first.
77
<!-- prettier-ignore-end -->
88
9+
## Unreleased
10+
11+
### Fixes
12+
13+
- Remove `.sentry` tmp directory and use environmental variables instead to save default Babel transformer path ([#4298](https://github.com/getsentry/sentry-react-native/pull/4298))
14+
- This resolves concurrency issues when running multiple bundle processes
15+
916
## 6.3.0-beta.1
1017

1118
### Features

packages/core/src/js/tools/metroconfig.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as process from 'process';
55
import { env } from 'process';
66

77
import { enableLogger } from './enableLogger';
8-
import { cleanDefaultBabelTransformerPath, saveDefaultBabelTransformerPath } from './sentryBabelTransformerUtils';
8+
import { setSentryDefaultBabelTransformerPathEnv } from './sentryBabelTransformerUtils';
99
import { createSentryMetroSerializer, unstable_beforeAssetSerializationPlugin } from './sentryMetroSerializer';
1010
import type { DefaultConfigOptions } from './vendor/expo/expoconfig';
1111
export * from './sentryMetroSerializer';
@@ -143,10 +143,7 @@ export function withSentryBabelTransformer(config: MetroConfig): MetroConfig {
143143
}
144144

145145
if (defaultBabelTransformerPath) {
146-
saveDefaultBabelTransformerPath(defaultBabelTransformerPath);
147-
process.on('exit', () => {
148-
cleanDefaultBabelTransformerPath();
149-
});
146+
setSentryDefaultBabelTransformerPathEnv(defaultBabelTransformerPath);
150147
}
151148

152149
return {

packages/core/src/js/tools/sentryBabelTransformerUtils.ts

+12-40
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,34 @@
11
import { logger } from '@sentry/utils';
2-
import * as fs from 'fs';
3-
import * as path from 'path';
42
import * as process from 'process';
53

64
import type { BabelTransformer } from './vendor/metro/metroBabelTransformer';
75

8-
/**
9-
* Saves default Babel transformer path to the project root.
10-
*/
11-
export function saveDefaultBabelTransformerPath(defaultBabelTransformerPath: string): void {
12-
try {
13-
fs.mkdirSync(path.join(process.cwd(), '.sentry'), { recursive: true });
14-
fs.writeFileSync(getDefaultBabelTransformerPath(), defaultBabelTransformerPath);
15-
logger.debug('Saved default Babel transformer path');
16-
} catch (e) {
17-
// eslint-disable-next-line no-console
18-
console.error('[Sentry] Failed to save default Babel transformer path:', e);
19-
}
20-
}
6+
export const SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH = 'SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH';
217

228
/**
23-
* Reads default Babel transformer path from the project root.
9+
* Sets default Babel transformer path to the environment variables.
2410
*/
25-
export function readDefaultBabelTransformerPath(): string | undefined {
26-
try {
27-
return fs.readFileSync(getDefaultBabelTransformerPath()).toString();
28-
} catch (e) {
29-
// eslint-disable-next-line no-console
30-
console.error('[Sentry] Failed to read default Babel transformer path:', e);
31-
}
32-
return undefined;
11+
export function setSentryDefaultBabelTransformerPathEnv(defaultBabelTransformerPath: string): void {
12+
process.env[SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH] = defaultBabelTransformerPath;
13+
logger.debug(`Saved default Babel transformer path ${defaultBabelTransformerPath}`);
3314
}
3415

3516
/**
36-
* Cleans default Babel transformer path from the project root.
17+
* Reads default Babel transformer path from the environment variables.
3718
*/
38-
export function cleanDefaultBabelTransformerPath(): void {
39-
try {
40-
fs.unlinkSync(getDefaultBabelTransformerPath());
41-
logger.debug('Cleaned default Babel transformer path');
42-
} catch (e) {
43-
// We don't want to fail the build if we can't clean the file
44-
// eslint-disable-next-line no-console
45-
console.error('[Sentry] Failed to clean default Babel transformer path:', e);
46-
}
47-
}
48-
49-
function getDefaultBabelTransformerPath(): string {
50-
return path.join(process.cwd(), '.sentry/.defaultBabelTransformerPath');
19+
export function getSentryDefaultBabelTransformerPathEnv(): string | undefined {
20+
return process.env[SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH];
5121
}
5222

5323
/**
5424
* Loads default Babel transformer from `@react-native/metro-config` -> `@react-native/metro-babel-transformer`.
5525
*/
5626
export function loadDefaultBabelTransformer(): BabelTransformer {
57-
const defaultBabelTransformerPath = readDefaultBabelTransformerPath();
27+
const defaultBabelTransformerPath = getSentryDefaultBabelTransformerPathEnv();
5828
if (!defaultBabelTransformerPath) {
59-
throw new Error('Default Babel Transformer Path not found in `.sentry` directory.');
29+
throw new Error(
30+
`Default Babel transformer path environment variable ${SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH} is not set.`,
31+
);
6032
}
6133

6234
logger.debug(`Loading default Babel transformer from ${defaultBabelTransformerPath}`);

packages/core/test/tools/metroconfig.test.ts

+3-40
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
1-
jest.mock('fs', () => {
2-
return {
3-
readFile: jest.fn(),
4-
mkdirSync: jest.fn(),
5-
writeFileSync: jest.fn(),
6-
unlinkSync: jest.fn(),
7-
};
8-
});
9-
101
import type { getDefaultConfig } from 'expo/metro-config';
11-
import * as fs from 'fs';
122
import type { MetroConfig } from 'metro';
13-
import * as path from 'path';
143
import * as process from 'process';
154

165
import {
@@ -19,6 +8,7 @@ import {
198
withSentryFramesCollapsed,
209
withSentryResolver,
2110
} from '../../src/js/tools/metroconfig';
11+
import { SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH } from '../../src/js/tools/sentryBabelTransformerUtils';
2212

2313
type MetroFrame = Parameters<Required<Required<MetroConfig>['symbolicator']>['customizeFrame']>[0];
2414

@@ -69,43 +59,16 @@ describe('metroconfig', () => {
6959
},
7060
);
7161

72-
test.each([
73-
[{ transformer: { babelTransformerPath: 'babelTransformerPath' }, projectRoot: 'project/root' }],
74-
[{ transformer: { babelTransformerPath: 'babelTransformerPath' } }],
75-
])('save default babel transformer path to a file', () => {
62+
test('save default babel transformer path to environment variable', () => {
7663
const defaultBabelTransformerPath = '/default/babel/transformer';
7764

7865
withSentryBabelTransformer({
7966
transformer: {
8067
babelTransformerPath: defaultBabelTransformerPath,
8168
},
82-
projectRoot: 'project/root',
8369
});
8470

85-
expect(fs.mkdirSync).toHaveBeenCalledWith(path.join(process.cwd(), '.sentry'), { recursive: true });
86-
expect(fs.writeFileSync).toHaveBeenCalledWith(
87-
path.join(process.cwd(), '.sentry/.defaultBabelTransformerPath'),
88-
defaultBabelTransformerPath,
89-
);
90-
});
91-
92-
test('clean default babel transformer path file on exit', () => {
93-
const processOnSpy: jest.SpyInstance = jest.spyOn(process, 'on');
94-
95-
const defaultBabelTransformerPath = 'defaultBabelTransformerPath';
96-
97-
withSentryBabelTransformer({
98-
transformer: {
99-
babelTransformerPath: defaultBabelTransformerPath,
100-
},
101-
projectRoot: 'project/root',
102-
});
103-
104-
const actualExitHandler: () => void | undefined = processOnSpy.mock.calls[0][1];
105-
actualExitHandler?.();
106-
107-
expect(processOnSpy).toHaveBeenCalledWith('exit', expect.any(Function));
108-
expect(fs.unlinkSync).toHaveBeenCalledWith(path.join(process.cwd(), '.sentry/.defaultBabelTransformerPath'));
71+
expect(process.env[SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH]).toBe(defaultBabelTransformerPath);
10972
});
11073

11174
test('return config with sentry babel transformer path', () => {

packages/core/test/tools/sentryBabelTransformer.test.ts

+3-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
jest.mock('fs', () => {
2-
return {
3-
readFileSync: jest.fn(),
4-
};
5-
});
1+
import * as process from 'process';
62

7-
import * as fs from 'fs';
3+
import { SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH } from '../../src/js/tools/sentryBabelTransformerUtils';
84

9-
// needs to be defined before sentryBabelTransformer is imported
10-
// the transformer is created on import (side effect)
11-
(fs.readFileSync as jest.Mock).mockReturnValue(require.resolve('./fixtures/mockBabelTransformer.js'));
5+
process.env[SENTRY_DEFAULT_BABEL_TRANSFORMER_PATH] = require.resolve('./fixtures/mockBabelTransformer.js');
126

137
import * as SentryBabelTransformer from '../../src/js/tools/sentryBabelTransformer';
148
import type { BabelTransformerArgs } from '../../src/js/tools/vendor/metro/metroBabelTransformer';

0 commit comments

Comments
 (0)