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(opentelemetry-node): fix types to be on the ./sdk entry-point where they are to be used #444

Merged
merged 9 commits into from
Nov 27, 2024

Conversation

trentm
Copy link
Member

@trentm trentm commented Nov 22, 2024

For clarity (I hope) I've moved the lib/index.js to lib/sdk.js to
match the entry-point name. THis adds a small test case ensuring
a TypeScript 5 project is able to use @elastic/opentelemetry-node.


@david-luna This differs a bit from what we pair-programmed:

  1. I move index.js to sdk.js to be the entry-point, rather than moving elastic-node-sdk.js and dropping index.js.
  2. I found that the TS project (test/fixtures/a-ts-proj), needs to use TS 5 for the ".../sdk" entry-point to work. When we were looking at this, we had only verified that the types worked in VSCode. I believe VSCode is using its own internal TS 5. When running npx tsc on the CLI it failed. So I think this means we **want to set a min-supported TS version of v5, at least to use the ".../sdk" entry-point.
the failure with 'npx tsc'
[14:28:30 trentm@peach:~/tmp/asdf.20241122T111719]
% npx tsc
src/index.ts:1:23 - error TS2307: Cannot find module '@elastic/opentelemetry-node/sdk' or its corresponding type declarations.

1 import * as etel from '@elastic/opentelemetry-node/sdk';
                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tsconfig.json:27:15 - error TS6046: Argument for '--module' option must be: 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'esnext'.

27     "module": "node16",                                /* Specify what module code is generated. */
                 ~~~~~~~~


Found 2 errors.

Here is what the added test case looks like before the fix, i.e. showing that is fails as we expected:

test failure (before the fix was applied)
% node typescript-usage.test.js
TAP version 13
# typescript usage
not ok 1 "npm start" in "./fixtures/a-ts-proj" succeeded
  ---
    operator: error
    at: <anonymous> (/Users/trentm/el/elastic-otel-node5/packages/opentelemetry-node/test/typescript-usage.test.js:31:15)
    stack: |-
      Error: Command failed: npm start

          at ChildProcess.exithandler (node:child_process:422:12)
          at ChildProcess.emit (node:events:517:28)
          at maybeClose (node:internal/child_process:1098:16)
          at ChildProcess._handle.onexit (node:internal/child_process:303:5)
  ...
# $ npm start
# -- stdout --
#
# > [email protected] start
# > npm install && tsc && node index.js
#
#
# up to date, audited 740 packages in 729ms
#
# 98 packages are looking for funding
# run `npm fund` for details
#
# found 0 vulnerabilities
# index.ts(2,53): error TS2307: Cannot find module '@elastic/opentelemetry-node/sdk' or its corresponding type declarations.
#
# -- stderr --
#
# --

1..1
# tests 1
# pass  0
# fail  1

…re they are to be used

For clarity (I hope) I've moved the lib/index.js to lib/sdk.js to
match the entry-point name. THis adds a small test case ensuring
a TypeScript 5 project is able to use @elastic/opentelemetry-node.
@trentm trentm requested a review from david-luna November 22, 2024 23:24
@trentm trentm self-assigned this Nov 22, 2024
@david-luna
Copy link
Member

david-luna commented Nov 25, 2024

Hi @trentm

I found that the TS project (test/fixtures/a-ts-proj), needs to use TS 5 for the ".../sdk" entry-point to work.

This is due to the module compiler option feature. I did a bit of research and found that it was added in [email protected] release. Here are the notes https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#ecmascript-module-support-in-nodejs

User apps must have this feat to be able to resolve the exports from our package. We can say that min version required of TypeScript is ^4.7.2 (1st 4.7 released) which was release more that 2 years ago. IMHO its okay to also support below 5.x

npm view typescript time 
...
 '4.7.2': '2022-05-24T18:38:10.371Z',
...

Here's the diff after doing the checks, I've added @types/node for completeness although is getting the types from the node_modules at the root level

git diff
diff --git a/packages/opentelemetry-node/test/fixtures/a-ts-proj/package.json b/packages/opentelemetry-node/test/fixtures/a-ts-proj/package.json
index b939929..b755e65 100644
--- a/packages/opentelemetry-node/test/fixtures/a-ts-proj/package.json
+++ b/packages/opentelemetry-node/test/fixtures/a-ts-proj/package.json
@@ -8,7 +8,8 @@
   },
   "license": "Apache-2.0",
   "devDependencies": {
-    "typescript": "^5.0.2"
+    "@types/node": "^22.9.3",
+    "typescript": "^4.7.2"
   },
   "dependencies": {
     "@elastic/opentelemetry-node": "file:../../.."

Copy link
Member

@david-luna david-luna left a comment

Choose a reason for hiding this comment

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

The fix is good. Let's decide on the TS version later

@trentm trentm requested a review from david-luna November 25, 2024 19:07
"lint:license-files": "../../scripts/gen-notice.sh --lint . # requires node >=16",
"lint:changelog": "../../scripts/extract-release-notes.sh .",
"test": "NODE_OPTIONS='-r dotenv/config' DOTENV_CONFIG_PATH=./test/test-services.env tape test/**/*.test.js",
"test:without-test-services": "tape test/**/*.test.js",
"test-services:start": "docker compose -f ./test/docker-compose.yaml up -d --wait",
"test-services:stop": "docker compose -f ./test/docker-compose.yaml down",
"gen:types": "rm -rf types && tsc"
"gen:types": "rm -rf types && tsc # TODO: would be nice to tree-shake the dts files"
Copy link
Member

Choose a reason for hiding this comment

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

should we create an issue for that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nah. :) At least I won't bother. The odd TODO for low-priority and clear nice-to-haves in the code is fine by me. I don't object to one being created, however.

@trentm trentm merged commit c472673 into main Nov 27, 2024
12 checks passed
@trentm trentm deleted the trentm/fix-types-on-sdk-entry-point branch November 27, 2024 21:17
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.

2 participants