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

TypeError: Cannot read properties of undefined (reading 'kind') #725

Closed
silverwind opened this issue Jul 11, 2024 · 10 comments
Closed

TypeError: Cannot read properties of undefined (reading 'kind') #725

silverwind opened this issue Jul 11, 2024 · 10 comments
Labels
bug Something isn't working

Comments

@silverwind
Copy link

silverwind commented Jul 11, 2024

Running against a vite typescript project, I'm hit with this error. Maybe the stack trace will help identify the bug. Also the CJS vite error seems odd, the project is pure ESM.

$ npx knip@5.25.2
Analyzing workspace ....
The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprec
Analyzing source files...
node_modules/typescript/lib/typescript.js:30130
  return node.kind === 208 /* BindingElement */;
              ^

TypeError: Cannot read properties of undefined (reading 'kind')
    at Object.isBindingElement (node_modules/typescript/lib/typescript.js:30130:15)
    at file://node_modules/knip/dist/typescript/visitors/dynamic-imports/importCall.js:102:32
    at file://node_modules/knip/dist/typescript/visitors/index.js:4:35
    at visit (file://node_modules/knip/dist/typescript/getImportsAndExports.js:223:28)
    at visitNodes (node_modules/typescript/lib/typescript.js:31715:22)
    at forEachChildInArrayLiteralExpression (node_modules/typescript/lib/typescript.js:31861:12)
    at Object.forEachChild (node_modules/typescript/lib/typescript.js:32228:35)
    at visit (file://node_modules/knip/dist/typescript/getImportsAndExports.js:303:12)
    at visitNodes (node_modules/typescript/lib/typescript.js:31715:22)
    at forEachChildInCallOrNewExpression (node_modules/typescript/lib/typescript.js:32182:99)
Thrown at:
    at isBindingElement (node_modules/typescript/lib/typescript.js:30130:15)
    at file://node_modules/knip/dist/typescript/visitors/dynamic-imports/importCall.js:102:32
    at file://node_modules/knip/dist/typescript/visitors/index.js:4:35
    at visit (file://node_modules/knip/dist/typescript/getImportsAndExports.js:223:28)
    at visitNodes (node_modules/typescript/lib/typescript.js:31715:22)
    at forEachChildInArrayLiteralExpression (node_modules/typescript/lib/typescript.js:31861:12)
    at forEachChild (node_modules/typescript/lib/typescript.js:32228:35)
    at visit (file://node_modules/knip/dist/typescript/getImportsAndExports.js:303:12)
    at visitNodes (node_modules/typescript/lib/typescript.js:31715:22)
    at forEachChildInCallOrNewExpression (node_modules/typescript/lib/typescript.js:32182:99)


Node.js v22.4.0
@webpro
Copy link
Collaborator

webpro commented Jul 12, 2024

Maybe the stack trace will help identify the bug.

Unfortunately it does not, all I can say is that most likely the issue is related to an import call (import('specifier')). Please add more details or, even better, a reproduction (https://knip.dev/guides/issue-reproduction).

Also the CJS vite error seems odd, the project is pure ESM.

Please see https://knip.dev/reference/known-issues#the-cjs-build-of-vites-node-api-is-deprecated

@silverwind
Copy link
Author

silverwind commented Jul 12, 2024

I guess the only way to find the source of the problem will be to continously divide my code base down until only the offending line remains, but I'm not motivated to do that as this is a 50k LOC repo.

If something could be done to indicate the location of the offending line, it'd be appreciated.

@webpro
Copy link
Collaborator

webpro commented Jul 21, 2024

In this file:

node_modules/knip/dist/typescript/visitors/dynamic-imports/importCall.js:102:32

Add this line before the one that has ts.isBindingElement (line 102):

console.log(node.getSourceFile().fileName, specifier)

Then run knip -n, it should print the file name and the import specifier. That import() call is what we're interested in :)

@webpro
Copy link
Collaborator

webpro commented Jul 21, 2024

You can also try to e.g. npm i -D https://pkg.pr.new/knip@ae5c341 to see if that fixes your issue, but it does fall back to using the default export, if that specifier can be resolved.

@dentuzhik
Copy link

Encountered the same issue while tying out knip on our project.

I am getting it on the expression of the following type

  const [{foo}] = await Promise.all([
    import('./bar'),
    import('./baz'),
    import('@xyz'),
  ]);

It seems that in this case calculating the element based on the index goes wrong, because for on of them element becomes undefined. Hopefully this helps a bit

@webpro
Copy link
Collaborator

webpro commented Jul 22, 2024

Ah, that makes sense.

I'd guess e.g. npm i -D https://pkg.pr.new/knip@ae5c341 should work fine for you then. Any chance you could give this a shot?

@webpro
Copy link
Collaborator

webpro commented Jul 23, 2024

Should be fixed in v5.27.0, let me know if it isn't.

@webpro webpro closed this as completed Jul 23, 2024
@silverwind
Copy link
Author

Yes, npx knip@5.27.0 works for me as well now, thanks.

@fernando-redondo
Copy link

When using Promise.all to import multiple modules and then destructuring the result array with ignored values, the issue persists in version 5.27.0. Here is an example code snippet illustrating the problem:

const [ , , thirdImport] = await Promise.all([
    import('pathToFirstImport'),
    import('pathToSecondImport'),
    import('pathToThirdImport'),
]);

@webpro
Copy link
Collaborator

webpro commented Aug 7, 2024

🚀 This issue has been resolved in v5.27.1. See Release 5.27.1 for release notes.

Using Knip in a commercial project? Please consider becoming a sponsor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants