Skip to content

fix: do not instrument the top level module#225

Merged
timfish merged 4 commits intonodejs:mainfrom
BridgeAR:BridgeAR/2026-01-12-fix-174
Jan 14, 2026
Merged

fix: do not instrument the top level module#225
timfish merged 4 commits intonodejs:mainfrom
BridgeAR:BridgeAR/2026-01-12-fix-174

Conversation

@BridgeAR
Copy link
Member

@BridgeAR BridgeAR commented Jan 12, 2026

This makes sure ts-node works as expected, since it needs to verify if the top level is the main module. That also applies to other CLI tools.

This could be considered a breaking change, while I can not think of a good reason why anyone would ever want to instrument a top level file.

Fixes: #174
Fixes: #34

This works for Node.js 16+, while I am unsure how to fix this in older versions.

This makes sure ts-node works as expected, since it needs to verify
if the top level is the main module. That also applies to other
CLI tools.

Fixes: nodejs#174
Fixes: nodejs#34
@BridgeAR BridgeAR force-pushed the BridgeAR/2026-01-12-fix-174 branch from 9221346 to aaed0a1 Compare January 12, 2026 20:08
@BridgeAR BridgeAR marked this pull request as ready for review January 12, 2026 20:14
@timfish
Copy link
Contributor

timfish commented Jan 13, 2026

I can not think of a good reason why anyone would ever want to instrument a top level file.

I seem to remember that Open Telemetry AWS serverless hooks an absolute path to the entry module

@timfish
Copy link
Contributor

timfish commented Jan 13, 2026

Here is where the "entry" filename gets used for hooking aws-lambda:

https://github.com/open-telemetry/opentelemetry-js-contrib/blob/fa8d2e97526404df3c5ff96425d4f2267310785b/packages/instrumentation-aws-lambda/src/instrumentation.ts#L169

I'm unsure if this file is actually considered the entry module though!

@BridgeAR
Copy link
Member Author

I had a look at it and that filename is not the entrypoint, if I am not completely mistaken.
I can still not imagine any code to want to self wrap itself. There should just not be any reason for that and even a 'hook' for that would be very confusing to write.

timfish
timfish previously approved these changes Jan 13, 2026
Copy link
Contributor

@timfish timfish left a comment

Choose a reason for hiding this comment

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

Yep, good point. This shouldn't ever be required.

- name: Verify Minimum Coverage Is Met
run: >
lcov --summary all_lcov.info | grep lines | cut -d' ' -f 4 | cut -d% -f 1 | xargs node -e "x=process.argv[1];console.log(x);assert(+x >= 90)"
lcov --summary all_lcov.info | grep lines | cut -d' ' -f 4 | cut -d% -f 1 | xargs node -e "x=process.argv[1];console.log(x);assert(+x >= 80)"
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a significant drop in accepted coverage. Are we unable to cover the things in this PR?

Copy link
Member Author

Choose a reason for hiding this comment

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

I am uncertain why the job failed the second time, I did not check the coverage locally. I believe the third argument changes are not reflected in the coverage. I would bump that afterwards again

@timfish timfish merged commit b563b35 into nodejs:main Jan 14, 2026
51 checks passed
@BridgeAR
Copy link
Member Author

Interestingly, I broke our Datadog hook tests by including this. We (Datadog) do something incorrect in our hook when using the --experimental-loader flag and the fix here still seems good as is.

The question for me is just if we should revert it on v2, release v3 and include it there, since the change needs adjustments on our side.

@timfish @jsumners-nr optinions?

@timfish
Copy link
Contributor

timfish commented Jan 24, 2026

revert it on v2, release v3 and include it there

Sounds like a good plan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ts-node does not work tests not running when using import-in-the-middle with mocha

3 participants