prettier / prettier-vscode

Visual Studio Code extension for Prettier
https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
MIT License
5.04k stars 446 forks source link

TypeError: t.match is not a function #1136

Closed TranquilMarmot closed 4 years ago

TranquilMarmot commented 4 years ago

Summary

When running Prettier in VSCode, the following exception is thrown:

["INFO" - 1:43:42 PM] Loaded module 'prettier@1.17.0' from 'C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier'
["INFO" - 1:43:42 PM] Using ignore file (if present) at d:\Documents\Projects\moorecodes\.prettierignore
["INFO" - 1:43:42 PM] File Info:
{
  "ignored": false,
  "inferredParser": "typescript"
}
["INFO" - 1:43:42 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 1:43:42 PM] Prettier Options:
{
  "arrowParens": "avoid",
  "bracketSpacing": true,
  "endOfLine": "auto",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 80,
  "proseWrap": "preserve",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "none",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "d:\\Documents\\Projects\\moorecodes\\src\\SomeProject\\Header\\index.tsx",
  "parser": "typescript"
}
["INFO" - 1:43:42 PM] Loaded module 'prettier@1.17.0' from 'C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier'
["INFO" - 1:43:42 PM] Formatting using 'prettier-tslint'
["ERROR" - 1:43:42 PM] Error formatting document.
t.match is not a function
TypeError: t.match is not a function
    at Object.e.extract (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\parser-babylon.js:1:1119)
    at Object.h [as hasPragma] (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\parser-babylon.js:1:2691)
    at format (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:14030:63)
    at formatWithCursor (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:14117:12)
    at C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:42518:15
    at format (C:\Users\nate2\AppData\Roaming\npm\node_modules\prettier\index.js:42537:12)
    at c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:352591
    at t.default.safeExecution (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:353582)
    at t.default.<anonymous> (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:352554)
    at Generator.next (<anonymous>)
    at s (c:\Users\nate2\.vscode\extensions\esbenp.prettier-vscode-3.13.0\dist\extension.js:1:347571)
["INFO" - 1:43:42 PM] Formatting completed in 17.5141ms.

This happens with both auto-format on save and when running the format command.

VS Code Version:

Version: 1.41.0 (system setup)
Commit: 9579eda04fdb3a9bba2750f15193e5fafe16b959
Date: 2019-12-11T18:37:42.077Z
Electron: 6.1.5
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Windows_NT x64 10.0.18362

Prettier Extension Version:

Name: Prettier - Code formatter
Id: esbenp.prettier-vscode
Description: Code formatter using prettier
Version: 3.13.0
Publisher: Esben Petersen
VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

OS and version:

Windows 10

kachkaev commented 4 years ago

Getting the same all the time on macos since recenly, could be related to bumping VSCode to 1.41.0

escalonc commented 4 years ago

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

travislang commented 4 years ago

I am getting a similar error when I try to format a file.

["INFO" - 7:05:17 PM] Loaded module 'prettier@1.19.1' from '/usr/local/lib/node_modules/prettier'
["INFO" - 7:05:17 PM] Formatting using 'prettier-eslint'
["ERROR" - 7:05:17 PM] Error formatting document.
r is not a function
TypeError: r is not a function
    at /Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352891
    at t.default.safeExecution (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:353582)
    at t.default.<anonymous> (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352865)
    at Generator.next (<anonymous>)
    at s (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:347571)
["INFO" - 7:05:17 PM] Formatting completed in 48.240989ms.

The last message says "formatting complete" but nothing gets formatted. I just recently noticed this problem as well, up until tonight everything had been working perfectly.

I ran the minified code through source map and this is what I am getting back:

// 1:352891
Maps to ../src/PrettierEditService.ts:352:13 (languageResolver)

    if (this.languageResolver.doesParserSupportStylelint(parser)) {

// 1:353582
Maps to ../src/PrettierEditService.ts:406:44 (FormattingResult)

      this.statusBarService.updateStatusBar(FormattingResult.Success);
travislang commented 4 years ago

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn't have to downgrade node all the way to 10.17.0

escalonc commented 4 years ago

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn't have to downgrade node all the way to 10.17.0

I just provide an alternative workaround or a point of reference to track better the issue, In the meantime, if you have a better solution I will love to know it.

travislang commented 4 years ago

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn't have to downgrade node all the way to 10.17.0

I just provide an alternative workaround or a point of reference to track better the issue, In the meantime, if you have a better solution I will love to know it.

I don't have a better solution, I haven't had time to dig into the source to try to find the issue in the minified code.

I think downgrading node that far will break more dependencies than is worth it is all

travislang commented 4 years ago

Okay I looked into the source code and I am pretty sure the error is coming from the most recent commit 3b2030104a4719779e37e5d8ac000cf46aa6a9b8 here:

https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/ModuleResolver.ts#L139-L155

I dont think the new ModuleResolver is returning a moduleInstance now and when it gets called here it throws the TypeError: r is not a function https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/PrettierEditService.ts#L340-L348

bushblade commented 4 years ago

I'm also getting this issue today.

["INFO" - 11:36:30] Formatting using 'prettier-eslint'
["ERROR" - 11:36:30] Error formatting document.
r is not a function
TypeError: r is not a function

For now I'm just using this instead

mikepulaski commented 4 years ago

I have changed my current node version to 10.17.0 and the prettier vscode extension works!.

I shouldn't have to downgrade node all the way to 10.17.0

I just provide an alternative workaround or a point of reference to track better the issue, In the meantime, if you have a better solution I will love to know it.

You can downgrade to the previous version (3.12.0) from VSCode which will revert the breaking changes introduced in 3.13.0.

image

You have to reload the window (or restart VSCode) for the changes to take effect.

wisethee commented 4 years ago

Install prettier in your project: npm install -D prettier

Then will work.

ntotten commented 4 years ago

Could somebody provide a project that reproduces this. I am seeing that it looks like it may be impacting tslint prettier integration but also stylelint prettier.

@travislang It could be related, but it should be fine if moduleInstance, in that case, is undefined. It first tries local, then global, then falls back to the bundled prettier. Its certainly possible though that causes the error.

kachkaev commented 4 years ago

@ntotten πŸ‘‹

You can try https://github.com/kachkaev/njt 🐸

I've been working on this project for a couple of weekends and prettier-vscode started crashing in it yesterday after some point. The project uses yarn.

ntotten commented 4 years ago

@kachkaev This seems to be working for me. Are you on windows? Could be a windows only bug. I'm not at my windows computer now but i can try to check later.

kachkaev commented 4 years ago

@ntotten I'm on macOS Catalina 10.15.1 πŸ€”

kachkaev commented 4 years ago

For me, prettier-vscode works in vscode@v1.41.0 after I've downgraded the extension to v3.12.0. What you might also find interesting is that kachkaev/njt reproduces #1066 too. Try opening package.json, adding a space and saving. This message pops out in the bottom-right corner:

Failed to load module. If you have prettier or plugins referenced in package.json,
ensure you have run `npm install` Attempted to load prettier from package.json

This can be because I'm using prettier-plugin-packagejson.

ntotten commented 4 years ago

@kachkaev Can you paste your prettier output log.

The Failed to load module error should only happen when prettier is found in the package.json but you haven't run npm install or yarn, but I am taking it that you have does those and the module is there, but still it isn't finding it.

travislang commented 4 years ago

@ntotten I am on MacOS 10.14.6. Here are my logs:

["INFO" - 10:53:24 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 10:53:24 PM] Prettier Options:
{
  "filepath": "/Users/tjlang/projects/ledger-app/ledger/sagas/auth.js",
  "parser": "babel",
  "semi": false,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 100,
  "tabWidth": 4
}
["INFO" - 10:53:24 PM] Loaded module 'prettier@1.19.1' from '/usr/local/lib/node_modules/prettier'
["INFO" - 10:53:24 PM] Formatting using 'prettier-eslint'
["ERROR" - 10:53:24 PM] Error formatting document.
r is not a function
TypeError: r is not a function
    at /Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352891
    at t.default.safeExecution (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:353582)
    at t.default.<anonymous> (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352865)
    at Generator.next (<anonymous>)
    at s (/Users/tjlang/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:347571)
["INFO" - 10:53:24 PM] Formatting completed in 53.787154ms.

The strange thing is source map is mapping the r is not a function to this

Maps to ../src/PrettierEditService.ts:352:13 (languageResolver)

    if (this.languageResolver.doesParserSupportStylelint(parser)) {

But clearly from looking at the log Formatting using 'prettier-eslint' it isn't running the stylelint prettier so I don't really know....

kachkaev commented 4 years ago

I have similar output. Steps:

  1. Clone https://github.com/kachkaev/njt
  2. Run yarn install
  3. Open vscode with a single project dir (vscode@1.41.0, prettier-vscode@3.13.0)
  4. Open next.config.js
  5. Add a new line to the end of the new file, press save. New line is not removed.
Output
["INFO" - 17:34:32] Extension Name: "prettier-vscode".
["INFO" - 17:34:32] Extension Version: "3.13.0".
["INFO" - 17:34:32] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:34:32] Enabling prettier for languages
[
  "css",
  "graphql",
  "html",
  "javascript",
  "javascriptreact",
  "json",
  "json5",
  "jsonc",
  "less",
  "markdown",
  "mdx",
  "mongo",
  "postcss",
  "scss",
  "typescript",
  "typescriptreact",
  "vue",
  "yaml"
]
["INFO" - 17:34:32] Enabling prettier for range supported languages
[
  "graphql",
  "javascript",
  "javascriptreact",
  "json",
  "typescript",
  "typescriptreact"
]
["INFO" - 17:34:35] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:34:39] Formatting /Users/ak/-/projects/kachkaev/oss/njt/next.config.js
["INFO" - 17:34:39] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:34:39] Using ignore file (if present) at /Users/ak/-/projects/kachkaev/oss/njt/.prettierignore
["INFO" - 17:34:39] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 17:34:39] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 17:34:39] Prettier Options:
{
  "filepath": "/Users/ak/-/projects/kachkaev/oss/njt/next.config.js",
  "parser": "babel",
  "arrowParens": "always",
  "endOfLine": "lf",
  "trailingComma": "all"
}
["INFO" - 17:34:40] Loaded module 'prettier@1.18.2' from '/usr/local/lib/node_modules/prettier'
["INFO" - 17:34:40] Formatting using 'prettier-eslint'
["ERROR" - 17:34:40] Error formatting document.
r is not a function
TypeError: r is not a function
    at /Users/ak/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352891
    at t.default.safeExecution (/Users/ak/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:353582)
    at t.default. (/Users/ak/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:352865)
    at Generator.next ()
    at s (/Users/ak/.vscode/extensions/esbenp.prettier-vscode-3.13.0/dist/extension.js:1:347571)
["INFO" - 17:34:40] Formatting completed in 349.554412ms.

It's unusual to see mentions of prettier 1.18.2, which is installed globally.


UPD: Running npm rm --global prettier brings the latest version of the extension back to life:

Output
["INFO" - 17:41:03] Extension Name: "prettier-vscode".
["INFO" - 17:41:03] Extension Version: "3.13.0".
["INFO" - 17:41:03] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:41:03] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:41:03] Enabling prettier for languages
[
  "css",
  "graphql",
  "html",
  "javascript",
  "javascriptreact",
  "json",
  "json5",
  "jsonc",
  "less",
  "markdown",
  "mdx",
  "mongo",
  "postcss",
  "scss",
  "typescript",
  "typescriptreact",
  "vue",
  "yaml"
]
["INFO" - 17:41:03] Enabling prettier for range supported languages
[
  "graphql",
  "javascript",
  "javascriptreact",
  "json",
  "typescript",
  "typescriptreact"
]
["INFO" - 17:41:10] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:41:14] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:41:17] Formatting /Users/ak/-/projects/kachkaev/oss/njt/next.config.js
["INFO" - 17:41:17] Loaded module 'prettier@1.19.1' from '/Users/ak/-/projects/kachkaev/oss/njt/node_modules/prettier/index.js'
["INFO" - 17:41:17] Using ignore file (if present) at /Users/ak/-/projects/kachkaev/oss/njt/.prettierignore
["INFO" - 17:41:17] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 17:41:17] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 17:41:17] Prettier Options:
{
  "filepath": "/Users/ak/-/projects/kachkaev/oss/njt/next.config.js",
  "parser": "babel",
  "arrowParens": "always",
  "endOfLine": "lf",
  "trailingComma": "all"
}
["INFO" - 17:41:18] Formatting completed in 412.703647ms.
travislang commented 4 years ago

@ntotten I think the issue is that the new update is somehow forcing the extension to load the global package, look at these logs:

// here it finds and loads the local prettier module
["INFO" - 12:20:09 PM] Loaded module 'prettier@1.19.1' from '/Users/tjlang/projects/ledger-app/ledger/node_modules/prettier/index.js'

["INFO" - 12:20:19 PM] Formatting /Users/tjlang/projects/ledger-app/ledger/sagas/auth.js
["INFO" - 12:20:19 PM] Loaded module 'prettier@1.19.1' from '/Users/tjlang/projects/ledger-app/ledger/node_modules/prettier/index.js'
["INFO" - 12:20:19 PM] Using ignore file (if present) at /Users/tjlang/projects/ledger-app/ledger/.prettierignore
["INFO" - 12:20:19 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 12:20:19 PM] Detected local configuration (i.e. .prettierrc or .editorconfig), VS Code configuration will not be used
["INFO" - 12:20:19 PM] Prettier Options:
{
  "filepath": "/Users/tjlang/projects/ledger-app/ledger/sagas/auth.js",
  "parser": "babel",
  "semi": false,
  "trailingComma": "all",
  "singleQuote": true,
  "printWidth": 100,
  "tabWidth": 4
}

// now here it loads the global prettier module
["INFO" - 12:20:19 PM] Loaded module 'prettier@1.19.1' from '/usr/local/lib/node_modules/prettier'

["INFO" - 12:20:19 PM] Formatting using 'prettier-eslint'
["ERROR" - 12:20:19 PM] Error formatting document.

It initially finds the local package but after getting all of the configs it loads the global package.

The fix for now is to run npm rm -g prettier like @kachkaev mentioned. This forces the extension to use the local package and it then formats correctly.

So somehow it is trying to load the global package and returning undefined somewhere even if the local package exists...

ntotten commented 4 years ago

I think i fixed it, try updating to 3.14.0. I accidentally left a hard-coded string in that was supposed to be a variable. See: https://github.com/prettier/prettier-vscode/blob/69af3e2713c0e0411b0e1deaf18bfd21dca220c1/src/ModuleResolver.ts#L148

Not sure how tests didn't get this. I need to investigate, but don't have time right now. My wife gave birth to our second kid last night so just doing a bit of work while they are both sleeping. ;)

Let me at least know that this fixed the problem.

kachkaev commented 4 years ago

@ntotten I reinstalled prettier globally and reproduced the bug in prettier-vscode@3.13.0 again. After upgrading to prettier-vscode@3.14.0 and reloading VSCode, the error disappeared πŸŽ‰

All works, many thanks for your quick reaction and for cutting a new release! πŸ™Œ

ntotten commented 4 years ago

Great. I'm going to leave this open so I can write some tests later, but glad it is working again.

travislang commented 4 years ago

@ntotten Same thing for me, reinstalled globally, 3.13 still had the issue, upgraded to 3.14 and the problem was solved. Thanks for fixing quickly and congrats on the second child! πŸŽ‰

hellatan commented 4 years ago

osx 10.15, vscode 1.41.0 - upgrade from prettier-vscode 3.13.0 to 3.14.0 and it looks like prettier is working again. thanks @ntotten and congrats! πŸΌπŸ‘ΆπŸŽ‰

franky47 commented 4 years ago

@ntotten Fixed as well here with 3.14.0 on VSCode 1.41.0. Thanks for a quick release and congratulations ! πŸŽ‰

github-actions[bot] commented 4 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.