Search code examples
typescriptprettier

Why is Prettier not formating one of the .ts files?


In Visual Studio Code, according to the Prettier logs, formatting has completed but the file in question hasn’t changed.

["INFO" - 8:03:25 AM] Prettier Options:
{
  "filepath": "/path/to/project/src/test/index.ts",
  "parser": "typescript",
  "useTabs": false,
  "tabWidth": 2,
  "endOfLine": "lf",
  "printWidth": 80,
  "semi": false,
  "trailingComma": "es5"
}
["INFO" - 8:03:25 AM] Formatting completed in 466.936538ms.

File content before and after it is saved (excerpt)

'use strict'

import dotenv from 'dotenv';

Expected file content after it is saved (excerpt)

"use strict"

import dotenv from "dotenv"

Here’s my Prettier section in package.json.

"prettier": {
  "endOfLine": "lf",
  "printWidth": 80,
  "semi": false,
  "tabWidth": 2,
  "trailingComma": "es5"
}

All other files in the project appear to be formatted correctly when they are saved.


Solution

  • Found the culprit!

    Visual Studio Code has an editor.formatOnSaveTimeout setting that is set to 750 milliseconds by default.

    Timeout in milliseconds after which the formatting that is run on file save is cancelled.

    That wasn’t enough time for Prettier to format my file which has over 2,000 lines.

    I switched editor.formatOnSaveTimeout to 1500 and it worked!