neoclide / coc-prettier

Prettier extension for coc.nvim.
MIT License
546 stars 48 forks source link

[help] Silent failure #110

Closed maricn closed 2 years ago

maricn commented 3 years ago

What doesn't work

The commands report success, but the file doesn't change. Especially with :call CocAction('format') I get the response [coc.nvim] Formatted by prettier, while with other two commands it just finishes without reporting anything in the output. Ofc, format on save doesn't work.

All of those worked completely fine until a few days ago. I don't know what changed, I did run arch packages update and update of vim plugins and of coc extensions but i've also tried reverting them without fixing the problem.

I tried

What works well

Context (files)

:CocConfig (only global, no local):

{
  "prettier.tslintIntegration": true,
  "coc.preferences.formatOnSaveFiletypes": [
    "css",
    "javascript",
    "typescript",
    "json",
    "jsonc"
  ],
  "coc.preferences.colorSupport": false,
  "diagnostic.hintSign": ">>",
  "diagnostic.infoSign": "ℹ️",
  "diagnostic.warningSign": "⚠️",
  "diagnostic.errorSign": "💣",
  "diagnostic.enableHighlightLineNumber": true,
  "diagnostic-languageserver.filetypes": {
    "vim": "vint",
    "email": "languagetool",
    "markdown": ["write-good", "markdownlint"],
    "sh": "shellcheck"
  },
  "typescript.preferences.importModuleSpecifier": "relative",
  "markdownlint.config": {
    "line-length": false,
    "MD022": false,
    "MD032": false,
    "MD033": false, // inline HTML shouldn't be forbidden (<a name="asdf"></a>...[goto](#asdf))
    "MD034": false,
    "MD025": { "front_matter_title": "" }
  },
  "languageserver": {
    "ccls": {
      "command": "ccls",
      "filetypes": ["c", "cpp", "cuda", "objc", "objcpp"],
      "rootPatterns": [".ccls-root", "compile_commands.json"],
      "initializationOptions": {
        "cache": {
          "directory": ".ccls-cache"
        },
        "client": {
          "snippetSupport": true
        }
      }
    }
  },
  "explorer.icon.enableNerdfont": true
}

.config/coc/extensions/package.json:

{
  "dependencies": {
    "coc-db": ">=0.0.43",
    "coc-emoji": ">=1.2.3",
    "coc-explorer": ">=0.9.1",
    "coc-highlight": ">=1.2.6",
    "coc-jest": ">=1.1.1",
    "coc-json": ">=1.3.2",
    "coc-markdownlint": ">=1.7.0",
    "coc-prettier": ">=1.1.18",
    "coc-python": ">=1.2.13",
    "coc-sql": ">=0.4.0",
    "coc-syntax": ">=1.2.4",
    "coc-tsserver": ">=1.6.0",
    "coc-word": ">=1.2.2"
  }
}

.prettierrc:

{
  "useTabs": false,
  "singleQuote": true,
  "trailingComma": "all",
  "tabWidth": 2,
  "printWidth": 120
}

tslint.json:

{
  "defaultSeverity": "error",
  "extends": ["tslint:recommended", "tslint-config-prettier"],
  "jsRules": {
    "no-unused-expression": true
  },
  "rules": {
    "arrow-parens": false,
    "array-type": false,
    "curly": [true, "ignore-same-line"],
    "eofline": true,
    "indent": [true, "spaces", 2],
    "interface-name": false,
    "max-classes-per-file": false,
    "max-line-length": [true, 120],
    "member-access": false,
    "member-ordering": [
      true,
      {
        "order": [
          "public-static-field",
          "public-static-method",
          "protected-static-field",
          "protected-static-method",
          "private-static-field",
          "private-static-method",
          "public-instance-field",
          "protected-instance-field",
          "private-instance-field",
          "public-constructor",
          "protected-constructor",
          "private-constructor",
          "public-instance-method",
          "protected-instance-method",
          "private-instance-method"
        ]
      }
    ],
    "no-empty": false,
    "no-empty-interface": false,
    "no-unused-expression": true,
    "object-literal-sort-keys": false,
    "one-line": true,
    "one-variable-per-declaration": true,
    "ordered-imports": true,
    "quotemark": [true, "single", "avoid-escape"],
    "variable-name": [
      true,
      "ban-keywords",
      "check-format",
      "allow-snake-case",
      "allow-pascal-case",
      "allow-leading-underscore"
    ]
  },
  "rulesDirectory": []
}
dlee commented 3 years ago

I'm running into the same issue, except it only happens on a specific file. Other files get properly formatted.

indeedwatson commented 3 years ago

I'm running into the same issue, except it only happens on a specific file. Other files get properly formatted.

Same here, with an html file

kozer commented 3 years ago

Just out of curiosity, as I had a similar problem. Does add prettier-tslint to your project solve the issue?

maricn commented 3 years ago

Just out of curiosity, as I had a similar problem. Does add prettier-tslint to your project solve the issue?

Finally! @kozer Yes, it does, the magic is back 🧙

But I'm still puzzled why this became necessary out of sudden when it was working before.

chemzqm commented 3 years ago

It could be using bundled prettier from coc-prettier before.

kozer commented 3 years ago

@maricn This is what puzzles me also, as the bundled prettier-tslint is in there (in coc-prettier's node modules/.bin)

chemzqm commented 3 years ago

Local prettier is used when possible by default, so when you don't have local prettier, global prettier is used.

kozer commented 3 years ago

@chemzqm I think that this is not the same functionality as vs code. In vs code I didn't have to install prettier-tslint in order to make it work with typescript files. Maybe this is a bug?

chemzqm commented 3 years ago

I see, seems API used by prettier get changed.

chemzqm commented 3 years ago

Works as expected for me, don't know how to reproduce.

maricn commented 3 years ago

@chemzqm Without going into the internals of this package, maybe this line from my original post is helpful:

Especially with :call CocAction('format') I get the response [coc.nvim] Formatted by prettier, while with other two commands it just finishes without reporting anything in the output. Ofc, format on save doesn't work.

So, maybe the way the plugin detects failure of the external command is flawed? It reports success, but actually nothing happened. Is the fallback to plugin's prettier working in case there's an error with the local one (if it's how it works anyways)?

chemzqm commented 3 years ago

Please checkout output from coc-prettier https://github.com/neoclide/coc.nvim/wiki/Debug-language-server#using-output-channel

maricn commented 3 years ago

Please checkout output from coc-prettier neoclide/coc.nvim/wiki/Debug-language-server#using-output-channel

This is what happens without prettier-tslint when I do :call CocAction('format'):

[Error - 6/18/2021, 6:07:32 PM:] Failed to load prettier-tslint from /home/nikola/.dotfiles/.config/coc/extensions/node_modules/coc-prettier

[Error - 6/18/2021, 6:07:32 PM:] Cannot read property 'format' of null

[Trace - 6/18/2021, 6:07:32 PM:] Formatted file: file:///home/nikola/Workspace/ProjectXXX/src/PackageYYY/FileZZZ.ts

[Trace - 6/18/2021, 6:07:32 PM:] Prettier format edits: [
  {
    "range": {
      "start": {
        "character": 0,
        "line": 0
      },
      "end": {
        "character": 0,
        "line": 54
      }
    },
    "newText": "REDACTED"
  }
]

there's some symlinks on that path, but this is what's there:

$> ls -lah /home/nikola/.dotfiles/.config/coc/extensions/node_modules/coc-prettier
.rw-r--r-- nikola nikola 116 B  Tue Mar 30 10:30:48 2021  .prettierrc
drwxr-xr-x nikola nikola 4.0 KB Tue Mar 30 10:30:48 2021  .release/
.rw-r--r-- nikola nikola 460 B  Tue Mar 30 10:30:48 2021  CHANGELOG.md
.rw-r--r-- nikola nikola 481 B  Tue Mar 30 10:30:48 2021  esbuild.js
drwxr-xr-x nikola nikola 4.0 KB Tue Mar 30 10:30:48 2021  lib/
drwxr-xr-x nikola nikola  20 KB Tue Mar 30 10:30:48 2021  node_modules/
.rw-r--r-- nikola nikola 233 B  Tue Mar 30 10:30:34 2021  package-json-schema.json
.rw-r--r-- nikola nikola 204 KB Tue Mar 30 10:30:34 2021  package-lock.json
.rw-r--r-- nikola nikola 7.7 KB Tue Mar 30 10:30:34 2021  package.json
.rw-r--r-- nikola nikola 5.9 KB Tue Mar 30 10:30:48 2021  Readme.md

And the list of extensions in nvim sees coc-prettier version 1.1.23 atm at that path.

Not sure what this line means:

[Error - 6/18/2021, 6:07:32 PM:] Cannot read property 'format' of null
chemzqm commented 3 years ago

There should be prettier-tslint inside node_modules folder of coc-prettier

maricn commented 3 years ago

There should be prettier-tslint inside node_modules folder of coc-prettier

I have prettier-tslint version 0.4.2 (latest, from Jan 2019) inside coc-prettier version 1.1.23.

derekstavis commented 2 years ago

I'm using coc-prettier@1.1.24. I was having a similar problem where files were not being formatted and just a new line was being added to the end of the file. After installing prettier-tslint to my project it started working again.

chemzqm commented 2 years ago

Make sure check the Prettier output by :CocCommand workspace.showOutput