Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Files in .prettierignore are emptied out on save #36

Closed
hsribei opened this issue May 29, 2018 · 3 comments
Closed

Files in .prettierignore are emptied out on save #36

hsribei opened this issue May 29, 2018 · 3 comments

Comments

@hsribei
Copy link

hsribei commented May 29, 2018

Steps to reproduce:

mkdir test
cd test
npm init -y
echo "package.json" > .prettierignore
emacsclient package.json
# make sure prettier-js-mode is running
# insert space then save

Desired result:

File gets saved without modifications.

Observed result:

File contents get deleted and file is saved as empty file.

@hsribei
Copy link
Author

hsribei commented May 29, 2018

This happens because prettier itself prints nothing and returns with exit code 0 (success) when passed a file as a parameter that is matched by the .prettierignore file.

This can be seen by running here:

mkdir test
cd test
npm init -y
echo "package.json" > .prettierignore

prettier package.json
# outputs nothing, goes straight to next prompt

mv .prettierignore .prettierignorebak
prettier package.json
# outputs this:
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}

Since it returns empty without an error code, this condition is not caught by the if here:

https://github.com/prettier/prettier-emacs/blob/master/prettier-js.el#L185

My elisp-fu is non-existent, but checking if prettier's output isn't empty shouldn't be too hard, so I might try fixing it.

@hsribei
Copy link
Author

hsribei commented May 29, 2018

My immediate use case for .prettierignore, which was avoiding formatting conflicts between npm and prettier on package.json, has been resolved by prettier just special-casing for package.json and package-lock.json to match npm's use of JSON.stringify(data, null, 2).

https://twitter.com/hsribei/status/1001560322082263040

I'll still see if I can make prettier-js-mode not clobber other .prettierignored files in case that helps someone else.

@hsribei
Copy link
Author

hsribei commented May 29, 2018

Ok, nevermind. The whole issue is solved since December last year. It's always a good idea to keep your packages updated!

Now instead of returning an empty output for ignored files, prettier just prints them back as-is, solving the problem.

prettier/prettier#3590

@hsribei hsribei closed this as completed May 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant