Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): Correctly handle Windows paths when resolving module name #5476

Merged
merged 7 commits into from
Jul 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
timfish marked this conversation as resolved.
Show resolved Hide resolved
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