Skip to content

Commit

Permalink
Fix getModule so it correctly handles Windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish committed Jul 27, 2022
1 parent 1d5ac33 commit 082d66f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions packages/node/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
import { basename, dirname } from '@sentry/utils';

/** normalizes Windows paths */
function normalisePath(path: string): string {
return path
.replace(/^[A-Z]:/, '') // remove Windows-style prefix
.replace(/\\/g, '/'); // replace all `\` instances with `/`
}

/** Gets the module from a filename */
export function getModule(filename: string | undefined): string | undefined {
if (!filename) {
return;
}

const normalizedFilename = normalisePath(filename);

// We could use optional chaining here but webpack does like that mixed with require
const base = `${
(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()
}/`;
const base = normalisePath(
`${(require && require.main && require.main.filename && dirname(require.main.filename)) || global.process.cwd()}/`,
);

// It's specifically a module
const file = basename(filename, '.js');
const file = basename(normalizedFilename, '.js');

const path = dirname(filename);
const path = dirname(normalizedFilename);
let n = path.lastIndexOf('/node_modules/');
if (n > -1) {
// /node_modules/ is 14 chars
Expand Down

0 comments on commit 082d66f

Please sign in to comment.