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

Running against .ts files fails with error: 'TypeError: p is not iterable' #688

Closed
danielreidwoebot opened this issue Dec 14, 2021 · 9 comments

Comments

@danielreidwoebot
Copy link

Bug Description

I'm trying to run depcheck against TypeScript code but it's failing with the error: TypeError: p is not iterable. I've tried installing with npm i -g depcheck and also npm i -g depcheck typescript.

The error was generated after running:

depcheck path/to/my/repo --json

I have tried:

depcheck path/to/my/repo --parsers="*.ts" --json 

However, when I run this (^) command I get the error: Cannot read property 'split' of undefined. It does seem to be working as expected for .js files though.

Here's the stack trace for the error in the issue title.

"TypeError: p is not iterable\n    at /Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:10658:43\n    at Array.some (<anonymous>)\n    at hasPlugin (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:10654:18)\n    at validatePlugins (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:10693:7)\n    at getParser (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:15283:5)\n    at parse (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:15254:12)\n    at parseTypescript (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/dist/parser/typescript.js:17:28)\n    at async getDependencies (/Users/danielreid/.nvm/versions/node/v14.18.2/lib/node_modules/depcheck/dist/check.js:71:18)"

Versions

  • node -v: v14.18.2
  • npm -v: 6.14.15
  • depcheck --version: 1.4.2
@rumpl
Copy link
Member

rumpl commented Dec 14, 2021

Can you try with depcheck path/to/my/repo --parsers="**/*.ts:ts" --json

@danielreidwoebot
Copy link
Author

depcheck path/to/my/repo --parsers="**/*.ts:ts" --json

@rumpl thanks, yeah I can see now I misunderstood the syntax for the argument. I definitely get better results when running that, but I'm still seeing errors:

"TypeError: parser is not a function\n    at getDependencies (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/dist/check.js:71:24)\n    at /Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/dist/check.js:97:32\n    at Array.map (<anonymous>)\n    at checkFile (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/dist/check.js:97:18)\n    at ReaddirpStream.<anonymous> (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/dist/check.js:126:24)\n    at ReaddirpStream.emit (events.js:314:20)\n    at addChunk (_stream_readable.js:297:12)\n    at readableAddChunk (_stream_readable.js:272:9)\n    at ReaddirpStream.Readable.push (_stream_readable.js:213:10)\n    at ReaddirpStream._read (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/readdirp/index.js:143:22)",
        "/Users/danielreid/Projects/woebot-lambdas/src/utils/npm-licenses.js": "SyntaxError: Unexpected reserved word 'package'. (129:14)\n    at Parser._raise (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:569:17)\n    at Parser.raiseWithData (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:562:17)\n    at Parser.raise (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:523:17)\n    at Parser.checkReservedWord (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:13047:12)\n    at Parser.parseIdentifierName (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:13001:12)\n    at Parser.parseIdentifier (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:12971:23)\n    at Parser.parseBindingAtom (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:11033:17)\n    at Parser.parseVarId (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:14208:20)\n    at Parser.parseVar (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:14184:12)\n    at Parser.parseVarStatement (/Users/danielreid/.nvm/versions/node/v12.22.7/lib/node_modules/depcheck/node_modules/@babel/parser/lib/index.js:14001:10)",

@hedgepigdaniel
Copy link

I think the problem is that this is not a valid plugin configuration:

{ pipelineOperator: { proposal: 'minimal' } },

@danielreidwoebot
Copy link
Author

@hedgepigdaniel thanks, i did try your fix but I get the same error.

@danielreidwoebot
Copy link
Author

danielreidwoebot commented Dec 15, 2021

@hedgepigdaniel actually, I noticed a tread outlining an issue with running with npx so I ran npm link to link your fix to the globally installed package and it seems to have worked.

I'm still seeing the error TypeError: parser is not a function but now it's only thrown for .json files:

TypeError: parser is not a function\n    
    at getDependencies (/Users/danielreid/Projects/depcheck/dist/check.js:71:24)\n    
    at /Users/danielreid/Projects/depcheck/dist/check.js:97:32\n    
    at Array.map (<anonymous>)\n    
    at checkFile (/Users/danielreid/Projects/depcheck/dist/check.js:97:18)\n    
    at ReaddirpStream.<anonymous> (/Users/danielreid/Projects/depcheck/dist/check.js:126:24)\n    
    at ReaddirpStream.emit (events.js:314:20)\n    
    at addChunk (_stream_readable.js:297:12)\n    
    at readableAddChunk (_stream_readable.js:272:9)\n    
    at ReaddirpStream.Readable.push (_stream_readable.js:213:10)\n    
    at ReaddirpStream._read (/Users/danielreid/Projects/depcheck/node_modules/readdirp/index.js:140:22)

@slavoroi
Copy link

slavoroi commented Dec 21, 2021

I can confirm, I get the same error messages.
Using node 16.13.0, npm 6 and depcheck 1.4.0, 1.4.1 and 1.4.2.

It's a babel-parser issue.
You didn't lock the babel-parser version in your dependencies and it broke your library.

@babel/parser@7.16.6

return plugins.some(p => {
    if (typeof p === "string") {
      return expectedOptionsIsEmpty && p === expectedName;
    } else {
      const [pluginName, pluginOptions] = p;

      if (pluginName !== expectedName) {
        return false;
      }

@capsuleman
Copy link

I can confirm, I get the same error messages. Using node 16.13.0, npm 6 and depcheck 1.4.0, 1.4.1 and 1.4.2.

It's a babel-parser issue. You didn't lock the babel-parser version in your dependencies and it broke your library.

@babel/parser@7.16.6

return plugins.some(p => {
    if (typeof p === "string") {
      return expectedOptionsIsEmpty && p === expectedName;
    } else {
      const [pluginName, pluginOptions] = p;

      if (pluginName !== expectedName) {
        return false;
      }

I can confirm, I modified my yarn.loock file to change the @babel/parser version to 7.16.5 and the errors have diseapeared. Thanks!

@Chocobozzz
Copy link

7.16.5 does not seem to work on my side. Had to install 7.16.4. package.json config for people that use yarn:

  "resolutions": {
    "depcheck/@babel/parser": "7.16.4"
  },

and then run yarn install to update yarn.lock

antongolub added a commit to antongolub/tsc-esm-fix that referenced this issue Dec 30, 2021
antongolub added a commit to antongolub/tsc-esm-fix that referenced this issue Dec 30, 2021
gribnoysup added a commit to mongodb-js/compass that referenced this issue Feb 21, 2022
Installing latest version of babel core in eslint config had a side-effect of
deduping and hoisting this version for every package in the monorepo, which in
turn broke depcheck package because of depcheck/depcheck#688

As the issue is not resolved yet on the depcheck side, our best way of handling this is
to make sure that we are not using this version of babel yet, but as soon
as fix is released, we should do a depcheck update in the monorepo
gribnoysup added a commit to mongodb-js/compass that referenced this issue Feb 22, 2022
…rser configs (#2815)

* fix(eslint-config-compass): Fix babel version at 7.14

Installing latest version of babel core in eslint config had a side-effect of
deduping and hoisting this version for every package in the monorepo, which in
turn broke depcheck package because of depcheck/depcheck#688

As the issue is not resolved yet on the depcheck side, our best way of handling this is
to make sure that we are not using this version of babel yet, but as soon
as fix is released, we should do a depcheck update in the monorepo

* fix(eslint-config-compass): Separate js and ts parser configs

Having babel as default and overriding it with typescript leads to
weird parsing issues when running lint for packages in the monorepo

* test(compass-aggregations): Skip tests that fail in evergreen
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Mar 3, 2022
Two fixes are necessary to the depcheck configuration so that it does
not report errors around TypeScript files:

* Don't report anything around `@types/*` packages, as those are
  implicitly imported by TypeScript
* Pin the version of `@babel/parser` that depcheck uses as the one it's
  currently using breaks the TypeScript parsing code, causing depcheck
  to complain about anything imported in TypeScript files.
  Related GH issue: <depcheck/depcheck#688>
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Mar 3, 2022
Two fixes are necessary to the depcheck configuration so that it does
not report errors around TypeScript files:

* Don't report anything around `@types/*` packages, as those are
  implicitly imported by TypeScript
* Pin the version of `@babel/parser` that depcheck uses as the one it's
  currently using breaks the TypeScript parsing code, causing depcheck
  to complain about anything imported in TypeScript files.
  Related GH issue: <depcheck/depcheck#688>
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Mar 3, 2022
Two fixes are necessary to the depcheck configuration so that it does
not report errors around TypeScript files:

* Don't report anything around `@types/*` packages, as those are
  implicitly imported by TypeScript
* Pin the version of `@babel/parser` that depcheck uses as the one it's
  currently using breaks the TypeScript parsing code, causing depcheck
  to complain about anything imported in TypeScript files.
  Related GH issue: <depcheck/depcheck#688>
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Mar 3, 2022
Two fixes are necessary to the depcheck configuration so that it does
not report errors around TypeScript files:

* Don't report anything around `@types/*` packages, as those are
  implicitly imported by TypeScript
* Pin the version of `@babel/parser` that depcheck uses as the one it's
  currently using breaks the TypeScript parsing code, causing depcheck
  to complain about anything imported in TypeScript files.
  Related GH issue: <depcheck/depcheck#688>
@stale
Copy link

stale bot commented Apr 27, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Apr 27, 2022
@stale stale bot closed this as completed Jul 31, 2022
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Aug 23, 2022
When support for TypeScript was added, I noticed that `depcheck` was
failing to properly detect dependencies in TypeScript files (running
`DEBUG=depcheck:* yarn depcheck` revealed that `depcheck` produced a "p
is not iterable" error whenever encountering these files). According to
[this GitHub issue], this error seems to be caused by some change in
`@babel/parser`. The solution suggested there was to pin `@babel/parser`
to 7.16.4, so I added a resolution to `package.json` to achieve this.

However, since then we've discovered that `yarn-deduplicate` will undo
changes to `yarn.lock` that `yarn install` makes, meaning any developer
will need to run `yarn-deduplicate` if they want to update dependencies
locally or else CI will fail. The aforementioned resolution for
`@babel/parser` seems to be the culprit. `@babel/parser` is used by a
lot of packages, but the resolution only targeted the version that
`depcheck` used. This meant that multiple versions of `@babel/parser`
were valid depending on the version resolution logic being used, leading
to indeterminate behavior.

To fix this problem, this commit updates the resolution such it is more
global, i.e., any time `@babel/parser` is referenced, only *one* version
will be used. This does lead to a warning when `yarn install` is run, so
maybe there is a better way to do this, but the alternative is to add
unnecessary exceptions to `.depcheckrc.yml` which seemed undesirable.

[1]: depcheck/depcheck#688 (comment)
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Aug 23, 2022
When support for TypeScript was added, I noticed that `depcheck` was
failing to properly detect dependencies in TypeScript files (running
`DEBUG=depcheck:* yarn depcheck` revealed that `depcheck` produced a "p
is not iterable" error whenever encountering these files). According to
[this GitHub issue][1], this error seems to be caused by some change in
`@babel/parser`. The solution suggested there was to pin `@babel/parser`
to 7.16.4, so I added a resolution to `package.json` to achieve this.

However, since then we've discovered that `yarn-deduplicate` will undo
changes to `yarn.lock` that `yarn install` makes, meaning any developer
will need to run `yarn-deduplicate` if they want to update dependencies
locally or else CI will fail. The aforementioned resolution for
`@babel/parser` seems to be the culprit. `@babel/parser` is used by a
lot of packages, but the resolution only targeted the version that
`depcheck` used. This meant that multiple versions of `@babel/parser`
were valid depending on the version resolution logic being used, leading
to indeterminate behavior.

To fix this problem, this commit updates the resolution such it is more
global, i.e., any time `@babel/parser` is referenced, only *one* version
will be used. This does lead to a warning when `yarn install` is run, so
maybe there is a better way to do this, but the alternative is to add
unnecessary exceptions to `.depcheckrc.yml` which seemed undesirable.

[1]: depcheck/depcheck#688 (comment)
mcmire added a commit to MetaMask/metamask-extension that referenced this issue Aug 23, 2022
When support for TypeScript was added, I noticed that `depcheck` was
failing to properly detect dependencies in TypeScript files (running
`DEBUG=depcheck:* yarn depcheck` revealed that `depcheck` produced a "p
is not iterable" error whenever encountering these files). According to
[this GitHub issue][1], this error seems to be caused by some change in
`@babel/parser`. The solution suggested there was to pin `@babel/parser`
to 7.16.4, so I added a resolution to `package.json` to achieve this.

However, since then we've discovered that `yarn-deduplicate` will undo
changes to `yarn.lock` that `yarn install` makes, meaning any developer
will need to run `yarn-deduplicate` if they want to update dependencies
locally or else CI will fail. The aforementioned resolution for
`@babel/parser` seems to be the culprit. `@babel/parser` is used by a
lot of packages, but the resolution only targeted the version that
`depcheck` used. This meant that multiple versions of `@babel/parser`
were valid depending on the version resolution logic being used, leading
to indeterminate behavior.

To fix this problem, this commit updates the resolution such it is more
global, i.e., any time `@babel/parser` is referenced, only *one* version
will be used. This does lead to a warning when `yarn install` is run, so
maybe there is a better way to do this, but the alternative is to add
unnecessary exceptions to `.depcheckrc.yml` which seemed undesirable.

[1]: depcheck/depcheck#688 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants