-
Notifications
You must be signed in to change notification settings - Fork 833
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(instrumentation): do not export Node.js types for Browser users #4378
fix(instrumentation): do not export Node.js types for Browser users #4378
Conversation
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #4378 +/- ##
==========================================
+ Coverage 92.23% 92.32% +0.08%
==========================================
Files 332 330 -2
Lines 9467 9454 -13
Branches 2011 2010 -1
==========================================
- Hits 8732 8728 -4
+ Misses 735 726 -9 |
I changed the test file back to what it was before to get the tests to run. As I felt like I'm just hiding a problem by doing that, I double checked if it actually works in a new ESM project with Node.js v20, and it does not.
But still, looking at the transpiled files, I'm exporting them (and it's working with older vesions). @dyladan since you worked on #4336 - do you know the reason why this wouldn't work on Node.js 20 but works fine on older versions? |
Ah this seems to be related to the loader changes that made it into Node 18.19.0. So that's why the test also failed for Node 18 (https://github.com/open-telemetry/opentelemetry-js/actions/runs/7248698310/job/19745127352#step:8:4690). When I create an ESM app and import it there, it works with Node.js 18.18.2. Maybe it has something to do with the loader or how it is set up? 🤔 If I don't use it, it works fine. (nothing is instrumented, obviously - but the import works) |
Ah, I see - this may be due to nodejs/import-in-the-middle#29 So it looks like we're in kind of a tough spot at the moment:
An alternative may be #3989 (switching to functional ESM output likely means we can circumvent the ITTM issue, but I've not tested it yet - I'll test it if the IITM fix does not do what I think it does) |
I doubt. Switching to ESM would break all CJS users as CJS can't use ESM. |
Fair point. I did not word that correctly: I meant to say double publishing CJS and ESM (not switching to) so that ESM users may use an ESM version of the
True. Right now, I think the way to go would be to fix browser users. From what I can tell, even with an ESM version double-published, using IITM right now would break when a
which may be more than a handful, not just I've actually tried the changes from nodejs/import-in-the-middle#30 in the meantime, that'd fix the issue of handling re-exports. |
export { InstrumentationNodeModuleFile } from './instrumentationNodeModuleFile'; | ||
export { InstrumentationNodeModuleDefinition } from './instrumentationNodeModuleDefinition'; |
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.
The platform folder should not have any exports for 1 platform but not another. All exports should be used exactly identically. The same code that uses the node
platform exports will also use the browser
platform exports.
// Exports for Node.js and Browser are different, therefore exporting * | ||
export * from './platform/index'; |
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.
This is not the way the platform-specific exports work and will cause things to break
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.
Edit: some background info first: the problem is that we use path.normalize
in InstrumentationNodeModuleDefinition
, which does not exist in the browser.
@dyladan I see. We actually rely on this (different exports for different platforms) quite a bit in other packages in this repo (exporter packages, mostly), so we should likely also get on top of this there. It is also (in spirit) the way we had it before it was before we merged #4336.
From what I can tell there are only three other ways for us to fix this then:
- provide and maintain our own
normalize
implementation - rely on a third-party package that provides an agnostic implementation
- normalize in Node.js while using a no-op in the browser (this does not seem like a safe option)
WDYT?
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.
Before #4336 we actually had the bad situation I'm trying to avoid and I had to fix it to get that PR to pass.
normalize in Node.js while using a no-op in the browser (this does not seem like a safe option)
Why do you think this is unsafe? There is no valid reason to use a NodeModuleFile in browser anyway and it would be completely broken in other ways. The only thing we need to fix is that we shouldn't import path
in browser contexts.
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.
Looking closer into this, I agree that it's likely fine if we do that (no-op in browser). Closing this PR, will re-open another one with that approach 👍
For reference, new PR is #4386 |
Which problem is this PR solving?
InstrumentationNodeModuleFile
andInstrumentationNodeModuleDefinition
were exported even if the@opentelemetry/instrumentation
package is used in a browser context. This PR changes it so that the Node type are not exported for Browser users anymore.Fixes #4373
Type of change
How Has This Been Tested?
webpack
and including the setup provided in [Instrumentation] Cannot build web project using webpack due to reference to nodejs core package (path) #4373. I installed the@opentelemetry/instrumentation
dependency as a packaged file that includes the changes from this PR.