-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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 resolve module name #10001
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,40 @@ | ||
import { getModuleFromFilename } from '../src/module'; | ||
|
||
function withFilename(fn: () => void, filename: string) { | ||
const prevFilename = require.main?.filename; | ||
if (require.main?.filename) { | ||
require.main.filename = filename; | ||
} | ||
|
||
try { | ||
fn(); | ||
} finally { | ||
if (require.main && prevFilename) { | ||
require.main.filename = prevFilename; | ||
} | ||
} | ||
} | ||
|
||
describe('getModuleFromFilename', () => { | ||
test('Windows', () => { | ||
withFilename(() => { | ||
expect(getModuleFromFilename('C:\\Users\\users\\Tim\\Desktop\\node_modules\\module.js', true)).toEqual('module'); | ||
}, 'C:\\Users\\Tim\\app.js'); | ||
expect( | ||
getModuleFromFilename('C:\\Users\\Tim\\node_modules\\some-dep\\module.js', 'C:\\Users\\Tim\\', true), | ||
).toEqual('some-dep:module'); | ||
|
||
expect(getModuleFromFilename('C:\\Users\\Tim\\some\\more\\feature.js', 'C:\\Users\\Tim\\', true)).toEqual( | ||
'some.more:feature', | ||
); | ||
}); | ||
|
||
test('POSIX', () => { | ||
withFilename(() => { | ||
expect(getModuleFromFilename('/Users/users/Tim/Desktop/node_modules/module.js')).toEqual('module'); | ||
}, '/Users/Tim/app.js'); | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.js', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
|
||
expect(getModuleFromFilename('/Users/Tim/some/more/feature.js', '/Users/Tim/')).toEqual('some.more:feature'); | ||
expect(getModuleFromFilename('/Users/Tim/main.js', '/Users/Tim/')).toEqual('main'); | ||
}); | ||
|
||
test('.mjs', () => { | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.mjs', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
}); | ||
|
||
test('POSIX .mjs', () => { | ||
withFilename(() => { | ||
expect(getModuleFromFilename('/Users/users/Tim/Desktop/node_modules/module.mjs')).toEqual('module'); | ||
}, '/Users/Tim/app.js'); | ||
test('.cjs', () => { | ||
expect(getModuleFromFilename('/Users/Tim/node_modules/some-dep/module.cjs', '/Users/Tim/')).toEqual( | ||
'some-dep:module', | ||
); | ||
}); | ||
|
||
test('POSIX .cjs', () => { | ||
withFilename(() => { | ||
expect(getModuleFromFilename('/Users/users/Tim/Desktop/node_modules/module.cjs')).toEqual('module'); | ||
}, '/Users/Tim/app.js'); | ||
test('node internal', () => { | ||
expect(getModuleFromFilename('node.js', '/Users/Tim/')).toEqual('node'); | ||
expect(getModuleFromFilename('node:internal/process/task_queues', '/Users/Tim/')).toEqual('task_queues'); | ||
expect(getModuleFromFilename('node:internal/timers', '/Users/Tim/')).toEqual('timers'); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
l: This is not really related to this PR, but it would be nice to have a comment explaining what & why we actually do here - e.g. why use
require.main.filename
overglobal.process.cwd()
etc. Feel free to ignore this, but as somebody with little to no historical context on this, this appears very magic to me 😅There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have no idea other than this has been like this for 11+ years from
raven-node
:https://github.com/getsentry/raven-node/blame/7019cb30efda40704105b7b510bc428ec0c1ee01/lib/utils.js#L87
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a good point though which is worth considering as it will be important for #9072.
I guess the issue with
process.cwd()
is that it might give us the repository root rather than the runtime root. For example, the app might be run fromprocess.cwd() + '/packages/my-app/dist/'
so if we use cwd, paths (and therefore modules, will always startpackages.my-app.dist.*
.dirname(require.main.filename)
will only give us an accurate runtime root if the entry point is in the root. I suspect this is true the majority of the time.In the Deno SDK, due to permissions we might not have have access to
cwd()
or the file system. To work around this, we parse anew Error().stack
frominit
and pick the common root path from the stack trace.sentry-javascript/packages/deno/src/integrations/normalizepaths.ts
Lines 7 to 40 in ff95fd5