Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
withActiveSpan,
} from '@sentry/core';
import * as fsNamespace from 'fs';
import { syncBuiltinESMExports } from 'module';
import * as moduleNamespace from 'module';
import { promisify } from 'util';
import { CALLBACK_FUNCTIONS, PROMISE_FUNCTIONS, SYNC_FUNCTIONS } from './constants';
import type { FMember, FPMember, FsInstrumentationConfig, GenericFunction } from './types';
Expand All @@ -36,6 +36,11 @@ import { indexFs } from './utils';
// behind its default export instead. The CJS build has no `.default` and already gets the mutable object.
const fs = (fsNamespace as { default?: typeof fsNamespace }).default ?? fsNamespace;

// Deno before 2.8.2 has `syncBuiltinESMExports` only on the default export of `node:module`, so a named import fails to link.
type SyncBuiltinESMExports = { syncBuiltinESMExports?: () => void };
const moduleExports = moduleNamespace as unknown as SyncBuiltinESMExports & { default?: SyncBuiltinESMExports };
Comment thread
logaretm marked this conversation as resolved.
Resolved
const syncBuiltinESMExports = moduleExports.default?.syncBuiltinESMExports ?? moduleExports.syncBuiltinESMExports;

const SPAN_ORIGIN = 'auto.file.fs';
const SPAN_OP = FILE;

Expand Down Expand Up @@ -312,7 +317,7 @@ export function enableFsInstrumentation(config: FsInstrumentationConfig = {}): v

// Named ESM imports (`import { readFile } from 'fs'`) are snapshots of the CJS exports until synced.
try {
syncBuiltinESMExports();
syncBuiltinESMExports?.();
} catch {
// Cloudflare workerd exports `syncBuiltinESMExports` but throws `ERR_METHOD_NOT_IMPLEMENTED` when it is called.
}
Expand Down
17 changes: 17 additions & 0 deletions packages/node/test/integrations/fs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it, vi } from 'vitest';

const { syncBuiltinESMExports } = vi.hoisted(() => ({ syncBuiltinESMExports: vi.fn() }));

// Deno before 2.8.2 has `syncBuiltinESMExports` only on the default export of `node:module`, so a named import of it fails to link.
vi.mock('module', () => ({ default: { syncBuiltinESMExports } }));
vi.mock('fs', () => ({ default: { promises: {}, realpath: {}, realpathSync: {} } }));

describe('enableFsInstrumentation', () => {
it('syncs the builtin ESM exports when `module` has no named `syncBuiltinESMExports` export', async () => {
const { enableFsInstrumentation } = await import('../../src/integrations/fs/vendored/instrumentation');

enableFsInstrumentation();

expect(syncBuiltinESMExports).toHaveBeenCalledTimes(1);
});
});
Loading