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
Show file tree
Hide file tree
Changes from 3 commits
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
15 changes: 15 additions & 0 deletions packages/node/test/module.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { getModule } from '../src/module';

describe('getModule', () => {
test('Windows', async () => {
timfish marked this conversation as resolved.
Show resolved Hide resolved
(require.main as any) = { filename: 'C:\\Users\\Tim\\app.js' };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we mock this return value with jest, and mockRestore afterwards?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jest.spyOn complains that require and require.main are "not declared configurable". I guess this means the properties are marked as configurable: false and there is no way to modify them other than by overwriting them?


expect(getModule('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js')).toEqual('module');
});

test('POSIX', async () => {
(require.main as any) = { filename: '/Users/Tim/app.js' };

expect(getModule('/Users/users/Tim/Desktop/node_modules/module.js')).toEqual('module');
});
});