question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Semicolon added on each save

See original GitHub issue

Summary

A certain combination of code and modifications to that code will cause a semicolon to be added on every save, causing syntax error.

This issue was first raised on vscode here but re-directed here. VS Code system info can be found there.

Github Repository to Reproduce Issue

Link to a Github repo that can be used to reproduce the issue.

Steps To Reproduce:

  1. Load the repo with VS Code and VS Code Prettier extension.
  2. Ensure format on save is on, that Prettier is chosen as formatter and that the format on save mode is set to either "editor.formatOnSaveMode": "modificationsIfAvailable" or "editor.formatOnSaveMode": "modifications".
  3. Open the file test_case.js (which is same as before_change.js). Copy the contents of the after_change.js onto the test_case.js file.
  4. Save the file and see that a semicolon is added on last line every time you save, causing Syntax error.

Expected result

No invalid semicolons added.

Additional information

This only happens on the two mentioned formatOnSaveMode modes. If the format on save is changed to "editor.formatOnSaveMode": "file" the error doesn’t appear. So it seems to be an interplay between the way VS Code formats only the diffing parts (the diff is compared with what’s in the Git repo, see Test commit in the reproduction repo). The error doesn’t appear if there is no Git history to diff to.

Extension bisect was used to find that this error appears only with Prettier.

VS Code Version: Code 1.66.0 (https://github.com/microsoft/vscode/commit/e18005f0f1b33c29e81d732535d8c0e47cafb0b5, 2022-03-30T05:50:04.419

Prettier Extension Version: 9.5.0

OS and version: Darwin x64 18.7.0

Prettier Log Output

["INFO" - 4:05:20 PM] Extension Name: esbenp.prettier-vscode.
["INFO" - 4:05:20 PM] Extension Version: 9.5.0.
["INFO" - 4:05:36 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:36 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:36 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:36 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:36 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 130,
  "rangeStart": 39
}
["INFO" - 4:05:36 PM] Formatting completed in 0.06ms.
["INFO" - 4:05:36 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:36 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:36 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:36 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:36 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 313,
  "rangeStart": 313
}
["INFO" - 4:05:36 PM] Formatting completed in 0.007ms.
["INFO" - 4:05:36 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:36 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:36 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:36 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:36 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 611,
  "rangeStart": 501
}
["INFO" - 4:05:36 PM] Formatting completed in 0.028ms.
["INFO" - 4:05:36 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:36 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:36 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:36 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:36 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 313,
  "rangeStart": 39
}
["INFO" - 4:05:36 PM] Formatting completed in 0.008ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 130,
  "rangeStart": 39
}
["INFO" - 4:05:39 PM] Formatting completed in 0.009ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 313,
  "rangeStart": 313
}
["INFO" - 4:05:39 PM] Formatting completed in 0.006ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 611,
  "rangeStart": 501
}
["INFO" - 4:05:39 PM] Formatting completed in 0.012ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 795,
  "rangeStart": 792
}
["INFO" - 4:05:39 PM] Formatting completed in 0.008ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 313,
  "rangeStart": 39
}
["INFO" - 4:05:39 PM] Formatting completed in 0.008ms.
["INFO" - 4:05:39 PM] Formatting file:///Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js
["INFO" - 4:05:39 PM] Using ignore file (if present) at /Users/martin/dev/src/repro-vscode-prettier-semicolon/.prettierignore
["INFO" - 4:05:39 PM] File Info:
{
  "ignored": false,
  "inferredParser": "babel"
}
["INFO" - 4:05:39 PM] No local configuration (i.e. .prettierrc or .editorconfig) detected, falling back to VS Code configuration
["INFO" - 4:05:39 PM] Prettier Options:
{
  "arrowParens": "always",
  "bracketSpacing": true,
  "endOfLine": "lf",
  "htmlWhitespaceSensitivity": "css",
  "insertPragma": false,
  "jsxBracketSameLine": false,
  "jsxSingleQuote": false,
  "printWidth": 120,
  "proseWrap": "always",
  "quoteProps": "as-needed",
  "requirePragma": false,
  "semi": true,
  "singleQuote": false,
  "tabWidth": 2,
  "trailingComma": "es5",
  "useTabs": false,
  "vueIndentScriptAndStyle": false,
  "filepath": "/Users/martin/dev/src/repro-vscode-prettier-semicolon/test_case.js",
  "parser": "babel",
  "rangeEnd": 795,
  "rangeStart": 39
}
["INFO" - 4:05:39 PM] Formatting completed in 0.008ms.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:9
  • Comments:23 (8 by maintainers)

github_iconTop GitHub Comments

12reactions
darzucommented, Apr 7, 2022

I’m pretty suspicious of this commit: https://github.com/prettier/prettier-vscode/commit/be790ca13b85b26196340ce2a89a1f8a47545605

Indeed, if I change my “on save” mode to “whole file” instead of “modifications”, everything works again, no extra semi colon.

11reactions
medmincommented, Jun 26, 2022

any news ? This is annoying.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add semicolon to the end of the line in visual studio code
Settings -> in Search bar type: 'semicolon' -> change values to 'insert' for both JS and TS, save and close. Now every time...
Read more >
automatic semicolon after saving javascript file. : r/vscode
I would advise using semi colons. It makes it more obvious when you're ending a step in code and is easier to read....
Read more >
Thank you. For adding semicolon, you need to add semi
Yeah it is working after saving a file. But what I wanted is to add semicolon just immediately after using snippets. 1 ...
Read more >
Auto insert semicolon in Javascript when formatting file
Then use Alt-Ctrl-L to reformat your code according your style settings. It should add all missing semicolons. If any semicolon is not inserted...
Read more >
semi - ESLint - Pluggable JavaScript Linter
JavaScript doesn't require semicolons at the end of each statement. ... that a semicolon should be in a certain spot and will automatically...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found