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

ref(node): use native path module to infer path #7415

Merged
merged 1 commit into from
Mar 20, 2023
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
43 changes: 29 additions & 14 deletions packages/node/src/module.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,55 @@
import { basename, dirname } from '@sentry/utils';
import { posix, sep } from 'path';

const isWindowsPlatform = sep === '\\';

/** normalizes Windows paths */
function normalizePath(path: string): string {
function normalizeWindowsPath(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 {
export function getModule(
filename: string | undefined,
normalizeWindowsPathSeparator: boolean = isWindowsPlatform,
): string | undefined {
if (!filename) {
return;
}

const normalizedFilename = normalizePath(filename);
const normalizedFilename = normalizeWindowsPathSeparator ? normalizeWindowsPath(filename) : filename;

// eslint-disable-next-line prefer-const
let { root, dir, base: basename, ext } = posix.parse(normalizedFilename);

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

const normalizedBase = `${base}/`;

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

if (ext === '.js') {
file = file.slice(0, file.length - '.js'.length);
}

if (!root && !dir) {
// No dirname whatsoever
dir = '.';
}

const path = dirname(normalizedFilename);
let n = path.lastIndexOf('/node_modules/');
let n = dir.lastIndexOf('/node_modules/');
if (n > -1) {
// /node_modules/ is 14 chars
return `${path.slice(n + 14).replace(/\//g, '.')}:${file}`;
return `${dir.slice(n + 14).replace(/\//g, '.')}:${file}`;
}
// Let's see if it's a part of the main module
// To be a part of main module, it has to share the same base
n = `${path}/`.lastIndexOf(base, 0);
n = `${dir}/`.lastIndexOf(normalizedBase, 0);

if (n === 0) {
let moduleName = path.slice(base.length).replace(/\//g, '.');
let moduleName = dir.slice(normalizedBase.length).replace(/\//g, '.');
if (moduleName) {
moduleName += ':';
}
Expand Down
2 changes: 1 addition & 1 deletion packages/node/test/module.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function withFilename(fn: () => void, filename: string) {
describe('getModule', () => {
test('Windows', () => {
withFilename(() => {
expect(getModule('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js')).toEqual('module');
expect(getModule('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js', true)).toEqual('module');
}, 'C:\\Users\\Tim\\app.js');
});

Expand Down