@@ -71,6 +71,12 @@ object ScalafmtPlugin extends AutoPlugin {
71
71
val scalafmtFilter = settingKey[String ](
72
72
" File filtering mode when running scalafmt."
73
73
)
74
+ val scalafmtLogOnEachError = settingKey[Boolean ](
75
+ " Enables logging on an error."
76
+ )
77
+ val scalafmtFailOnErrors = settingKey[Boolean ](
78
+ " Controls whether to fail in case of formatting errors."
79
+ )
74
80
}
75
81
76
82
import autoImport ._
@@ -127,13 +133,13 @@ object ScalafmtPlugin extends AutoPlugin {
127
133
resolvers : Seq [Resolver ],
128
134
currentProject : ResolvedProject ,
129
135
filterMode : String ,
130
- detailedErrorEnabled : Boolean
136
+ errorHandling : ErrorHandling
131
137
) {
132
138
private val log = taskStreams.log
133
139
private val reporter = new ScalafmtSbtReporter (
134
140
log,
135
141
new OutputStreamWriter (taskStreams.binary()),
136
- detailedErrorEnabled
142
+ errorHandling
137
143
)
138
144
139
145
private val scalafmtSession = {
@@ -179,18 +185,28 @@ object ScalafmtPlugin extends AutoPlugin {
179
185
180
186
private def withFormattedSources [T ](sources : Seq [File ])(
181
187
onFormat : (File , Input , Output ) => T
182
- ): Seq [Option [T ]] =
183
- sources.map { file =>
188
+ ): Seq [Option [T ]] = {
189
+ val res = sources.map { file =>
184
190
val path = file.toPath.toAbsolutePath
185
191
Try (IO .read(file)) match {
186
192
case Failure (x) =>
187
193
reporter.error(path, " Failed to read" , x)
188
194
None
189
195
case Success (x) =>
190
- val output = scalafmtSession.format(path, x)
191
- Some (onFormat(file, x, output))
196
+ val output = scalafmtSession.formatOrError(path, x)
197
+ /* no need to report on exception since for all errors
198
+ * reporter.error would have been called already */
199
+ Option (output.value).map(o => onFormat(file, x, o))
192
200
}
193
201
}
202
+ val bad = res.count(_ eq None )
203
+ if (bad != 0 ) {
204
+ val err = s " scalafmt: failed for $bad sources "
205
+ if (errorHandling.failOnErrors) throw new MessageOnlyException (err)
206
+ log.error(err)
207
+ }
208
+ res
209
+ }
194
210
195
211
def formatTrackedSources (
196
212
cacheStoreFactory : CacheStoreFactory ,
@@ -413,7 +429,11 @@ object ScalafmtPlugin extends AutoPlugin {
413
429
fullResolvers.value,
414
430
thisProject.value,
415
431
scalafmtFilter.value,
416
- scalafmtDetailedError.value
432
+ new ErrorHandling (
433
+ scalafmtLogOnEachError.value,
434
+ scalafmtFailOnErrors.value,
435
+ scalafmtDetailedError.value
436
+ )
417
437
)
418
438
func(files, session)
419
439
}
@@ -453,7 +473,11 @@ object ScalafmtPlugin extends AutoPlugin {
453
473
fullResolvers.value,
454
474
thisProject.value,
455
475
" " ,
456
- scalafmtDetailedError.value
476
+ new ErrorHandling (
477
+ scalafmtLogOnEachError.value,
478
+ scalafmtFailOnErrors.value,
479
+ scalafmtDetailedError.value
480
+ )
457
481
).formatSources(absFiles)
458
482
}
459
483
)
@@ -480,6 +504,8 @@ object ScalafmtPlugin extends AutoPlugin {
480
504
Seq (
481
505
scalafmtFilter := " " ,
482
506
scalafmtOnCompile := false ,
507
+ scalafmtLogOnEachError := false ,
508
+ scalafmtFailOnErrors := true ,
483
509
scalafmtDetailedError := false
484
510
)
485
511
0 commit comments