Skip to content

Commit

Permalink
Add warning about 'main.sc' used, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Nov 17, 2023
1 parent ae24ac2 commit a871cc7
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 11 deletions.
4 changes: 2 additions & 2 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ object Build {

val scopedSources = value(crossSources.scopedSources(baseOptions))

val mainSources = value(scopedSources.sources(Scope.Main, baseOptions, allInputs.workspace))
val mainSources = value(scopedSources.sources(Scope.Main, baseOptions, allInputs.workspace, logger))
val mainOptions = mainSources.buildOptions

val testSources = value(scopedSources.sources(Scope.Test, baseOptions, allInputs.workspace))
val testSources = value(scopedSources.sources(Scope.Test, baseOptions, allInputs.workspace, logger))
val testOptions = testSources.buildOptions

val inputs0 = updateInputs(
Expand Down
12 changes: 11 additions & 1 deletion modules/build/src/main/scala/scala/build/ScopedSources.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import java.nio.charset.StandardCharsets
import scala.build.EitherCps.{either, value}
import scala.build.errors.BuildException
import scala.build.info.{BuildInfo, ScopedBuildInfo}
import scala.build.internal.util.WarningMessages
import scala.build.internal.AppCodeWrapper
import scala.build.options.{BuildOptions, HasScope, Scope}
import scala.build.preprocessing.ScriptPreprocessor

Expand Down Expand Up @@ -55,7 +57,8 @@ final case class ScopedSources(
def sources(
scope: Scope,
baseOptions: BuildOptions,
workspace: os.Path
workspace: os.Path,
logger: Logger
): Either[BuildException, Sources] = either {
val combinedOptions = combinedBuildOptions(scope, baseOptions)

Expand All @@ -65,6 +68,13 @@ final case class ScopedSources(
.flatMap(_.valueFor(scope).toSeq)
.map(_.wrap(codeWrapper))

codeWrapper match {
case _:AppCodeWrapper.type if wrappedScripts.size > 1 =>
wrappedScripts.find(_.originalPath.exists(_._1.toString == "main.sc"))
.foreach(_ => logger.diagnostic(WarningMessages.mainScriptNameClashesWithAppWrapper))
case _ => ()
}

val defaultMainClass = defaultMainElemPath.flatMap { mainElemPath =>
wrappedScripts.collectFirst {
case Sources.InMemory(Right((_, path)), _, _, Some(wrapperParams))
Expand Down
4 changes: 2 additions & 2 deletions modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ final class BspImpl(
pprint.err.log(scopedSources)

val sourcesMain = value {
scopedSources.sources(Scope.Main, sharedOptions, allInputs.workspace)
scopedSources.sources(Scope.Main, sharedOptions, allInputs.workspace, persistentLogger)
.left.map((_, Scope.Main))
}

val sourcesTest = value {
scopedSources.sources(Scope.Test, sharedOptions, allInputs.workspace)
scopedSources.sources(Scope.Test, sharedOptions, allInputs.workspace, persistentLogger)
.left.map((_, Scope.Test))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,7 @@ object WarningMessages {
|$recommendedMsg
|""".stripMargin
}

val mainScriptNameClashesWithAppWrapper =
"Script file named 'main.sc' detected, keep in mind that accessing it from other scripts is impossible due to a clash of `main` symbols"
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ object DependencyUpdate extends ScalaCommand[DependencyUpdateOptions] {

def generateActionableUpdateDiagnostic(scope: Scope)
: Seq[ActionableDependencyUpdateDiagnostic] = {
val sources = scopedSources.sources(scope, sharedOptions, inputs.workspace).orExit(logger)
val sources = scopedSources.sources(scope, sharedOptions, inputs.workspace, logger).orExit(logger)

if (verbosity >= 3)
pprint.err.log(sources)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ object Export extends ScalaCommand[ExportOptions] {

val scopedSources: ScopedSources = value(crossSources.scopedSources(buildOptions))
val sources: Sources =
scopedSources.sources(scope, crossSources.sharedOptions(buildOptions), inputs.workspace)
scopedSources.sources(scope, crossSources.sharedOptions(buildOptions), inputs.workspace, logger)
.orExit(logger)

if (verbosity >= 3)
Expand Down
4 changes: 2 additions & 2 deletions modules/cli/src/main/scala/scala/cli/commands/fix/Fix.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ object Fix extends ScalaCommand[FixOptions] {
val sharedOptions = crossSources.sharedOptions(buildOptions)
val scopedSources = crossSources.scopedSources(sharedOptions).orExit(logger)

val mainSources = scopedSources.sources(Scope.Main, sharedOptions, inputs.workspace)
val testSources = scopedSources.sources(Scope.Test, sharedOptions, inputs.workspace)
val mainSources = scopedSources.sources(Scope.Main, sharedOptions, inputs.workspace, logger)
val testSources = scopedSources.sources(Scope.Test, sharedOptions, inputs.workspace, logger)

(mainSources, testSources).traverseN
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object PublishSetup extends ScalaCommand[PublishSetupOptions] {

val crossSourcesSharedOptions = crossSources.sharedOptions(cliBuildOptions)
val scopedSources = crossSources.scopedSources(crossSourcesSharedOptions).orExit(logger)
val sources = scopedSources.sources(Scope.Main, crossSourcesSharedOptions, inputs.workspace)
val sources = scopedSources.sources(Scope.Main, crossSourcesSharedOptions, inputs.workspace, logger)
.orExit(logger)

val pureJava = sources.hasJava && !sources.hasScala
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
val mainSources = value(scopedSources.sources(
Scope.Main,
crossSources.sharedOptions(options),
allInputs.workspace
allInputs.workspace,
logger
))

mainSources.buildOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
root
).out.trim()
expect(output == message)
expect(!output.contains("Script file named 'main.sc' detected, keep in mind that accessing it"))
}
}

Expand Down Expand Up @@ -80,9 +81,30 @@ trait RunScriptTestDefinitions { _: RunTestDefinitions =>
root
).out.trim()
expect(output == message)
expect(!output.contains("Script file named 'main.sc' detected, keep in mind that accessing it"))
}
}

test("warn when main.sc file is used together with other scripts") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "message.sc" ->
s"""println(main.msg)
|""".stripMargin,
os.rel / "main.sc" ->
s"""def msg = "$message"
|""".stripMargin
)
inputs.fromRoot { root =>
val res = os.proc(TestUtil.cli, "--power", extraOptions, "message.sc", "main.sc", "--object-wrapper")
.call(cwd = root, check = false, mergeErrIntoOut = true)

expect(res.exitCode == 1)
val output = res.out.trim()
expect(output.contains("Script file named 'main.sc' detected, keep in mind that accessing it"))
}
}

test("Directory") {
val message = "Hello"
val inputs = TestInputs(
Expand Down

0 comments on commit a871cc7

Please sign in to comment.