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

Update and apply scalafmt #57

Merged
merged 3 commits into from
Oct 12, 2022

Conversation

mdedetrich
Copy link
Contributor

@mdedetrich mdedetrich commented Oct 7, 2022

Description

This PR updates the scalafmt config to the newest version as well as replacing a pre commit git hook with a github workflow action. This may be subjective but I have a personal hate with putting formatters within githooks because it really messes with IDE's especially in large codebases. Instead the github workflow action

  • Runs concurrently to the main build and uses the scala-native version of scalafmt. This means its ultra fast, taking only around 2-3 seconds on small projects
  • You can add the check to the list of branch checks that must pass before merging a PR to enforce not being able to merge unformatted code, i.e.

image

Type of change

Updating of scalafmt

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

@mdedetrich mdedetrich requested a review from a team as a code owner October 7, 2022 10:41
Copy link
Contributor

@paul-a-kennedy-rally paul-a-kennedy-rally left a comment

Choose a reason for hiding this comment

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

LGTM

@mdedetrich mdedetrich force-pushed the update-and-apply-scalafmt branch from 4680eb5 to 61f3d08 Compare October 7, 2022 22:50
.pre-commit-config.yaml Outdated Show resolved Hide resolved
Copy link
Contributor

@jeffmay jeffmay left a comment

Choose a reason for hiding this comment

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

I'm not sure I agree with removing pre-commit-hooks. I would prefer just running pre-commit manually with pre-commit run -a whenever you want and just verifying that pre-commit succeeds with a github workflow.

@jeffmay
Copy link
Contributor

jeffmay commented Oct 7, 2022

This may be subjective but I have a personal hate with putting formatters within githooks because it really messes with IDE's especially in large codebases.

Which IDE do you use? I use IntelliJ and you can tell it to not run git hooks. Also, you don't have to install pre-commit hooks locally.

@jducoeur
Copy link

jducoeur commented Oct 7, 2022

No longer at Rally myself, but from my time there, I found pre-commit-hooks absolutely invaluable. In combination with scalafmt and a properly-configured IntelliJ, it meant that I almost never needed to think about formatting, which was a non-trivial time-saver.

@mdedetrich
Copy link
Contributor Author

mdedetrich commented Oct 7, 2022

Regarding me personally when I did have them available it slowed down my workflow especially in large projects (especially with Scala due to sbt startup time). I typically only ran a scalafmt just before I was going to push something where as locally I may commit many times quickly. In tandem with the github actions scalafmt which takes a few seconds to complete on CI, if I did happen to forget to format I pretty got immediate feedback just when creating the PR.

In any case I am not going to die on this hill so I will rebase the PR to undo the removal of the pre commit hook.

@mdedetrich mdedetrich force-pushed the update-and-apply-scalafmt branch 3 times, most recently from bb615ef to 07cb459 Compare October 7, 2022 23:52
@mdedetrich
Copy link
Contributor Author

@jducoeur @jeffmay I have reverted the removal of the pre commit git hook

@mdedetrich mdedetrich requested a review from jeffmay October 8, 2022 01:34
@jeffmay
Copy link
Contributor

jeffmay commented Oct 10, 2022

Regarding me personally when I did have them available it slowed down my workflow especially in large projects (especially with Scala due to sbt startup time). I typically only ran a scalafmt just before I was going to push something where as locally I may commit many times quickly. In tandem with the github actions scalafmt which takes a few seconds to complete on CI, if I did happen to forget to format I pretty got immediate feedback just when creating the PR.

In any case I am not going to die on this hill so I will rebase the PR to undo the removal of the pre commit hook.

Maybe we can look into using sbt --client in our own pre-commit-hook. That way, if you have the native client running for other reasons, then it will use the same sbt instance.

Also, if you prefer, you don't have to install pre-commit locally and only run it manually when you want to finalize a commit with pre-commit run -a

Also, we should definitely be validating pre-commit in Github Actions, so thank you for adding that check!

@jeffmay jeffmay enabled auto-merge (squash) October 10, 2022 16:49
@jeffmay jeffmay disabled auto-merge October 10, 2022 16:54
@jeffmay
Copy link
Contributor

jeffmay commented Oct 10, 2022

Hmm...

scalafmt-native --list
Error: Command failed: /home/runner/bin/scalafmt-native --list

@mdedetrich
Copy link
Contributor Author

mdedetrich commented Oct 10, 2022

@jeffmay You can check the failure in the actual github action at https://github.com/rallyhealth/scalacheck-ops/actions/runs/3221009924/jobs/5268494582 i.e.

scalafmt-native --list
  build.sbt
  
  ::endgroup::
Error: Command failed: /home/runner/bin/scalafmt-native --list

You failed to format build.sbt (likely because you changed it). scalafmtSbt along with git commit amend should solve the issue

@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch from 83c00c1 to 76089f0 Compare October 11, 2022 17:12
@mdedetrich
Copy link
Contributor Author

So scalafmt pipeline failed again, are you doing scalafmtSbt locally?

@jeffmay
Copy link
Contributor

jeffmay commented Oct 11, 2022

I wonder how we can bridge the gap between the github action and pre-commit-conifg... I want to be able to run hooks that will autoformat the code (quickly) so that it passes CI. I think the "quickly" part rules out the old pre-commit-hooks that requires running a new JVM and sbt (not to mention it didn't even run scalafmtSbt). I wonder if we can run scalafmt-native from pre-commit the same way as github actions.

@mdedetrich
Copy link
Contributor Author

mdedetrich commented Oct 11, 2022

I wonder how we can bridge the gap between the github action and pre-commit-conifg... I want to be able to run hooks that will autoformat the code (quickly) so that it passes CI. I think the "quickly" part rules out the old pre-commit-hooks that requires running a new JVM and sbt (not to mention it didn't even run scalafmtSbt). I wonder if we can run scalafmt-native from pre-commit the same way as github actions.

I guess you can try sbt in thin client mode (see https://www.scala-sbt.org/1.x/docs/sbt-1.4-Release-Notes.html#Native+thin+client), that should reuse the JVM process. Even though it doesn't work with environment variables I don't think thats relevant here.

Or as you said you could also just include the binaries of scalafmt-native in the git repo however it only supports Linux/Mac (see https://scalameta.org/scalafmt/docs/installation.html#native-image) and it also presents its own host of problems.

@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch from 6887a55 to ccc9b7b Compare October 12, 2022 04:54
@jeffmay
Copy link
Contributor

jeffmay commented Oct 12, 2022

@mdedetrich it was easier than I thought. pre-commit supports docker images directly, so I could just use this: https://hub.docker.com/r/scalameta/scalafmt/tags

@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch 2 times, most recently from ae034ae to 6ad21a8 Compare October 12, 2022 05:24
@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch from 6ad21a8 to 8cb4fa9 Compare October 12, 2022 05:27
jeffmay
jeffmay previously approved these changes Oct 12, 2022
@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch 3 times, most recently from 028c1bd to f09e0d2 Compare October 12, 2022 05:40
@jeffmay jeffmay dismissed stale reviews from paul-a-kennedy-rally and themself via 1706d97 October 12, 2022 06:11
@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch 2 times, most recently from 23c87fa to 804e445 Compare October 12, 2022 06:40
@jeffmay jeffmay enabled auto-merge (rebase) October 12, 2022 06:59
Comment on lines 9 to 15
/** Some formatted
* @param gen
* @param config
* @param typeName
* @tparam T
*/

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove. This was for testing

- Add pre-commit-hooks-scala v0.0.1
  - Run scalafmt via coursier hook
- Run pre-commit on all files
- Validate pre-commit in format.yml github workflow
@jeffmay jeffmay force-pushed the update-and-apply-scalafmt branch from 804e445 to 978db2b Compare October 12, 2022 15:38
@paul-a-kennedy-rally
Copy link
Contributor

Copy link
Contributor

@paul-a-kennedy-rally paul-a-kennedy-rally left a comment

Choose a reason for hiding this comment

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

LGTM

@paul-a-kennedy-rally
Copy link
Contributor

Still something wonky going on: https://github.com/rallyhealth/scalacheck-ops/actions/runs/3235997094/jobs/5301188323

Well, I re-ran it and it was fine... hopefully not a source of flakiness.

@jeffmay
Copy link
Contributor

jeffmay commented Oct 12, 2022

Still something wonky going on: https://github.com/rallyhealth/scalacheck-ops/actions/runs/3235997094/jobs/5301188323

Well, I re-ran it and it was fine... hopefully not a source of flakiness.

Weird... looks like it had trouble downloading sbt... hopefully a rare issue.

@jeffmay jeffmay merged commit 1dae2bc into rallyhealth:main Oct 12, 2022
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

Successfully merging this pull request may close these issues.

4 participants