Allow opt out of code discipline (#28588)

This commit is contained in:
Johan Andrén 2020-02-14 15:19:10 +01:00 committed by GitHub
parent c032ac2fdb
commit cacc20bd8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 41 deletions

View file

@ -10,7 +10,7 @@ ignored-files = [
"ProtobufProtocol.java" "ProtobufProtocol.java"
] ]
//ignore by pacakges: //ignore by packages:
// 1. adated source code // 1. adated source code
// 2. protobuf generated messages // 2. protobuf generated messages
ignored-packages = [ ignored-packages = [

View file

@ -427,14 +427,21 @@ Preferences > Editor > Code Style > Scala, select Scalafmt as formatter and enab
IntelliJ will then use the same settings and version as defined in `.scalafmt.conf` file. Then it's IntelliJ will then use the same settings and version as defined in `.scalafmt.conf` file. Then it's
not needed to use `sbt scalafmtAll` when editing with IntelliJ. not needed to use `sbt scalafmtAll` when editing with IntelliJ.
PR validation includes checking that the Scala sources are formatted and will fail if they are not.
### Java style ### Java style
Java code is currently not automatically reformatted by sbt (expecting to have a plugin to do this soon). Akka uses [the sbt Java Formatter plugin](https://github.com/sbt/sbt-java-formatter) to format Java sources.
Thus we ask Java contributions to follow these simple guidelines:
- 2 spaces PR validation includes checking that the Java sources are formatted and will fail if they are not.
- `{` on same line as method name
- in all other aspects, follow the [Oracle Java Style Guide](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html) ### Code discipline opt out
In addition to formatting the Akka build enforces code discipline through a set of compiler flags. While exploring ideas
the discipline may be more of a hindrance than a help so it is possible to disable it by setting the system property `akka.no.discipline`
to any non-empty string value when starting up sbt: `sbt -Dakka.no.discipline=youbet`
PR validation includes the discipline flags and therefore may fail if the flags were disabled during development. Make sure you compile your code at least once with discipline enabled before sending a PR.
### Preferred ways to use timeouts in tests ### Preferred ways to use timeouts in tests

View file

@ -16,6 +16,9 @@ object AkkaDisciplinePlugin extends AutoPlugin with ScalafixSupport {
override def requires: Plugins = JvmPlugin && ScalafixPlugin override def requires: Plugins = JvmPlugin && ScalafixPlugin
override lazy val projectSettings = disciplineSettings override lazy val projectSettings = disciplineSettings
// allow toggling for pocs/exploration of ideas without discpline
val enabled = !sys.props.contains("akka.no.discipline")
// We allow warnings in docs to get the 'snippets' right // We allow warnings in docs to get the 'snippets' right
val nonFatalWarningsFor = Set("akka-docs") val nonFatalWarningsFor = Set("akka-docs")
@ -40,8 +43,7 @@ object AkkaDisciplinePlugin extends AutoPlugin with ScalafixSupport {
"akka-stream-testkit", "akka-stream-testkit",
"akka-stream-tests", "akka-stream-tests",
"akka-stream-tests-tck", "akka-stream-tests-tck",
"akka-testkit", "akka-testkit")
)
lazy val scalaFixSettings = Seq(Compile / scalacOptions += "-Yrangepos") lazy val scalaFixSettings = Seq(Compile / scalacOptions += "-Yrangepos")
@ -54,40 +56,44 @@ object AkkaDisciplinePlugin extends AutoPlugin with ScalafixSupport {
} }
lazy val disciplineSettings = lazy val disciplineSettings =
scalaFixSettings ++ if (enabled) {
silencerSettings ++ Seq( scalaFixSettings ++
Compile / scalacOptions ++= ( silencerSettings ++ Seq(
if (!nonFatalWarningsFor(name.value)) Seq("-Xfatal-warnings") Compile / scalacOptions ++= (
else Seq.empty if (!nonFatalWarningsFor(name.value)) Seq("-Xfatal-warnings")
), else Seq.empty
Test / scalacOptions --= testUndicipline, ),
Compile / scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match { Test / scalacOptions --= testUndicipline,
case Some((2, 13)) => Compile / scalacOptions ++= (CrossVersion.partialVersion(scalaVersion.value) match {
disciplineScalacOptions -- Set( case Some((2, 13)) =>
"-Ywarn-inaccessible", disciplineScalacOptions -- Set(
"-Ywarn-infer-any", "-Ywarn-inaccessible",
"-Ywarn-nullary-override", "-Ywarn-infer-any",
"-Ywarn-nullary-unit", "-Ywarn-nullary-override",
"-Ypartial-unification", "-Ywarn-nullary-unit",
"-Yno-adapted-args") "-Ypartial-unification",
case Some((2, 12)) => "-Yno-adapted-args")
disciplineScalacOptions case Some((2, 12)) =>
case _ => disciplineScalacOptions
Nil case _ =>
}).toSeq, Nil
Compile / scalacOptions --= }).toSeq,
(if (looseProjects.contains(name.value)) undisciplineScalacOptions.toSeq Compile / scalacOptions --=
else Seq.empty), (if (looseProjects.contains(name.value)) undisciplineScalacOptions.toSeq
// Discipline is not needed for the docs compilation run (which uses else Seq.empty),
// different compiler phases from the regular run), and in particular // Discipline is not needed for the docs compilation run (which uses
// '-Ywarn-unused:explicits' breaks 'sbt ++2.13.0-M5 akka-actor/doc' // different compiler phases from the regular run), and in particular
// https://github.com/akka/akka/issues/26119 // '-Ywarn-unused:explicits' breaks 'sbt ++2.13.0-M5 akka-actor/doc'
Compile / doc / scalacOptions --= disciplineScalacOptions.toSeq :+ "-Xfatal-warnings", // https://github.com/akka/akka/issues/26119
// having discipline warnings in console is just an annoyance Compile / doc / scalacOptions --= disciplineScalacOptions.toSeq :+ "-Xfatal-warnings",
Compile / console / scalacOptions --= disciplineScalacOptions.toSeq) // having discipline warnings in console is just an annoyance
Compile / console / scalacOptions --= disciplineScalacOptions.toSeq)
} else {
// we still need these in opt-out since the annotations are present
silencerSettings ++ Seq(Compile / scalacOptions += "-deprecation")
}
val testUndicipline = Seq( val testUndicipline = Seq("-Ywarn-dead-code" // '???' used in compile only specs
"-Ywarn-dead-code", // '???' used in compile only specs
) )
/** /**