Skip to content

Commit 081f009

Browse files
authored
Merge pull request #3646 from Gedochao/feature/fix-tweaks
`fix` sub-command tweaks
2 parents 79b1b56 + 8faa13e commit 081f009

File tree

13 files changed

+111
-57
lines changed

13 files changed

+111
-57
lines changed

modules/cli/src/main/scala/scala/cli/commands/compile/Compile.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ object Compile extends ScalaCommand[CompileOptions] with BuildCommandHelpers {
102102
configDb.get(Keys.actions).getOrElse(None)
103103
)
104104

105+
val shouldBuildTestScope = options.shared.scope.test.getOrElse(false)
105106
if (options.watch.watchMode) {
106107
val watcher = Build.watch(
107108
inputs,
@@ -110,7 +111,7 @@ object Compile extends ScalaCommand[CompileOptions] with BuildCommandHelpers {
110111
None,
111112
logger,
112113
crossBuilds = cross,
113-
buildTests = options.shared.scope.test,
114+
buildTests = shouldBuildTestScope,
114115
partial = None,
115116
actionableDiagnostics = actionableDiagnostics,
116117
postAction = () => WatchUtil.printWatchMessage()
@@ -129,7 +130,7 @@ object Compile extends ScalaCommand[CompileOptions] with BuildCommandHelpers {
129130
None,
130131
logger,
131132
crossBuilds = cross,
132-
buildTests = options.shared.scope.test,
133+
buildTests = shouldBuildTestScope,
133134
partial = None,
134135
actionableDiagnostics = actionableDiagnostics
135136
)

modules/cli/src/main/scala/scala/cli/commands/dependencyupdate/DependencyUpdate.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ object DependencyUpdate extends ScalaCommand[DependencyUpdateOptions] {
2525
args: RemainingArgs,
2626
logger: Logger
2727
): Unit = {
28-
if options.shared.scope.test then
28+
if options.shared.scope.test.nonEmpty then
2929
logger.message(
3030
s"""$warnPrefix Including the test scope does not change the behaviour of this command.
3131
|$warnPrefix Test dependencies are updated regardless.""".stripMargin

modules/cli/src/main/scala/scala/cli/commands/doc/Doc.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ object Doc extends ScalaCommand[DocOptions] {
5252
configDb.get(Keys.actions).getOrElse(None)
5353
)
5454

55-
val withTestScope = options.shared.scope.test
55+
val withTestScope = options.shared.scope.test.getOrElse(false)
5656
Build.build(
5757
inputs,
5858
initialBuildOptions,

modules/cli/src/main/scala/scala/cli/commands/export0/Export.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ object Export extends ScalaCommand[ExportOptions] {
136136
override def sharedOptions(opts: ExportOptions): Option[SharedOptions] = Some(opts.shared)
137137

138138
override def runCommand(options: ExportOptions, args: RemainingArgs, logger: Logger): Unit = {
139-
if options.shared.scope.test then {
139+
if options.shared.scope.test.nonEmpty then {
140140
logger.error(
141141
s"""Including the test scope sources together with the main scope is currently not supported.
142142
|Note that test scope sources will still be exported as per the output build tool tests definition demands.""".stripMargin

modules/cli/src/main/scala/scala/cli/commands/fix/ScalafixRules.scala

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import scala.build.compiler.ScalaCompilerMaker
1010
import scala.build.errors.BuildException
1111
import scala.build.input.{Inputs, ScalaCliInvokeData}
1212
import scala.build.internal.{Constants, Runner}
13+
import scala.build.internals.ConsoleUtils.ScalaCliConsole.warnPrefix
1314
import scala.build.options.{BuildOptions, Scope}
1415
import scala.build.{Build, Logger, Os, ScalafixArtifacts}
1516
import scala.cli.commands.fix.ScalafixOptions
@@ -32,24 +33,44 @@ object ScalafixRules extends CommandHelpers {
3233
actionableDiagnostics: Option[Boolean],
3334
logger: Logger
3435
)(using ScalaCliInvokeData): Either[BuildException, Int] = {
35-
val buildOptionsWithSemanticDb = buildOptions.copy(scalaOptions =
36-
buildOptions.scalaOptions.copy(semanticDbOptions =
37-
buildOptions.scalaOptions.semanticDbOptions.copy(generateSemanticDbs = Some(true))
38-
)
39-
)
40-
41-
val scalaVersion =
42-
buildOptions.scalaParams.orExit(logger).map(_.scalaVersion)
43-
.getOrElse(Constants.defaultScalaVersion)
36+
sharedOptions.semanticDbOptions.semanticDb match {
37+
case Some(false) =>
38+
logger.message(
39+
s"""$warnPrefix SemanticDB files' generation was explicitly set to false.
40+
|$warnPrefix Some scalafix rules require .semanticdb files and may not work properly."""
41+
.stripMargin
42+
)
43+
case Some(true) =>
44+
logger.debug("SemanticDB files' generation enabled.")
45+
case None =>
46+
logger.debug("Defaulting SemanticDB files' generation to true, to satisfy scalafix needs.")
47+
}
48+
val buildOptionsWithSemanticDb =
49+
if buildOptions.scalaOptions.semanticDbOptions.generateSemanticDbs.isEmpty then
50+
buildOptions.copy(scalaOptions =
51+
buildOptions.scalaOptions.copy(semanticDbOptions =
52+
buildOptions.scalaOptions.semanticDbOptions.copy(generateSemanticDbs =
53+
Some(true)
54+
)
55+
)
56+
)
57+
else buildOptions
4458

59+
val shouldBuildTestScope = sharedOptions.scope.test.getOrElse(true)
60+
if !shouldBuildTestScope then
61+
logger.message(
62+
s"""$warnPrefix Building test scope was explicitly disabled.
63+
|$warnPrefix Some scalafix rules may not work correctly with test scope inputs."""
64+
.stripMargin
65+
)
4566
val res = Build.build(
4667
inputs,
4768
buildOptionsWithSemanticDb,
4869
compilerMaker,
4970
None,
5071
logger,
5172
crossBuilds = false,
52-
buildTests = true,
73+
buildTests = shouldBuildTestScope,
5374
partial = None,
5475
actionableDiagnostics = actionableDiagnostics
5576
)
@@ -64,6 +85,13 @@ object ScalafixRules extends CommandHelpers {
6485
successfulBuilds.headOption.toSeq
6586
.flatMap(_.options.scalaOptions.scalacOptions.toSeq.map(_.value.value))
6687

88+
val scalaVersion = {
89+
for {
90+
b <- successfulBuilds.headOption
91+
scalaParams <- b.scalaParams
92+
} yield scalaParams.scalaVersion
93+
}.getOrElse(Constants.defaultScalaVersion)
94+
6795
either {
6896
val artifacts =
6997
value(

modules/cli/src/main/scala/scala/cli/commands/fmt/Fmt.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ object Fmt extends ScalaCommand[FmtOptions] {
4343
override def runCommand(options: FmtOptions, args: RemainingArgs, logger: Logger): Unit = {
4444
val buildOptions = buildOptionsOrExit(options)
4545

46-
if options.shared.scope.test then
46+
if options.shared.scope.test.nonEmpty then
4747
logger.message(
4848
s"""$warnPrefix Including the test scope does not change the behaviour of this command.
4949
|$warnPrefix Test scope inputs are formatted, regardless.""".stripMargin

modules/cli/src/main/scala/scala/cli/commands/package0/Package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ object Package extends ScalaCommand[PackageOptions] with BuildCommandHelpers {
8888
configDb.get(Keys.actions).getOrElse(None)
8989
)
9090

91-
val withTestScope = options.shared.scope.test
91+
val withTestScope = options.shared.scope.test.getOrElse(false)
9292
if options.watch.watchMode then {
9393
var expectedModifyEpochSecondOpt = Option.empty[Long]
9494
val watcher = Build.watch(

modules/cli/src/main/scala/scala/cli/commands/publish/Publish.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ object Publish extends ScalaCommand[PublishOptions] with BuildCommandHelpers {
266266
() => configDb,
267267
options.mainClass,
268268
dummy = options.sharedPublish.dummy,
269-
buildTests = options.shared.scope.test
269+
buildTests = options.shared.scope.test.getOrElse(false)
270270
)
271271
}
272272

modules/cli/src/main/scala/scala/cli/commands/publish/PublishLocal.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ object PublishLocal extends ScalaCommand[PublishLocalOptions] {
8686
configDb = () => ConfigDb.empty, // shouldn't be used, no need of repo credentials here
8787
mainClassOptions = options.mainClass,
8888
dummy = options.sharedPublish.dummy,
89-
buildTests = options.shared.scope.test
89+
buildTests = options.shared.scope.test.getOrElse(false)
9090
)
9191
}
9292
}

modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,11 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
190190
configDb.get(Keys.actions).getOrElse(None)
191191
)
192192

193+
val shouldBuildTestScope = options.shared.scope.test.getOrElse(false)
193194
if (inputs.isEmpty) {
194195
val allArtifacts =
195196
Seq(initialBuildOptions.artifacts(logger, Scope.Main).orExit(logger)) ++
196-
(if options.shared.scope.test
197+
(if shouldBuildTestScope
197198
then Seq(initialBuildOptions.artifacts(logger, Scope.Test).orExit(logger))
198199
else Nil)
199200
// synchronizing, so that multiple presses to enter (handled by WatchUtil.waitForCtrlC)
@@ -224,7 +225,7 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
224225
None,
225226
logger,
226227
crossBuilds = cross,
227-
buildTests = options.shared.scope.test,
228+
buildTests = shouldBuildTestScope,
228229
partial = None,
229230
actionableDiagnostics = actionableDiagnostics,
230231
postAction = () => WatchUtil.printWatchMessage()
@@ -252,7 +253,7 @@ object Repl extends ScalaCommand[ReplOptions] with BuildCommandHelpers {
252253
None,
253254
logger,
254255
crossBuilds = cross,
255-
buildTests = options.shared.scope.test,
256+
buildTests = shouldBuildTestScope,
256257
partial = None,
257258
actionableDiagnostics = actionableDiagnostics
258259
)

0 commit comments

Comments
 (0)