Skip to content

Commit 7b07ab9

Browse files
committed
ref(profiling) test local require
1 parent b271bc8 commit 7b07ab9

File tree

4 files changed

+48
-20
lines changed

4 files changed

+48
-20
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as Sentry from '@sentry/node';
2+
import { nodeProfilingIntegration } from '@sentry/profiling-node';
3+
4+
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
5+
6+
Sentry.init({
7+
dsn: 'https://[email protected]/6625302',
8+
integrations: [nodeProfilingIntegration()],
9+
tracesSampleRate: 1.0,
10+
profilesSampleRate: 1.0,
11+
});
12+
13+
Sentry.startSpan({ name: 'Precompile test' }, async () => {
14+
await wait(500);
15+
});
16+
17+
// Test that globalThis.require is not defined by any side effects of the profiling
18+
// https://github.com/getsentry/sentry-javascript/issues/13662
19+
if (globalThis.require !== undefined) {
20+
throw new Error('globalThis.require should not be defined, check that profiling integration is not defining it, received: ' + typeof globalThis.require);
21+
}

dev-packages/e2e-tests/test-applications/node-profiling/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"scripts": {
66
"typecheck": "tsc --noEmit",
77
"build": "node build.mjs && node build.shimmed.mjs",
8-
"test": "node dist/index.js && node --experimental-require-module dist/index.js && node dist/index.shimmed.mjs",
8+
"test": "node dist/index.js && node --experimental-require-module dist/index.js && node dist/index.shimmed.mjs && node index.mjs",
99
"clean": "npx rimraf node_modules dist",
1010
"test:electron": "$(pnpm bin)/electron-rebuild && playwright test",
1111
"test:build": "pnpm run typecheck && pnpm run build",

packages/profiling-node/rollup.npm.config.mjs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import commonjs from '@rollup/plugin-commonjs';
22
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';
33

4-
export const ESMShim = `
5-
import cjsUrl from 'node:url';
6-
import cjsPath from 'node:path';
4+
export const ESMImportShim = `
75
import cjsModule from 'node:module';
6+
`;
87

9-
if(typeof __filename === 'undefined'){
10-
globalThis.__filename = cjsUrl.fileURLToPath(import.meta.url);
11-
}
8+
const ESMRequireShim = `
9+
const require = cjsModule.createRequire(import.meta.url);
10+
`
1211

13-
if(typeof __dirname === 'undefined'){
14-
globalThis.__dirname = cjsPath.dirname(__filename);
15-
}
16-
17-
if(typeof require === 'undefined'){
18-
globalThis.require = cjsModule.createRequire(import.meta.url);
12+
function makeESMImportShimPlugin(shim) {
13+
return {
14+
transform(code) {
15+
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_IMPORT_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_IMPORT_SHIM/;
16+
return code.replace(SHIM_REGEXP, shim);
17+
},
18+
};
1919
}
20-
`;
2120

22-
function makeESMShimPlugin(shim) {
21+
function makeESMRequireShimPlugin(shim){
2322
return {
2423
transform(code) {
25-
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_SHIM/;
24+
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_REQUIRE_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_REQUIRE_SHIM/;
2625
return code.replace(SHIM_REGEXP, shim);
2726
},
2827
};
@@ -39,10 +38,12 @@ const variants = makeNPMConfigVariants(
3938

4039
for (const variant of variants) {
4140
if (variant.output.format === 'esm') {
42-
variant.plugins.push(makeESMShimPlugin(ESMShim));
41+
variant.plugins.push(makeESMImportShimPlugin(ESMImportShim));
42+
variant.plugins.push(makeESMRequireShimPlugin(ESMRequireShim))
4343
} else {
4444
// Remove the ESM shim comment
45-
variant.plugins.push(makeESMShimPlugin(''));
45+
variant.plugins.push(makeESMImportShimPlugin(''));
46+
variant.plugins.push(makeESMRequireShimPlugin(''));
4647
}
4748
}
4849

packages/profiling-node/src/cpu_profiler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import type {
1515
} from './types';
1616
import type { ProfileFormat } from './types';
1717

18-
// #START_SENTRY_ESM_SHIM
18+
// #START_SENTRY_ESM_IMPORT_SHIM
1919
// When building for ESM, we shim require to use createRequire and __dirname.
2020
// We need to do this because .node extensions in esm are not supported.
2121
// The comment below this line exists as a placeholder for where to insert the shim.
22-
// #END_SENTRY_ESM_SHIM
22+
// #END_SENTRY_ESM_IMPORT_SHIM
2323

2424
const stdlib = familySync();
2525
const platform = process.env['BUILD_PLATFORM'] || _platform();
@@ -34,6 +34,12 @@ const built_from_source_path = resolve(__dirname, '..', `./sentry_cpu_profiler-$
3434
*/
3535
// eslint-disable-next-line complexity
3636
export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
37+
// #START_SENTRY_ESM_REQUIRE_SHIM
38+
// When building for ESM, we shim require to use createRequire and __dirname.
39+
// We need to do this because .node extensions in esm are not supported.
40+
// The comment below this line exists as a placeholder for where to insert the shim.
41+
// #END_SENTRY_ESM_REQUIRE_SHIM
42+
3743
// If a binary path is specified, use that.
3844
if (env['SENTRY_PROFILER_BINARY_PATH']) {
3945
const envPath = env['SENTRY_PROFILER_BINARY_PATH'];

0 commit comments

Comments
 (0)