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
Add diff in output of --check (for CI use cases) #6885
Comments
"Forgot to run Prettier?" is really not a very helpful message... |
The message contains the file names. What else do you need to run Prettier on those files? |
I personally like the output of CI: "Forgot to run Prettier?" Me: "Oh yeah, I guess I did!" Runs Prettier. CI: "All good!" |
I like to point again to:
That means they have no local IDE installed. That's why they can not run prettier on the file. |
Expected behavior would be a detailed error message or a diff
|
Why not run |
thx. I would try on monday the output of |
@muescha I think this proposal might be solving your problem the wrong way. Forcing people to click into a CI build log, find diffs of expected output and then manually applying them does no sound very friendly. I think we could try several other things:
|
thats much overhead :(
it should run normal prettier
this is only the solution for one user, and not for all
this changes the PR - the user should have information about the problems
to much overhead to checkout a branch etc
also prettier is a good help the only thing is that there should be an more verbose error message :/ |
Thanks for the responses! You didn’t seem to respond to this one, though:
Thoughts? |
since they edit the PR on github and check why the CI build is failing they already looking into the CI logs. and then when they only find a not helpful message - that is frustrating. example gatsbyjs/gatsby#18719 (comment)
|
I was making changes to a repository that'd had prettier added to it without me realising, but I had not installed it in my local development environment. So naturally it blew up in CI and asked if I'd "forgotten to run prettier". But that message came from a program called "prettier", which was pretty confusing - yes I suppose I'd "forgotten" to run prettier (I hadn't actually, I didn't even know I had to. Unnecessarily snarky error message, if you ask me...) but even more annoying was that the prettier program was printing that message!
|
@dylannz-sailthru Would you really prefer to get a diff instead like @muescha has proposed and apply it manually instead of running the formatter? |
I admit, I've now set up my IDE with prettier and now it doesn't bother me. But it seems there is a use case where someone wants to make a small change and doesn't have prettier installed (either on GitHub or in a local development environment), adds an extra space somewhere, and now has to install a 3rd party program and run it instead of just fixing a small error. And while I'll admit it's a narrow use case, I can't see much of a downside to being more verbose (or at least having a configurable verbose flag). Most CI tools will collapse each CI step so you can view the logs of each one in isolation. |
example where i used the GitHub editor and i get for this change gatsbyjs/gatsby@7b78781 in gatsbyjs/gatsby#19822 this CI log message: now i have to checkout ~500MB data from pull request, install prettier only just to see whats wrong in this commit and i can change the file? |
@dylannz-sailthru Locally, extra setup isn't needed. Prettier is installed as a dev dependency and there is usually a command in the project to run all the linters, formatters, etc. (e.g. |
Sorry, the message is actually not supposed to be snarky, but friendly! Do you have ideas for better wording?
I don’t think that’s a good example. That’s not a markdown change, but a code change! Is that really something people do in the browser? That sounds terrible to me – how do you debug and test then? Here’s yet one idea: When editing a markdown file in the GitHub editor and are about to hit the Commit button – first copy-paste the entire file into https://prettier.io/playground/ and then copy back the output. |
yes - people do it. so why not also on all files.
that maybe a workaround but not a solution and also not fit with the project settings for prettier |
Sure! Something like: This way it's telling me more specifically what I need to do to get past the error. I think it doesn't matter so much for people who have used
Actually that'd be a great thing to link to in the error message as well - I'd have found it really useful at the time :) |
This comment has been minimized.
This comment has been minimized.
I would also like to see a diff or more detailed information about why prettier thinks a file isn't formatted. I currently don't run |
@grncdr It tells you which files aren’t formatted, so all you need to do is run Prettier on those files. Or on your whole project – probably |
GitHub Actions and Bamboo are the two that I know of that can parse this output |
I understand where you are coming from but Of course, one could edit their prettierconfig and run it again and again till the desired config is reached but still, there's no way to properly revert.
There might indeed be better ways to use Prettier but looking at the amount of comments and votes in this thread, there is clearly a lot of people that would like to use Prettier in this specific way. Now, it's easy to support both ways of working. Simply add a I also agree that the current error message is incorrect either way. "Forgot to run Prettier" is a weird error to give to a user that has just ran Prettier. |
As a workaround, if your project is using git, you can use |
Still no diffs in outputs??? |
Listing the rule(s) because of which a file contains warning(s) is necessary to understand the current setup. (E.g. when some behaviour makes you crazy and you want to find out which rule is responsible for that to modify it). I'M SHOCKED that there is no way to list this when running prettier command. How some functionality as basic and expected as this one can be missing is beyond me. |
Is there any chance to have a helpful message while |
I agree with others that this is some pretty basic functionality that is provided by tooling in other languages (isort, black in Python). At the very least, it'd be useful to know as a beginner to the codebase what you messed up when it comes to the style so that you can make a conscious effort at not repeating it instead of relying on the IDE to magically fix things for you. For me style checks in a CI env is akin to UTs. Think of how annoying and frustrating it would be if your test suite said "A test failed in your project. Did you not write it properly?" without giving you ANY further logs on which test failed. |
I encountered this issue today! I don't want to install whole npm and frontend packages on my machine just to find out 1 small issue that I could have fixed right away if there was a simple diff or description about the issue! And this issue is Open for 4 years now!! |
Agreed. Just saying "something" failed on the failed, without saying what is not as useful as it should be. For example, eslint gives output like this.
Not only does it tell me which file had a failure, but it also tells me at what line, and what the failure is. If I'm confused, I can search the web for "eslint import/order" for additional information. A similar output from prettier would be very helpful. My current method of understanding prettier failures is to run with the |
So we have our Prettier setup correctly with our developers IDE's, but now we are running into some issues in the CI pipeline. It's probably some cache issue, also probably unrelated to Prettier itself. But anyway, whatever the actual issue is, it would be quite helpful if we could see what the problem in a file is in the CI logs, because on our local IDE's/terminals we don't have the issue. It would make debugging why our CI is failing much faster, we could see if it's maybe running it on an old version of the file or whatever. I think it's a pretty mundane feature that most linters have and I think it makes sense for Prettier too. I know Prettier is not really a linter, but still, it's a script that runs automatic checks on your code. |
Have you tried the "Prettier write -> git diff" method? |
The "Prettier write -> git diff" method doesn't really work in CI pipelines. The container running the pipeline probably doesn't even have git installed. |
I mean.. Depends on the pipelines. There are a lot of pipelines that do some validation, and then generate a git tag and push it. |
I was finally able to overcome this limitation with GitHub actions using The name: Prettier code formatter (Push)
on:
push:
branches:
- master
- main
workflow_dispatch:
jobs:
check:
# available images: https://github.com/actions/runner-images#available-images
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Node.js ⚙️
uses: actions/setup-node@v4
- name: Install Prettier 💾
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
- name: Prettier Check 🔎
id: prettier
run: npx prettier . --check
- name: Show diff 📝
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
if: ${{ failure() }}
run: |
npx prettier . --write
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
npm install -g diff2html-cli
diff2html -i file -s side -F diff.html -- diff.txt
- name: Upload html diff
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
uses: actions/upload-artifact@v4
with:
name: HTML Diff
path: diff.html
retention-days: 3 The name: Prettier code formatter (PR)
on:
pull_request:
branches:
- master
- main
workflow_dispatch:
jobs:
check:
# available images: https://github.com/actions/runner-images#available-images
runs-on: ubuntu-latest
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
- name: Setup Node.js ⚙️
uses: actions/setup-node@v4
- name: Install Prettier 💾
run: npm install --save-dev --save-exact prettier @shopify/prettier-plugin-liquid
- name: Prettier Check 🔎
id: prettier
run: npx prettier . --check
- name: Show diff 📝
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure
if: ${{ failure() }}
run: |
npx prettier . --write
git diff -- . ':(exclude)package-lock.json' ':(exclude)package.json' > diff.txt
npm install -g diff2html-cli
diff2html -i file -s side -F diff.html -- diff.txt
- name: PR comment with html diff
# https://docs.github.com/en/actions/learn-github-actions/expressions#failure-with-conditions
if: ${{ failure() && steps.prettier.conclusion == 'failure' }}
uses: thollander/actions-comment-pull-request@v2
with:
filePath: diff.html |
I'm still happy to rebase #12598 and fix any further feedback if someone can commit to reviewing and merging it. I simply don't have the access to do so. |
I'm a back-end dev coming mostly from C# and am fairly new to npm, node etc. I was surprised to see a linter not give information on what the issues are in a check, mostly because I cannot blindly allow it to modify files. I have used eslint previously and definitely like the output from it very much. It's short, concise and tells you exactly what to do to fix it if you want to do it yourself. Glad to see this ticket open (since 2019🫠) and yes a |
also there exists an Feature Request for
|
I found, that sometimes there are false positives or new situations where you do not want the code formatted like that. There is no way, that #12598 would solve the conflict. It does not modify the default "way" of prettier. |
Struggling with an issue where locally running I think this is definitely an example of a legit use case for getting a diff printout straight from Prettier. Yes, it can be achieved with a combination of |
Oh man, this is happening to me now. It only happens for tsconfig.json files too! Not sure why. EDIT: looks like it's been fixed in 3.2.5, thanks! @joonashak |
I don't even want this for CI usecases, I'm just trying out prettier and want to see what it'll do to my code before I clobber it all. I could try to figure out the right incantations to get git to stash the changes I'm working on atm without stashing the prettier config itself, etc etc, so that git can revert --write's changes if I don't like them but it's a little silly that there's not just a dry run option in prettier itself. :x |
@mesmere have you considered to opt out prettier? |
@alexey-sh BLASPHEMER!! 😂🙃 Another option would be to use the eslint plugin, which I think will show the errors when you run it through that? |
More people submit PR for documents in Markdown files or other things via Github web editor. If they make a mistake then the CI fails but there is no chance to inform the user whats wrong.
I see some infos should be with the
--debug-check
but currently there is no output with this option.Environments:
Steps to reproduce:
Expected behavior:
Show each error and the changes like with
--debug-check
here in #4415Actual behavior:
Show nothing about the errors:
Also there are no more infos available with this commands:
The text was updated successfully, but these errors were encountered: