Skip to content

Commit

Permalink
ref(profiling) test local require
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Nov 25, 2024
1 parent b271bc8 commit 7b07ab9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
21 changes: 21 additions & 0 deletions dev-packages/e2e-tests/test-applications/node-profiling/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as Sentry from '@sentry/node';
import { nodeProfilingIntegration } from '@sentry/profiling-node';

const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

Sentry.init({
dsn: 'https://[email protected]/6625302',
integrations: [nodeProfilingIntegration()],
tracesSampleRate: 1.0,
profilesSampleRate: 1.0,
});

Sentry.startSpan({ name: 'Precompile test' }, async () => {
await wait(500);
});

// Test that globalThis.require is not defined by any side effects of the profiling
// https://github.com/getsentry/sentry-javascript/issues/13662
if (globalThis.require !== undefined) {
throw new Error('globalThis.require should not be defined, check that profiling integration is not defining it, received: ' + typeof globalThis.require);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"typecheck": "tsc --noEmit",
"build": "node build.mjs && node build.shimmed.mjs",
"test": "node dist/index.js && node --experimental-require-module dist/index.js && node dist/index.shimmed.mjs",
"test": "node dist/index.js && node --experimental-require-module dist/index.js && node dist/index.shimmed.mjs && node index.mjs",
"clean": "npx rimraf node_modules dist",
"test:electron": "$(pnpm bin)/electron-rebuild && playwright test",
"test:build": "pnpm run typecheck && pnpm run build",
Expand Down
35 changes: 18 additions & 17 deletions packages/profiling-node/rollup.npm.config.mjs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import commonjs from '@rollup/plugin-commonjs';
import { makeBaseNPMConfig, makeNPMConfigVariants } from '@sentry-internal/rollup-utils';

export const ESMShim = `
import cjsUrl from 'node:url';
import cjsPath from 'node:path';
export const ESMImportShim = `
import cjsModule from 'node:module';
`;

if(typeof __filename === 'undefined'){
globalThis.__filename = cjsUrl.fileURLToPath(import.meta.url);
}
const ESMRequireShim = `
const require = cjsModule.createRequire(import.meta.url);
`

if(typeof __dirname === 'undefined'){
globalThis.__dirname = cjsPath.dirname(__filename);
}
if(typeof require === 'undefined'){
globalThis.require = cjsModule.createRequire(import.meta.url);
function makeESMImportShimPlugin(shim) {
return {
transform(code) {
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_IMPORT_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_IMPORT_SHIM/;
return code.replace(SHIM_REGEXP, shim);
},
};
}
`;

function makeESMShimPlugin(shim) {
function makeESMRequireShimPlugin(shim){
return {
transform(code) {
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_SHIM/;
const SHIM_REGEXP = /\/\/ #START_SENTRY_ESM_REQUIRE_SHIM[\s\S]*?\/\/ #END_SENTRY_ESM_REQUIRE_SHIM/;
return code.replace(SHIM_REGEXP, shim);
},
};
Expand All @@ -39,10 +38,12 @@ const variants = makeNPMConfigVariants(

for (const variant of variants) {
if (variant.output.format === 'esm') {
variant.plugins.push(makeESMShimPlugin(ESMShim));
variant.plugins.push(makeESMImportShimPlugin(ESMImportShim));
variant.plugins.push(makeESMRequireShimPlugin(ESMRequireShim))
} else {
// Remove the ESM shim comment
variant.plugins.push(makeESMShimPlugin(''));
variant.plugins.push(makeESMImportShimPlugin(''));
variant.plugins.push(makeESMRequireShimPlugin(''));
}
}

Expand Down
10 changes: 8 additions & 2 deletions packages/profiling-node/src/cpu_profiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import type {
} from './types';
import type { ProfileFormat } from './types';

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

const stdlib = familySync();
const platform = process.env['BUILD_PLATFORM'] || _platform();
Expand All @@ -34,6 +34,12 @@ const built_from_source_path = resolve(__dirname, '..', `./sentry_cpu_profiler-$
*/
// eslint-disable-next-line complexity
export function importCppBindingsModule(): PrivateV8CpuProfilerBindings {
// #START_SENTRY_ESM_REQUIRE_SHIM
// When building for ESM, we shim require to use createRequire and __dirname.
// We need to do this because .node extensions in esm are not supported.
// The comment below this line exists as a placeholder for where to insert the shim.
// #END_SENTRY_ESM_REQUIRE_SHIM

// If a binary path is specified, use that.
if (env['SENTRY_PROFILER_BINARY_PATH']) {
const envPath = env['SENTRY_PROFILER_BINARY_PATH'];
Expand Down

0 comments on commit 7b07ab9

Please sign in to comment.