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.

rejects .prettierignore'd files when prettier can't parse the file

See original GitHub issue

Description

lint-staged doesn’t respect .prettierignore if prettier can’t parse the file.

Steps to reproduce

  1. Make a file prettier can’t parse.
// file.ts
import { <%= classify(name) %>Query } from './<%= dasherize(name) %>.query';
  1. Add file to .prettierignore
// .prettierignore
**/path/to
  1. Stage file and commit
  2. Watch commit get rejected
× prettier found some errors. Please fix them and try committing again.

[error] path\to\file.ts: SyntaxError: Identifier expected. (1:10)
[error] > 1 | import { <%= classify(name) %>Query } from './<%= dasherize(name) %>.query';
[error]     |          ^

I verified that prettier ignores it by running prettier -c "path/to/**/*.ts" and that correctly doesn’t report any errors.

Debug Logs

Happy to but it’s not explained how to enable debug mode via package.json.lint-staged and couldn’t get it to work 😦

Current config

  "lint-staged": {
    "*.ts": [
      "prettier --write"
    ]
  },
expand to view
$ git commit --file="C:/Repos/nx-akita/.git/COMMITMESSAGE"


husky > pre-commit (node v13.9.0)
2020-03-15T07:10:40.174Z lint-staged:bin Running `lint-staged@10.0.8`
2020-03-15T07:10:40.176Z lint-staged:bin Options parsed from command-line: {
  allowEmpty: false,
  concurrent: true,
  configPath: undefined,
  debug: true,
  maxArgLength: 4095.5,
  quiet: false,
  relative: false,
  shell: false
}
2020-03-15T07:10:40.178Z lint-staged Loading config using `cosmiconfig`
2020-03-15T07:10:40.199Z lint-staged Successfully loaded config from `C:\Repos\nx-akita\package.json`:
{ '*.{ts,yml,json}': [ 'prettier --write' ] }
2020-03-15T07:10:40.199Z lint-staged:cfg Validating config
Running lint-staged with the following config:
{
  '*.{ts,yml,json}': [
    'prettier --write'
  ]
}
2020-03-15T07:10:40.200Z lint-staged:run Running all linter scripts
2020-03-15T07:10:40.200Z lint-staged:resolveGitRepo Resolving git repo from `C:\Repos\nx-akita`
2020-03-15T07:10:40.200Z lint-staged:resolveGitRepo Deleting GIT_DIR from env with value `undefined`
2020-03-15T07:10:40.201Z lint-staged:git Running git command [ 'rev-parse', '--show-toplevel' ]
2020-03-15T07:10:40.244Z lint-staged:resolveGitRepo Resolved git directory to be `C:/Repos/nx-akita`
2020-03-15T07:10:40.244Z lint-staged:resolveGitRepo Resolved git config directory to be `C:/Repos/nx-akita/.git`
2020-03-15T07:10:40.244Z lint-staged:git Running git command [ 'diff', '--staged', '--diff-filter=ACMR', '--name-only' ]
2020-03-15T07:10:40.292Z lint-staged:run Loaded list of staged files in git:
[
  'libs/akita-schematics/src/ng-g/entity-query/files/__name@dasherize__.query.spec.ts'
]
2020-03-15T07:10:40.293Z lint-staged:chunkFiles Resolved an argument string length of 115 characters from 1 files
2020-03-15T07:10:40.293Z lint-staged:chunkFiles Creating 1 chunks for maxArgLength of 4095.5
2020-03-15T07:10:40.293Z lint-staged:gen-tasks Generating linter tasks
2020-03-15T07:10:40.296Z lint-staged:gen-tasks Generated task: 
{
  pattern: '*.{ts,yml,json}',
  commands: [ 'prettier --write' ],
  fileList: [
    'C:/Repos/nx-akita/libs/akita-schematics/src/ng-g/entity-query/files/__name@dasherize__.query.spec.ts'
  ]
}
2020-03-15T07:10:40.296Z lint-staged:make-cmd-tasks Creating listr tasks for commands [ 'prettier --write' ]
2020-03-15T07:10:40.297Z lint-staged:task cmd: prettier
2020-03-15T07:10:40.297Z lint-staged:task args: [ '--write' ]
2020-03-15T07:10:40.297Z lint-staged:task execaOptions: { preferLocal: true, reject: false, shell: false }
Preparing... [started]
2020-03-15T07:10:40.299Z lint-staged:git Backing up original state...
2020-03-15T07:10:40.299Z lint-staged:git Backing up merge state...
2020-03-15T07:10:40.299Z lint-staged:file Reading file `C:\Repos\nx-akita\.git\MERGE_HEAD`
2020-03-15T07:10:40.299Z lint-staged:file Reading file `C:\Repos\nx-akita\.git\MERGE_MODE`
2020-03-15T07:10:40.299Z lint-staged:file Reading file `C:\Repos\nx-akita\.git\MERGE_MSG`
2020-03-15T07:10:40.303Z lint-staged:file File `C:\Repos\nx-akita\.git\MERGE_HEAD` doesn't exist, ignoring...
2020-03-15T07:10:40.303Z lint-staged:file File `C:\Repos\nx-akita\.git\MERGE_MODE` doesn't exist, ignoring...
2020-03-15T07:10:40.303Z lint-staged:file File `C:\Repos\nx-akita\.git\MERGE_MSG` doesn't exist, ignoring...
2020-03-15T07:10:40.303Z lint-staged:git Done backing up merge state!
2020-03-15T07:10:40.303Z lint-staged:git Running git command [ 'ls-files', '--deleted' ]
2020-03-15T07:10:40.367Z lint-staged:git Running git command [
  'stash',
  'save',
  '--include-untracked',
  '--keep-index',
  'lint-staged automatic backup'
]
2020-03-15T07:10:40.918Z lint-staged:git Restoring merge state...
2020-03-15T07:10:40.918Z lint-staged:git Done restoring merge state!
2020-03-15T07:10:40.918Z lint-staged:git Running git command [ 'ls-files', '--others', '--exclude-standard' ]
2020-03-15T07:10:41.000Z lint-staged:git Running git command [ 'stash', 'list' ]
2020-03-15T07:10:41.097Z lint-staged:git Running git command [
  'diff',
  '--binary',
  '--unified=0',
  '--no-color',
  '--no-ext-diff',
  '--patch',
  '--output=C:\\Repos\\nx-akita\\.git\\lint-staged_unstaged.patch',
  'stash@{0}',
  '-R'
]
2020-03-15T07:10:41.194Z lint-staged:git Done backing up original state!
Preparing... [completed]
Running tasks... [started]
Running tasks for *.{ts,yml,json} [started]
prettier --write [started]
prettier --write [failed]
→ 
Running tasks for *.{ts,yml,json} [failed]
→ 
Running tasks... [failed]
Applying modifications... [started]
Applying modifications... [skipped]
→ Skipped because of errors from tasks.
Reverting to original state... [started]
2020-03-15T07:10:41.658Z lint-staged:git Restoring original state...
2020-03-15T07:10:41.659Z lint-staged:git Running git command [ 'stash', 'list' ]
2020-03-15T07:10:41.743Z lint-staged:git Running git command [ 'reset', '--hard', 'HEAD' ]
2020-03-15T07:10:41.877Z lint-staged:git Running git command [ 'stash', 'apply', '--quiet', '--index', 'stash@{0}' ]
2020-03-15T07:10:42.258Z lint-staged:git Done restoring original state!
2020-03-15T07:10:42.258Z lint-staged:git Restoring merge state...
2020-03-15T07:10:42.259Z lint-staged:git Done restoring merge state!
Reverting to original state... [completed]
Cleaning up... [started]
2020-03-15T07:10:42.259Z lint-staged:git Dropping backup stash...
2020-03-15T07:10:42.259Z lint-staged:file Unlinking file `C:\Repos\nx-akita\.git\lint-staged_unstaged.patch`
2020-03-15T07:10:42.260Z lint-staged:git Running git command [ 'stash', 'list' ]
2020-03-15T07:10:42.354Z lint-staged:git Running git command [ 'stash', 'drop', '--quiet', 'stash@{0}' ]
2020-03-15T07:10:42.470Z lint-staged:git Done dropping backup stash!
Cleaning up... [completed]



× prettier found some errors. Please fix them and try committing again.

[error] libs\akita-schematics\src
g-g\entity-query\files\__name@dasherize__.query.spec.ts: SyntaxError: Identifier expected. (1:10)
[error] > 1 | import { <%= classify(name) %>Query } from './<%= dasherize(name) %>.query';
[error]     |          ^
[error]   2 | import { <%= classify(name) %>Store } from './<%= dasherize(name) %>.store';
[error]   3 | 
[error]   4 | describe('<%= classify(name) %>Query', () => {
husky > pre-commit hook failed (add --no-verify to bypass)

Environment

  • OS: Windows 10
  • Node.js: v13.9.0
  • lint-staged: v10.0.8

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

1reaction
iirojcommented, Mar 15, 2020

Cool, in any case PR https://github.com/okonet/lint-staged/pull/801 will improve this.

1reaction
snebjorncommented, Mar 15, 2020

Indeed I was and that worked. Thanks. Edited OP with the log.

But I found out something more when trying to reproduce it. My .prettierignore file wasn’t committed yet so it was unstage when I committed the file in question.

Believe I read somewhere that all unstaged files get stashed? Cause that would remove the ignore setting when it would run the check

When I committed the .prettierignore file first and then the ignored file, then it worked

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does Prettier not format code in VS Code? - Stack Overflow
Open Files -> Preferences -> Settings (or Ctrl + , in Windows). Search for Editor: Default Formatter; Select your default formatter as Prettier...
Read more >
How to fix Prettier Extension not working issue in VS CODE
0:00 · New! Watch ads now so you can enjoy fewer interruptions. Got it.
Read more >
Vim Setup - Prettier
Run :Neoformat or :Neoformat prettier in a supported file to run Prettier. To have Neoformat run Prettier on save: autocmd BufWritePre *.js Neoformat....
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