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

Add register.js for use with --import flag #4084

Closed
wants to merge 15 commits into from

Conversation

smartinio
Copy link
Contributor

@smartinio smartinio commented Feb 21, 2024

What does this PR do?

Add support for using node --import dd-trace/register.js

Note: If you want to support extensionless import (dd-trace/register), package.json needs to explicitly list it in exports. However, adding an exports key is a breaking change which I would not recommend doing at this stage.

Fixes #3794 (I think)

Motivation

--loader (aka --experimental-loader) is deprecated and using --import and register is the officially recommended way by Node.js

See (these anchors seem broken, but you'll figure out which heading it is):

Additional Notes

Tried this out locally by changing my local node_modules to mirror these changes

Security

Datadog employees:

  • If this PR touches code that signs or publishes builds or packages, or handles credentials of any kind, I've requested a review from @DataDog/security-design-and-guidance.
  • This PR doesn't touch any of that.

Unsure? Have a question? Request a review!

with --import flag
@smartinio smartinio requested a review from a team as a code owner February 21, 2024 13:37
Copy link
Member

@rochdev rochdev left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Can a test be added for this in integration-tests similar to the loader test?

@smartinio
Copy link
Contributor Author

Thanks for the PR! Can a test be added for this in integration-tests similar to the loader test?

@rochdev Cheers. Which test is the "loader test" you refer to?

image

@rochdev
Copy link
Member

rochdev commented Feb 21, 2024

Cheers. Which test is the "loader test" you refer to?

Looks like it was removed, but basically a file similar to startup.spec.js that ensures that the library can be used using --import dd-trace/register.

It also looks like some of the CI Visibility tests are using the old approach, but I don't think this is a problem in the short-term.

Qard
Qard previously approved these changes Feb 22, 2024
@smartinio
Copy link
Contributor Author

smartinio commented Feb 22, 2024

@rochdev I opted for wrapping startup.spec.js with the different flags. I'm on an M2 Pro mac though so I can't seem to run the test locally.

I also noticed that adding an exports entry makes Node.js start to enforce these...

I added the ones I saw used in the codebase, as well as a wildcard to allow any fully qualified deep import. The wildcard doesn't work without full file extension though in my own testing on Node 18, which could break for users who do undocumented deep imports.

...so I removed it to not risk making a breaking change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I recommend viewing this with whitespace hidden:
https://github.com/DataDog/dd-trace-js/pull/4084/files?diff=split&w=1

it('works for options.port', async () => {
proc = await spawnProc(startupTestFile, {
cwd,
execArgv,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alternatively, env.NODE_OPTIONS could be set below. I'm not sure which approach is preferred. execArgv seems more correct but both approaches seem to run the loader.

@smartinio smartinio changed the title Add "./register" export for use with --import flag Add register.js for use with --import flag Feb 23, 2024
Qard
Qard previously approved these changes Feb 26, 2024
integration-tests/startup.spec.js Outdated Show resolved Hide resolved
@Qard
Copy link
Collaborator

Qard commented Feb 27, 2024

Resubmitted as #4110 so it can run the full CI.

@Qard Qard closed this Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ESM Support without custom --loader
4 participants