Skip to content

Commit

Permalink
Move mainClassObject function to CodeWrapper object
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejG604 committed Apr 28, 2023
1 parent 7a6a3f6 commit f70d88e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
11 changes: 10 additions & 1 deletion modules/build/src/main/scala/scala/build/bsp/BspClient.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scala.build.bsp

import ch.epfl.scala.{bsp4j => b}
import ch.epfl.scala.bsp4j as b

import java.lang.Boolean as JBoolean
import java.net.URI
Expand All @@ -10,6 +10,7 @@ import java.util.concurrent.{ConcurrentHashMap, ExecutorService}
import scala.build.Position.File
import scala.build.bsp.protocol.TextEdit
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, Severity}
import scala.build.internal.util.WarningMessages
import scala.build.postprocessing.LineConversion
import scala.build.{BloopBuildClient, GeneratedSource, Logger}
import scala.jdk.CollectionConverters.*
Expand Down Expand Up @@ -48,6 +49,14 @@ class BspClient(
val diag0 = diag.duplicate()
diag0.getRange.getStart.setLine(startLine)
diag0.getRange.getEnd.setLine(endLine)

if (
diag0.getMessage.contains(
"cannot be a main method since it cannot be accessed statically"
)
)
diag0.setMessage(WarningMessages.mainAnnotationNotSupported(""))

diag0
}
updatedDiagOpt.getOrElse(diag)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ package scala.build.internal
* Scala 3 feature 'export'<br> Incompatible with native JS members - the wrapper is a class
*/
case object ClassCodeWrapper extends CodeWrapper {
def mainClassObject(className: Name): Name =
Name(className.raw ++ "_sc")

private val userCodeNestingLevel = 1
def apply(
code: String,
Expand All @@ -17,7 +14,7 @@ case object ClassCodeWrapper extends CodeWrapper {
extraCode: String,
scriptPath: String
) = {
val name = mainClassObject(indexedWrapperName).backticked
val name = CodeWrapper.mainClassObject(indexedWrapperName).backticked
val wrapperClassName = Name(indexedWrapperName.raw ++ "$_").backticked
val mainObjectCode =
AmmUtil.normalizeNewlines(s"""|object $name {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ package scala.build.internal
* threads from script
*/
case object ObjectCodeWrapper extends CodeWrapper {
def mainClassObject(className: Name): Name =
Name(className.raw ++ "_sc")

private val userCodeNestingLevel = 1
def apply(
code: String,
Expand All @@ -16,7 +13,7 @@ case object ObjectCodeWrapper extends CodeWrapper {
extraCode: String,
scriptPath: String
) = {
val name = mainClassObject(indexedWrapperName).backticked
val name = CodeWrapper.mainClassObject(indexedWrapperName).backticked
val aliasedWrapperName = name + "$$alias"
val funHashCodeMethod =
if (name == "main_sc")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package scala.build.preprocessing

import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}
import scala.build.internal.CodeWrapper
import scala.build.options.{BuildOptions, BuildRequirements}
import scala.build.options.{BuildOptions, BuildRequirements, WithBuildRequirements}

sealed abstract class PreprocessedSource extends Product with Serializable {
def options: Option[BuildOptions]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ case object ScriptPreprocessor extends Preprocessor {
optionsWithTargetRequirements = processingOutput.optsWithReqs,
requirements = Some(processingOutput.globalReqs),
scopedRequirements = processingOutput.scopedReqs,
mainClassOpt = Some(CustomCodeWrapper.mainClassObject(Name(className)).backticked),
mainClassOpt = Some(CodeWrapper.mainClassObject(Name(className)).backticked),
scopePath = scopePath,
directivesPositions = processingOutput.directivesPositions
directivesPositions = processingOutput.directivesPositions,
wrapScriptFunOpt = Some(wrapScriptFun)
)
List(file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,18 @@ abstract class CodeWrapper {
val (topWrapper, bottomWrapper, userCodeNestingLevel) =
apply(code, pkgName, indexedWrapperName, extraCode0, scriptPath)
val (topWrapper0, bottomWrapper0) =
(topWrapper + "/*<script>*/\n", "/*</script>*/ /*<generated>*/" + bottomWrapper)
(
topWrapper + "/*<script>*/" + System.lineSeparator(),
"/*</script>*/ /*<generated>*/" + bottomWrapper
)
val importsLen = topWrapper0.length

(topWrapper0 + code + bottomWrapper0, importsLen, userCodeNestingLevel)
}

}

object CodeWrapper {
def mainClassObject(className: Name): Name =
Name(className.raw ++ "_sc")
}
Original file line number Diff line number Diff line change
Expand Up @@ -1311,6 +1311,63 @@ abstract class BspTestDefinitions(val scalaVersionOpt: Option[String])
}
}

if (actualScalaVersion.startsWith("3"))
test("@main in script") {
val inputs = TestInputs(
os.rel / "test.sc" ->
"""@main def main(args: Strings*): Unit = println("Args: " + args.mkString(" "))
|""".stripMargin
)

withBsp(inputs, Seq(".")) { (root, localClient, remoteServer) =>
async {
val buildTargetsResp = await(remoteServer.workspaceBuildTargets().asScala)
val target = {
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
expect(targets.length == 2)
extractMainTargets(targets)
}

val targetUri = TestUtil.normalizeUri(target.getUri)
checkTargetUri(root, targetUri)

val targets = List(target).asJava

val compileResp = await {
remoteServer
.buildTargetCompile(new b.CompileParams(targets))
.asScala
}
expect(compileResp.getStatusCode == b.StatusCode.ERROR)

val diagnosticsParams = {
val diagnostics = localClient.diagnostics()
val params = diagnostics(2)
expect(params.getBuildTarget.getUri == targetUri)
expect(
TestUtil.normalizeUri(params.getTextDocument.getUri) ==
TestUtil.normalizeUri((root / "test.sc").toNIO.toUri.toASCIIString)
)
params
}

val diagnostics = diagnosticsParams.getDiagnostics.asScala.toSeq
expect(diagnostics.length == 1)

checkDiagnostic(
diagnostic = diagnostics.head,
expectedMessage =
"Annotation @main in .sc scripts is not supported, use .scala format instead",
expectedSeverity = b.DiagnosticSeverity.ERROR,
expectedStartLine = 0,
expectedStartCharacter = 0,
expectedEndLine = 0,
expectedEndCharacter = 5
)
}
}
}

private def checkIfBloopProjectIsInitialised(
root: os.Path,
buildTargetsResp: b.WorkspaceBuildTargetsResult
Expand Down

0 comments on commit f70d88e

Please sign in to comment.