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

Enforce Prettier (add CI and pre-commit hook) #1251

Merged
merged 4 commits into from
Aug 21, 2019

Conversation

mchowning
Copy link
Contributor

@mchowning mchowning commented Jul 29, 2019

Fixes #816 by adding a prettier check to our CI builds and as a pre-commit hook.

This was tested by verifying that the "Check Correctness" check on CI failed before updating the code in this repo to comply with prettier, and the commit adding those changes caused the check to then succeed.

It may be easier to review this PR by looking at the commits so you can see the changes that are not solely prettier formatting updates separately. The vast majority of the changes in this PR are just formatting changes.

One mildly annoying aspect of this is the running prettier with the --list-different flag (which is needed to get a usable exit code) is that the console output seems rather unclear to me about whether it failed or succeeded ("success!" -> "error!"):

Screen Shot 2019-07-29 at 5 06 25 PM

That is why I decided to also echo some error messages to the console and make it clear what was occurring. Certainly open to other thoughts though.

Update release notes

Not applicable because there are no user facing changes.

@mchowning mchowning added this to the 1.11 milestone Jul 29, 2019
@mchowning mchowning self-assigned this Jul 29, 2019
@mchowning mchowning modified the milestones: 1.11, 1.12 Aug 20, 2019
@mchowning mchowning force-pushed the issue/816_enforce_prettier_on_ci branch 2 times, most recently from a9784e7 to cdbf311 Compare August 20, 2019 14:14
@mchowning mchowning force-pushed the issue/816_enforce_prettier_on_ci branch from cdbf311 to bf0ca2e Compare August 20, 2019 14:44
@mchowning mchowning requested a review from etoledom August 20, 2019 14:47
Copy link
Contributor

@jtreanor jtreanor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI changes look fine to me!

Copy link
Contributor

@JavonDavis JavonDavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mchowning This looks good! just a quick question, should this pre-check auto format? The message says something like success formatting 1 file with prettier-eslint so it made me think the file the should have been updated but it wasn't

Adding additional error messaging because with the
`--list-different` flag (which is needed to force a non-zero exit code)
and without the `--write` flag (which would cause a 0/successful exit
code) the default error messaging confusingly says both success and
error.
@mchowning mchowning force-pushed the issue/816_enforce_prettier_on_ci branch from bf0ca2e to adcc5dd Compare August 20, 2019 17:03
@mchowning
Copy link
Contributor Author

mchowning commented Aug 20, 2019

Thanks @JavonDavis !

This looks good! just a quick question, should this pre-check auto format? The message says something like success formatting 1 file with prettier-eslint so it made me think the file the should have been updated but it wasn't

Yes, that is confusing, I completely agree 😀! This sounds similar to what I was talking about with respect to the screenshot in the issue description. It seems like this is the behavior when using the --list-different flag, which is needed to make the command return a non-zero exit code if there are any formatting changes. If I add the --write flag to also change the files, then the exit code becomes 0, so the build doesn't fail. 🤦‍♂️

Because of this issue, I did make the script on husky and circleci print an additional error message, so that it would be clear(er) that yarn prettier:check did not actually succeed:

"husky": {
      "hooks": {
          "pre-commit": "yarn prettier:check || { echo '\nERROR: `yarn prettier:check` found a problem\n'; exit 1; }"
      }
  }

I'm betting that you didn't see that because you ran yarn prettier:check directly (instead of having it run by husky or circle-ci). Since I only had the additional error messaging at the level of husky and circle-ci, you wouldn't have seen it running the command directly. I just pushed a commit (adcc5dd) fixing that so the additional error messaging is contained with the prettier:check script itself. Thanks for catching that!

FWIW, I also added a bit more detail to the error message to (hopefully) make it clearer. WDYT?

@JavonDavis
Copy link
Contributor

@mchowning thanks for clearing that up for me, yeah I saw the error message and it was definitely clear but was just checking if I misunderstood what was entirely suppose to happen because of that first message I mentioned.

Now I'm wondering why we didn't go with the --write flag in the pre-commit hook since it would've done the auto-formatting and fix the errors, on CI we could leave as is to fail in case code code pushed without the pre commit hook being ran.

  • Is it that we want to not change someone's code automatically? - They'd have to do it themselves anyway to pass the CI check so why not?
  • Is it that the --write can't auto fix everything, in that case I'd still expect to fix what it can and return a non-zero exit code right?

I'm not sure but I think we could benefit more if we use the --write flag locally in the pre-commit hook, let me know if I'm making sense here or missing something, I haven't used prettier in a little while 😅

@mchowning
Copy link
Contributor Author

mchowning commented Aug 21, 2019

I'm not sure but I think we could benefit more if we use the --write flag locally in the pre-commit hook

I chatted about this with some of the other gutenberg-mobile devs and I think the preference at this time is to not automatically make the changes. A couple of the reasons were (1) consistency with the gutenberg project currently just rejects the commit instead of making the changes automatically; and (2) @koke raised a good point about there possibly being unexpected behavior when not all changes are staged for commit. Personally, I'm not a fan of pre-commit hooks making changes to non-generated files, but that is probably more of just a personal preference.

I prefer to not automatically make any changes in the hook at this time. We can, of course, always update this if we decide we want that in the future. Let me know what you think @JavonDavis , and thanks for bringing this up!

@JavonDavis
Copy link
Contributor

@mchowning Sounds good! I just caught up the conversation internally around it as well

Personally, I'm not a fan of pre-commit hooks making changes to non-generated files, but that is probably more of just a personal preference.

I kinda feel the same I mainly raised the question here since we'll be failing on CI if the check doesn't pass, so just thinking why not, in any case don't have strong feelings about this vs the way you currently have it so I think we're good to go for the merge IMO! 👍

Copy link
Contributor

@JavonDavis JavonDavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I also completely agree with the approach to stay as close to how it's done in gutenberg as possible.

@mchowning mchowning merged commit 0e597a4 into develop Aug 21, 2019
@mchowning mchowning deleted the issue/816_enforce_prettier_on_ci branch August 21, 2019 17:12
@maxme
Copy link
Contributor

maxme commented Aug 22, 2019

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prettier not being used consistently
4 participants