coderaiser / putout

🐊 Pluggable and configurable JavaScript Linter, code transformer and formatter, drop-in ESLint superpower replacement πŸ’ͺ with built-in support for js, jsx, typescript, flow, markdown, yaml and json. Write declarative codemods in a simplest possible way 😏
https://putout.cloudcmd.io/
MIT License
712 stars 40 forks source link

🐊 name.startsWith is not a function #208

Closed blefnk closed 5 months ago

blefnk commented 5 months ago

Hello, guys! Putout looks incredible, I regret not hearing about it earlier! You've done a great job!

However, unfortunately, I've encountered a strange issue. When I run npx putout test.js or pnpm putout test.js, I get 🐊 name.startsWith is not a function. I've tried several of the latest versions, and this error appears starting from v31.0.0. Versions up to v30.7.0 give the error 🐊 Cannot find package 'putout-formatter-[object Object]'. Interestingly, version v35.35.4 does not produce any output, but test.js (the example from your README.md) still doesn't change.

I'm using Node.js LTS (v20.15.0), and I tested the previous LTS (v18.20.3) – the error is there as well. I think it might be an issue with pnpm, but the same happens with npm and bun (including pnpm dlx, npx, bunx).

I tested both with and without a Putout config (with the full code from the repo and with just processors with javascript). I tried both a clean pnpm create next-app and bun init. I tried running it in PowerShell, CMD, Git Bash. In the VSCode terminal and in external ones.

> envinfo --system --binaries --utilities
  System:
    OS: Windows 11 10.0.22631
    CPU: (16) x64 AMD Ryzen 7 6800H with Radeon Graphics
    Memory: 1.73 GB / 15.25 GB
  Binaries:
    Node: 20.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.7.0 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.1.4 - C:\Program Files\nodejs\pnpm.CMD
    bun: 1.1.12 - ~\.bun\bin\bun.EXE
  Utilities:
    Git: 2.43.0.

Please let me know if I can provide any additional information, I will do my best and would be happy to help in any way I can.

Thanks for this wonderful tool! It's really cool!

coderaiser commented 5 months ago

Hi, thank you!

I cannot reproduce this:

coderaiser@localcmd:~/Amber$ cat > 1.js
var a = 5;
^C
coderaiser@localcmd:~/Amber$ npx putout 1.js
1.js
 1:4  error   'a' is defined but never used  remove-unused-variables

βœ– 1 errors in 1 files
  fixable with the `--fix` option

but there is a couple places where bug can occur: https://github.com/search?q=repo%3Acoderaiser%2Fputout%20name.startsWith&type=code.

Could you please provide more details? Maybe you can add console.log to identify problem place?

blefnk commented 5 months ago

@coderaiser Thanks for your response! Yes, of course, I plan to do it on Tuesday. I also plan to try to build Putout on my own. I will also try to run it on my other laptop with Windows 11. I will get back to you once I manage to explore a bit more details. Thank you for the search link πŸ‘

coderaiser commented 5 months ago

Just fixed similar bug in 🐊Putout v35.35.5. Could you please tell me if it works for you?

blefnk commented 5 months ago
devDependencies:
- putout 35.35.4
+ putout 35.35.5
> pnpm putout test.js
🐊 name.startsWith is not a function
> pnpm dlx putout@latest test.js
🐊 name.startsWith is not a function
> npm run putout test.js
🐊 name.startsWith is not a function
> npx putout@latest test.js
🐊 name.startsWith is not a function

// without .putout.json
npx putout@latest test.js
🐊 No processors found for test.js
bun add -D putout@latest
installed putout@35.35.5 with binaries: putout
> bun putout index.js      
$ putout index.js
🐊 name.startsWith is not a function
error: script "putout" exited with code 9

bun without .putout.json

bun add -D putout@latest
installed putout@35.35.5 with binaries: putout
> bun putout index.js
$ putout index.js
node:internal/event_target:1094
  process.nextTick(() => { throw err; });
                           ^
Error: Cannot find module './url'
Require stack:
- C:\B\S\putout-minimal-repro\node_modules\source-map\lib\util.js
- C:\B\S\putout-minimal-repro\node_modules\source-map\lib\source-map-generator.js
- C:\B\S\putout-minimal-repro\node_modules\source-map\source-map.js
- C:\B\S\putout-minimal-repro\node_modules\recast\lib\util.js
- C:\B\S\putout-minimal-repro\node_modules\recast\lib\options.js
- C:\B\S\putout-minimal-repro\node_modules\recast\lib\parser.js
- C:\B\S\putout-minimal-repro\node_modules\recast\main.js
- C:\B\S\putout-minimal-repro\node_modules\@putout\engine-parser\lib\print.js
- C:\B\S\putout-minimal-repro\node_modules\@putout\engine-parser\lib\parser.js
- C:\B\S\putout-minimal-repro\node_modules\putout\lib\putout.js
- C:\B\S\putout-minimal-repro\node_modules\putout\lib\cli\process-file.js
- C:\B\S\putout-minimal-repro\node_modules\putout\lib\cli\runner\runner.js
- C:\B\S\putout-minimal-repro\node_modules\putout\lib\cli\index.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1145:15)
    at Function._load (node:internal/modules/cjs/loader:986:27)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (C:\B\S\putout-minimal-repro\node_modules\source-map\lib\util.js:8:13)
    at Module._compile (node:internal/modules/cjs/loader:1358:14)
    at Object..js (node:internal/modules/cjs/loader:1416:10)
    at Module.load (node:internal/modules/cjs/loader:1208:32)
    at Function._load (node:internal/modules/cjs/loader:1024:12)
    at Module.require (node:internal/modules/cjs/loader:1233:19)
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:326:10)
    at [kOnMessage] (node:internal/worker:337:37)
    at MessagePort.<anonymous> (node:internal/worker:232:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:820:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\source-map\\lib\\util.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\source-map\\lib\\source-map-generator.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\source-map\\source-map.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\recast\\lib\\util.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\recast\\lib\\options.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\recast\\lib\\parser.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\recast\\main.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\@putout\\engine-parser\\lib\\print.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\@putout\\engine-parser\\lib\\parser.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\putout\\lib\\putout.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\putout\\lib\\cli\\process-file.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\putout\\lib\\cli\\runner\\runner.js',
    'C:\\B\\S\\putout-minimal-repro\\node_modules\\putout\\lib\\cli\\index.js'
  ]
}
Node.js v20.15.0
error: script "putout" exited with code 1
coderaiser commented 5 months ago

Could you please try to pass β€”-raw flag when running 🐊Putout and check ~/.putout.json in your home directory, looks like issue with configuration file, but I’m not sure what exactly issue is.

blefnk commented 5 months ago

I couldn't find .putout.json in my C:\Users\blefn home folder, subfolders there doesn't has this file as well. I just tried to create such a file myself, but unfortunately, the result is the same, with and without --raw flag.

> pnpm putout test.js --raw || pnpm dlx putout test.js --raw || npx putout test.js --raw
🐊 name.startsWith is not a function
error: "putout.cmd" exited with code 9
coderaiser commented 5 months ago

Looks like picomatch works differently on win and unix. I don't know what the problem with name.startsWith but 🐊 No processors found for test.js fixed in v35.35.6 πŸŽ‰ and should work good. Is it works for you?

blefnk commented 5 months ago

Yeah, hooray! This almost resolved the issue! πŸŽ‰

Now putout works successfully as long as the putout dependency is not installed.

devDependencies:
- putout 35.35.6

However, if it is installed, it seems to "think" that it is part of the original monorepo and expects all its dependencies to be in the project, and since they are not, we get 🐊 No processors found for test.js (or also 🐊 name.startsWith is not a function).

So, theoretically, if all the necessary dependencies are installed, it should work. Maybe it’s necessary to add some peer dependencies to package.json binaries for putout? I mean, so that all the necessary dependencies of putout are installed together with putout. Or, is this already being done?

So currently, as a temporary solution, I added the following to scripts of package.json file: "putout": "pnpm dlx putout@latest", which makes pnpm putout test.js work successfully. This works both with and without a config.

upd. It turned out 🐊 name.startsWith is not a function is still present if .putout.json contains "formatter": [{ "minCount": 10 }, "progress-bar" ]. This happens even with pnpm dlx putout@latest.

It also turned out that when using pnpm dlx putout@latest, it is impossible to install custom processors. For example, when using "processors": ["javascript", "typescript"], I get 🐊 Cannot find package 'putout-processor-typescript', even though @putout/processor-typescript is installed. I have a theory that the config tries to refer to the putout library first, but it is not installed locally. Or maybe the problem is that it searches for the dependency "putout-processor-typescript" while the actual package is named @putout/processor-typescript? Hmm... πŸ€”

p.s. I also tried testing putout on Linux Mint, and to my great surprise, it turned out that 🐊 name.startsWith is not a function is not a Windows-specific issue; it is also present on Linux (and presumably on macOS, I guess).

upd. So strange, but it seems that everything works now when "formatter": [{ "minCount": 10 }, "progress-bar" ] is set. So this specific issue may be was solved.

coderaiser commented 5 months ago

Looks like name.startsWith is not a function is pnpm-specific issue. Try to use bun to install dependencies. Also you can create issue in pnpm repository.

coderaiser commented 5 months ago

I cannot reproduce this with pnpm:

coderaiser@localcmd:~/putout$ cat > 1.js
const a = 'hello';
^C
coderaiser@localcmd:~/putout$ pnpm dlx putout@latest 1.js
1.js
 1:6  error   'a' is defined but never used  remove-unused-variables

βœ– 1 errors in 1 files
  fixable with the `--fix` option

If you have any ideas how to reproduce - you can send PR with test, I'll merge so it work for your case.

blefnk commented 5 months ago

I have completed the investigation. It turned out that the latest issues were due to the fact that if there is a key-value pair in .putout.json that expects the presence of another key-value pair but does not find it, a problem occurs. That's why the formatter worked for me in the end, as I copy-pasted the entire putout.json from your monorepo. This issue also arises if there is incorrect sorting of key-value pairs. As a solution, a .js/.ts/.mjs config could be implemented in addition to json. So issue is not related to specific package manager or OS.

coderaiser commented 5 months ago

Could you please provide an example of broken config? Just copied putout.json from monorepo and still cannot reproduce. Maybe link to minimal repository can help.

coderaiser commented 5 months ago

I suggest you to always install 🐊Putout as devDependency, if you install it as global, and run it as global, additional processors (like @putout/processor-typescript) should be also installed globally. devDependency is convinient and recommended way to install and run 🐊Putout.