-
Notifications
You must be signed in to change notification settings - Fork 315
/
ScalafixProvider.scala
688 lines (633 loc) Β· 21.9 KB
/
ScalafixProvider.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
package scala.meta.internal.metals
import java.net.URLClassLoader
import java.nio.file.Files
import java.nio.file.Path
import java.{util => ju}
import scala.collection.concurrent.TrieMap
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.util.Failure
import scala.util.Random
import scala.util.Success
import scala.util.Try
import scala.meta._
import scala.meta.internal.metals.MetalsEnrichments._
import scala.meta.internal.metals.clients.language.MetalsLanguageClient
import scala.meta.internal.metals.clients.language.MetalsQuickPickItem
import scala.meta.internal.metals.clients.language.MetalsQuickPickParams
import scala.meta.internal.metals.{BuildInfo => V}
import scala.meta.internal.mtags.SemanticdbClasspath
import scala.meta.internal.semanticdb.TextDocuments
import scala.meta.io.AbsolutePath
import com.typesafe.config.ConfigFactory
import coursierapi.Dependency
import coursierapi.Repository
import org.eclipse.lsp4j.MessageParams
import org.eclipse.lsp4j.MessageType
import org.eclipse.{lsp4j => l}
import scalafix.interfaces.Scalafix
import scalafix.interfaces.ScalafixEvaluation
import scalafix.interfaces.ScalafixFileEvaluationError
import scalafix.internal.interfaces.ScalafixCoursier
import scalafix.internal.interfaces.ScalafixInterfacesClassloader
case class ScalafixProvider(
buffers: Buffers,
userConfig: () => UserConfiguration,
workspace: AbsolutePath,
statusBar: StatusBar,
compilations: Compilations,
languageClient: MetalsLanguageClient,
buildTargets: BuildTargets,
buildClient: MetalsBuildClient,
interactive: InteractiveSemanticdbs,
)(implicit ec: ExecutionContext) {
import ScalafixProvider._
private val scalafixCache = TrieMap.empty[ScalaBinaryVersion, Scalafix]
private val rulesClassloaderCache =
TrieMap.empty[ScalafixRulesClasspathKey, URLClassLoader]
// Warms up the Scalafix instance so that the first organize imports request responds faster.
def load(): Unit = {
if (!Testing.isEnabled) {
val tmp = workspace
.resolve(Directories.tmp)
.resolve(s"Main${Random.nextLong()}.scala")
try {
val targets =
buildTargets.allScala.toList.groupBy(_.scalaVersion).flatMap {
case (_, targets) => targets.headOption
}
val contents = "object Main{}\n"
tmp.writeText(contents)
val testEvaluation =
for (target <- targets)
yield scalafixEvaluate(
tmp,
target,
contents,
produceSemanticdb = true,
List(organizeImportRuleName),
)
val evaluatedCorrectly = testEvaluation.forall {
case Success(evaluation) => evaluation.isSuccessful()
case _ => false
}
if (!evaluatedCorrectly) scribe.debug("Could not warm up Scalafix")
} catch {
case e: Throwable =>
scribe.debug(
s"Scalafix issue while warming up due to issue: ${e.getMessage()}",
e,
)
} finally {
if (tmp.exists) tmp.delete()
}
}
}
def runAllRules(file: AbsolutePath): Future[List[l.TextEdit]] = {
val definedRules = rulesFromScalafixConf()
runRules(file, definedRules.toList)
}
def runRulesOrPrompt(
file: AbsolutePath,
rules: List[String],
): Future[List[l.TextEdit]] = {
val definedRules = rulesFromScalafixConf()
val rulesFut =
if (rules.isEmpty) askForRule(definedRules).map(_.toList)
else Future.successful(rules)
rulesFut.flatMap(runRules(file, _))
}
def organizeImports(
file: AbsolutePath,
scalaTarget: ScalaTarget,
): Future[List[l.TextEdit]] = {
runScalafixRules(
file,
scalaTarget,
List(organizeImportRuleName),
)
}
def runScalafixRules(
file: AbsolutePath,
scalaTarget: ScalaTarget,
rules: List[String],
retried: Boolean = false,
): Future[List[l.TextEdit]] = {
val fromDisk = file.toInput
val inBuffers = file.toInputFromBuffers(buffers)
compilations.compilationFinished(file).flatMap { _ =>
val scalafixEvaluation =
scalafixEvaluate(
file,
scalaTarget,
inBuffers.value,
retried || isUnsaved(inBuffers.text, fromDisk.text),
rules,
)
scalafixEvaluation match {
case Failure(exception) =>
reportScalafixError(
"Unable to run scalafix, please check logs for more info.",
exception,
)
Future.failed(exception)
case Success(results)
if !scalafixSucceded(results) && hasStaleOrMissingSemanticdb(
results
) && buildClient.buildHasErrors(file) =>
val msg = "Attempt to organize your imports failed. " +
"It looks like you have compilation issues causing your semanticdb to be stale. " +
"Ensure everything is compiling and try again."
scribe.warn(
msg
)
languageClient.showMessage(
MessageType.Warning,
msg,
)
Future.successful(Nil)
case Success(results) if !scalafixSucceded(results) =>
val scalafixError = getMessageErrorFromScalafix(results)
val exception = ScalafixRunException(scalafixError)
if (
scalafixError.startsWith("Unknown rule") ||
scalafixError.startsWith("Class not found")
) {
languageClient
.showMessage(Messages.unknownScalafixRules(scalafixError))
}
scribe.error(scalafixError, exception)
if (!retried && hasStaleOrMissingSemanticdb(results)) {
// Retry, since the semanticdb might be stale
runScalafixRules(file, scalaTarget, rules, retried = true)
} else {
Future.failed(exception)
}
case Success(results) =>
Future.successful {
val edits = for {
fileEvaluation <- results.getFileEvaluations().headOption
patches <- fileEvaluation.previewPatches().asScala
} yield textEditsFrom(patches, inBuffers)
edits.getOrElse(Nil)
}
}
}
}
private def createTemporarySemanticdb(
file: AbsolutePath,
contents: String,
) = {
interactive
.textDocument(file, Some(contents))
.documentIncludingStale
.flatMap { semanticdb =>
/* We remove all diagnostics if there is an error so that
* we don't remove an import by mistake, which just has a typo
* for example and would produce an unsued warning.
* Without additional diagnotics imports will only get rearranged.
*/
val toSave =
if (semanticdb.diagnostics.exists(_.severity.isError))
semanticdb.withDiagnostics(Seq.empty)
else
semanticdb
val dir = workspace.resolve(Directories.tmp)
file.toRelativeInside(workspace).flatMap { relativePath =>
val writeTo =
dir.resolve(SemanticdbClasspath.fromScalaOrJava(relativePath))
writeTo.parent.createDirectories()
val docs = TextDocuments(Seq(toSave))
Files.write(writeTo.toNIO, docs.toByteArray)
Option(dir.toNIO)
}
}
}
/**
* Scalafix may be ran successfully, but that doesn't mean that every file
* evaluation also ran succesfully. This ensure that the scalafix run was successful
* and also that every file evaluation was successful.
*
* @param evaluation
* @return true only if the evaulation for every single file contains no errors
*/
private def scalafixSucceded(evaluation: ScalafixEvaluation): Boolean =
evaluation.isSuccessful && evaluation
.getFileEvaluations()
.forall(_.isSuccessful)
private def hasStaleOrMissingSemanticdb(
evaluation: ScalafixEvaluation
): Boolean = {
val error = evaluation
.getFileEvaluations()
.headOption
.flatMap(_.getError().asScala)
error.contains(ScalafixFileEvaluationError.StaleSemanticdbError) || error
.contains(ScalafixFileEvaluationError.MissingSemanticdbError)
}
/**
* Assumes that [[ScalafixProvider.scalafixSucceded]] has been called and
* returned false
*
* @param evaluation
* @return the error message of the evaluation or file evaluation
*/
private def getMessageErrorFromScalafix(
evaluation: ScalafixEvaluation
): String = {
(if (!evaluation.isSuccessful())
evaluation.getErrorMessage().asScala
else
evaluation
.getFileEvaluations()
.headOption
.flatMap(_.getErrorMessage().asScala))
.getOrElse("Unexpected error while running Scalafix.")
}
private lazy val scala3DefaultConfig = {
val path = Files.createTempFile(".scalafix", ".conf")
AbsolutePath(path).writeText(
s"""|rules = [
| OrganizeImports
|]
|OrganizeImports.removeUnused = false
|OrganizeImports.targetDialect = Scala3
|
|""".stripMargin
)
path.toFile().deleteOnExit()
path
}
private def scalafixConf(isScala3: Boolean): Option[Path] = {
val defaultLocation = workspace.resolve(".scalafix.conf")
val defaultConfig = if (isScala3) Some(scala3DefaultConfig) else None
userConfig().scalafixConfigPath match {
case Some(path) if !path.isFile && defaultLocation.isFile =>
languageClient.showMessage(
MessageType.Warning,
s"No configuration at $path, using default at $defaultLocation.",
)
Some(defaultLocation.toNIO)
case Some(path) if !path.isFile =>
languageClient.showMessage(
MessageType.Warning,
s"No configuration at $path, using Scalafix defaults.",
)
defaultConfig
case Some(path) => Some(path.toNIO)
case None if defaultLocation.isFile =>
Some(defaultLocation.toNIO)
case _ => defaultConfig
}
}
private def rulesFromScalafixConf(): Set[String] = {
scalafixConf(isScala3 = false) match {
case None => Set.empty
case Some(configPath) =>
val conf = ConfigFactory.parseFile(configPath.toFile)
if (conf.hasPath("rules"))
conf
.getList("rules")
.map { item =>
item.unwrapped().toString()
}
.asScala
.toSet
else Set.empty
}
}
/**
* Tries to use the Scalafix rule to organize imports.
*
* @param file file to run the rule on
* @param scalaTarget target with all the data about the module
* @param inBuffers file version that might not be saved to disk
* @param produceSemanticdb when set to true, we will try to create semanticdb and
* save to disk for Scalafix to use. This make organize imports work even if the file is
* unsaved. This however requires us to save both the file and semanticdb.
* @return
*/
private def scalafixEvaluate(
file: AbsolutePath,
scalaTarget: ScalaTarget,
inBuffers: String,
produceSemanticdb: Boolean,
rules: List[String],
): Try[ScalafixEvaluation] = {
val isScala3 = ScalaVersions.isScala3Version(scalaTarget.scalaVersion)
val scalaBinaryVersion =
if (isScala3) "2.13" else scalaTarget.scalaBinaryVersion
val targetRoot =
if (produceSemanticdb) createTemporarySemanticdb(file, inBuffers)
else
Some(scalaTarget.targetroot.toNIO)
val sourceroot =
if (produceSemanticdb)
targetRoot.map(AbsolutePath(_)).getOrElse(workspace)
else workspace
val diskFilePath = if (produceSemanticdb) {
file
.toRelativeInside(workspace)
.map { relativePath =>
val tempFilePath = sourceroot.resolve(relativePath)
tempFilePath.writeText(inBuffers)
tempFilePath
}
.getOrElse(file)
} else {
file
}
val scalaVersion = scalaTarget.scalaVersion
val scalafixRulesKey =
ScalafixRulesClasspathKey(
scalaBinaryVersion,
scalaVersion,
userConfig(),
rules,
)
// It seems that Scalafix ignores the targetroot parameter and searches the classpath
// Prepend targetroot to make sure that it's picked up first always
val classpath =
(targetRoot.toList ++ scalaTarget.fullClasspath).asJava
val isSource3 = scalaTarget.scalac.getOptions().contains("-Xsource:3")
for {
api <- getScalafix(scalaBinaryVersion)
urlClassLoaderWithExternalRule <- getRuleClassLoader(
scalafixRulesKey,
api.getClass.getClassLoader,
)
} yield {
val scalacOptions = {
val list = new ju.ArrayList[String](3)
if (scalaBinaryVersion == "2.13") list.add("-Wunused:imports")
else list.add("-Ywarn-unused-import")
if (!isScala3 && isSource3)
list.add("-Xsource:3")
// We always compile with synthetics:on but scalafix will fail if we don't set it here
list.add("-P:semanticdb:synthetics:on")
list
}
val evaluated = api
.newArguments()
.withScalaVersion(scalaVersion)
.withClasspath(classpath)
.withToolClasspath(urlClassLoaderWithExternalRule)
.withConfig(scalafixConf(isScala3 || isSource3).asJava)
.withRules(rules.asJava)
.withPaths(List(diskFilePath.toNIO).asJava)
.withSourceroot(sourceroot.toNIO)
.withScalacOptions(scalacOptions)
.evaluate()
if (produceSemanticdb)
targetRoot.foreach(AbsolutePath(_).deleteRecursively())
evaluated
}
}
private def reportScalafixError(
message: String,
exception: Throwable,
): Unit = {
val params = new MessageParams(MessageType.Error, message)
scribe.error(message, exception)
languageClient.showMessage(params)
}
private def textEditsFrom(
newFileContent: String,
input: Input,
): List[l.TextEdit] = {
val fullDocumentRange = Position.Range(input, 0, input.chars.length).toLsp
if (newFileContent != input.text) {
List(new l.TextEdit(fullDocumentRange, newFileContent))
} else {
Nil
}
}
private def getScalafix(
scalaBinaryVersion: ScalaBinaryVersion
): Try[Scalafix] = {
scalafixCache.get(scalaBinaryVersion) match {
case Some(value) => Success(value)
case None =>
statusBar.trackBlockingTask("Downloading scalafix") {
val scalafix =
if (scalaBinaryVersion == "2.11") Try(scala211Fallback)
else
Try(Scalafix.fetchAndClassloadInstance(scalaBinaryVersion))
scalafix.foreach(api => scalafixCache.update(scalaBinaryVersion, api))
scalafix
}
}
}
private def scala211Fallback: Scalafix = {
// last version that supports Scala 2.11.12
val latestSupporting = "0.10.4"
val jars = ScalafixCoursier.scalafixCliJars(
Repository.defaults(),
latestSupporting,
V.scala211,
)
val parent = new ScalafixInterfacesClassloader(
classOf[Scalafix].getClassLoader()
);
Scalafix.classloadInstance(
new URLClassLoader(jars.asScala.toArray, parent)
);
}
private def getRuleClassLoader(
scalfixRulesKey: ScalafixRulesClasspathKey,
scalafixClassLoader: ClassLoader,
): Try[URLClassLoader] = {
rulesClassloaderCache.get(scalfixRulesKey) match {
case Some(value) => Success(value)
case None =>
statusBar.trackBlockingTask(
"Downloading scalafix rules' dependencies"
) {
val rulesDependencies = scalfixRulesKey.usedRulesWithClasspath
val organizeImportRule =
// Scalafix version that supports Scala 2.11 doesn't have the rule built in
if (scalfixRulesKey.scalaBinaryVersion == "2.11")
Some(
Dependency.of(
"com.github.liancheng",
"organize-imports_" + scalfixRulesKey.scalaBinaryVersion,
"0.6.0",
)
)
else None
val allRules =
Try(
Embedded.rulesClasspath(
rulesDependencies.toList ++ organizeImportRule
)
).map { paths =>
val classloader = Embedded.toClassLoader(
Classpath(paths.map(AbsolutePath(_))),
scalafixClassLoader,
)
rulesClassloaderCache.update(scalfixRulesKey, classloader)
classloader
}
allRules
}
}
}
private def isUnsaved(fromBuffers: String, fromFile: String): Boolean = {
// zipAll will extend the shorter collection, which is needed for accurate comparison
fromBuffers.linesIterator
.zipAll(fromFile.linesIterator, null, null)
.exists { case (line1, line2) =>
line1 != line2
}
}
private def askForRule(rules: Set[String]): Future[Option[String]] =
languageClient
.metalsQuickPick(
MetalsQuickPickParams(
items = rules.toList.map(r => MetalsQuickPickItem(r, r)).asJava,
placeHolder = "Rule",
)
)
.asScala
.map(resultOpt => resultOpt.map(_.itemId))
private def runRules(
file: AbsolutePath,
rules: List[String],
): Future[List[l.TextEdit]] = {
val result = for {
buildId <- buildTargets.inverseSources(file)
target <- buildTargets.scalaTarget(buildId)
} yield {
runScalafixRules(
file,
target,
rules,
)
}
result.getOrElse(Future.successful(Nil))
}
}
object ScalafixProvider {
type ScalaBinaryVersion = String
type ScalaVersion = String
case class ScalafixRulesClasspathKey(
scalaBinaryVersion: ScalaBinaryVersion,
usedRulesWithClasspath: Set[Dependency],
)
object ScalafixRulesClasspathKey {
def apply(
scalaBinaryVersion: String,
scalaVersion: String,
userConfig: UserConfiguration,
rules: List[String],
): ScalafixRulesClasspathKey = {
val rulesClasspath =
rulesDependencies(scalaVersion, scalaBinaryVersion, userConfig, rules)
ScalafixRulesClasspathKey(scalaBinaryVersion, rulesClasspath)
}
}
case class ScalafixRunException(msg: String) extends Exception(msg)
val organizeImportRuleName = "OrganizeImports"
def rulesDependencies(
scalaVersion: String,
scalaBinaryVersion: String,
userConfig: UserConfiguration,
rules: List[String],
): Set[Dependency] = {
val fromSettings = userConfig.scalafixRulesDependencies.flatMap {
dependencyString =>
Try {
Dependency.parse(
dependencyString,
coursierapi.ScalaVersion.of(scalaVersion),
)
} match {
case Failure(exception) =>
scribe.warn(s"Could not download `${dependencyString}`", exception)
None
case Success(dep) =>
Some(dep)
}
}
val builtInRuleDeps = builtInRules(scalaBinaryVersion)
val allDeps = fromSettings ++ rules.flatMap(builtInRuleDeps.get)
// only get newest versions for each dependency
allDeps
.sortBy(_.getVersion())
.reverse
.distinctBy(dep => dep.getModule())
.toSet
}
// Hygiene rules from https://scalacenter.github.io/scalafix/docs/rules/community-rules.html
private def builtInRules(binaryVersion: String) = {
val scaluzziDep = Dependency.of(
"com.github.vovapolu",
s"scaluzzi_$binaryVersion",
"latest.release",
)
val scalafixUnifiedDep = Dependency.of(
"com.github.xuwei-k",
s"scalafix-rules_$binaryVersion",
"latest.release",
)
val scalafixPixivRule = Dependency.of(
"net.pixiv",
s"scalafix-pixiv-rule_$binaryVersion",
"latest.release",
)
val depsList = List(
"EmptyCollectionsUnified" -> Dependency.of(
"io.github.ghostbuster91.scalafix-unified",
s"unified_$binaryVersion",
"latest.release",
),
"UseNamedParameters" -> Dependency.of(
"com.github.jatcwang",
s"scalafix-named-params_$binaryVersion",
"latest.release",
),
"MissingFinal" -> scaluzziDep,
"Disable" -> scaluzziDep,
"AddExplicitImplicitTypes" -> scalafixUnifiedDep,
"AddLambdaParamParentheses" -> scalafixUnifiedDep,
"CirceCodec" -> scalafixUnifiedDep,
"DirectoryAndPackageName" -> scalafixUnifiedDep,
"DuplicateWildcardImport" -> scalafixUnifiedDep,
"ExplicitImplicitTypes" -> scalafixUnifiedDep,
"FileNameConsistent" -> scalafixUnifiedDep,
"ImplicitValueClass" -> scalafixUnifiedDep,
"KindProjector" -> scalafixUnifiedDep,
"LambdaParamParentheses" -> scalafixUnifiedDep,
"NoElse" -> scalafixUnifiedDep,
"ObjectSelfType" -> scalafixUnifiedDep,
"RemoveEmptyObject" -> scalafixUnifiedDep,
"RemovePureEff" -> scalafixUnifiedDep,
"RemoveSamePackageImport" -> scalafixUnifiedDep,
"ReplaceSymbolLiterals" -> scalafixUnifiedDep,
"Scala3ImportRewrite" -> scalafixUnifiedDep,
"Scala3ImportWarn" -> scalafixUnifiedDep,
"Scala3Placeholder" -> scalafixUnifiedDep,
"ScalaApp" -> scalafixUnifiedDep,
"ScalazEitherInfix" -> scalafixUnifiedDep,
"SimplifyForYield" -> scalafixUnifiedDep,
"ThrowableToNonFatal" -> scalafixUnifiedDep,
"UnnecessaryCase" -> scalafixUnifiedDep,
"UnnecessaryMatch" -> scalafixUnifiedDep,
"UnnecessarySort" -> scalafixUnifiedDep,
"UnnecessarySortRewriteConfig" -> scalafixUnifiedDep,
"UnnecessarySortRewrite" -> scalafixUnifiedDep,
"UnusedConstructorParams" -> scalafixUnifiedDep,
"UnusedTypeParams" -> scalafixUnifiedDep,
"UnnecessarySemicolon" -> scalafixPixivRule,
"ZeroIndexToHead" -> scalafixPixivRule,
"CheckIsEmpty" -> scalafixPixivRule,
"NonCaseException" -> scalafixPixivRule,
"UnifyEmptyList" -> scalafixPixivRule,
"SingleConditionMatch" -> scalafixPixivRule,
)
val depsMaps = depsList.toMap
// make sure there are no duplicate rules
assert(depsMaps.size == depsList.size, "Found duplicate scalafix rules")
depsMaps
}
}