Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New using directive syntax #546

Merged
merged 13 commits into from
Jan 24, 2022
Merged
16 changes: 7 additions & 9 deletions build.sc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import $file.project.settings, settings.{
localRepoResourcePath,
platformExecutableJarExtension
}
import $file.project.deps, deps.customRepositories

import java.io.File
import java.nio.charset.Charset
Expand Down Expand Up @@ -113,9 +114,7 @@ object `generate-reference-doc` extends SbtModule with ScalaCliScalafixModule {
def moduleDeps = Seq(
cli
)
def repositories = super.repositories ++ Seq(
coursier.Repositories.sonatype("snapshots")
)
def repositories = super.repositories ++ customRepositories
def ivyDeps = Agg(
Deps.caseApp,
Deps.munit
Expand Down Expand Up @@ -176,9 +175,8 @@ class Build(val crossScalaVersion: String)
def scalacOptions = T {
super.scalacOptions() ++ Seq("-Xasync", "-Ywarn-unused", "-deprecation")
}
def repositories = super.repositories ++ Seq(
coursier.Repositories.sonatype("snapshots")
)
def repositories = super.repositories ++ customRepositories

def compileIvyDeps = super.compileIvyDeps() ++ Agg(
Deps.svm
)
Expand Down Expand Up @@ -324,9 +322,9 @@ trait Cli extends SbtModule with CliLaunchers with ScalaCliPublishModule with Fo
build(Scala.defaultInternal),
`test-runner`(Scala.defaultInternal)
)
def repositories = super.repositories ++ Seq(
coursier.Repositories.sonatype("snapshots")
)

def repositories = super.repositories ++ customRepositories

def ivyDeps = super.ivyDeps() ++ Agg(
Deps.caseApp,
Deps.coursierLauncher,
Expand Down
4 changes: 2 additions & 2 deletions examples/cross-build/Hello.js.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// using scala 2.13
// using target scala-js
//> using scala 2.13
//> using target scala-js
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did I omit a moment when we allowed strings without quotes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do not check examples if compile file, a good catch, I've open a new issue for that: #575

I will also include fixes in a followup PR.


import scala.scalajs.js

Expand Down
4 changes: 2 additions & 2 deletions examples/cross-build/Hello.native.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// using scala 2.13
// using target scala-native
//> using scala 2.13
//> using target scala-native
Comment on lines +1 to +2
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above


import scala.scalanative.libc._
import scala.scalanative.unsafe._
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-versions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Using directives syntax is still experimental and may change in future versions
So when we will have:

```scala title=version.scala
// using scala 2.12.5
//> using scala 2.12.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above


object OldCode
//rest of the config
Expand Down
2 changes: 1 addition & 1 deletion examples/scala-versions/version.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// File was generated from based on docs/cookbooks/scala-versions.md, do not edit manually!

// using scala 2.12.5
//> using scala 2.12.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above


object OldCode
//rest of the config
2 changes: 1 addition & 1 deletion examples/scripting/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ print all files located in path passed as the first argument
Create a file name `ls.sc`
```scala
#!/usr/bin/env scala-cli
// using lib "com.lihaoyi::os-lib:0.7.8"
//> using lib "com.lihaoyi::os-lib:0.7.8"
val cwd = if(args.nonEmpty) os.Path(args(0), os.pwd) else os.pwd
println(os.list(cwd).map(_.last).mkString("\n"))
```
Expand Down
2 changes: 1 addition & 1 deletion examples/utest/MyTests.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// using lib "com.lihaoyi::utest::0.7.10"
//> using lib "com.lihaoyi::utest::0.7.10"

import utest._

Expand Down
6 changes: 3 additions & 3 deletions gcbenchmark/gcbenchmark.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// using lib "com.lihaoyi::os-lib:0.8.0"
// using lib "com.lihaoyi::pprint:0.7.1"
// using scala "2.13"
//> using lib "com.lihaoyi::os-lib:0.8.0"
//> using lib "com.lihaoyi::pprint:0.7.1"
//> using scala "2.13"

// Usage: scala-cli gcbenchmark.scala -- <path_to_scala_cli_executable>

Expand Down
2 changes: 1 addition & 1 deletion mill
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ EOF
exit 1
fi

eval "$(cs java --env --jvm temurin:17)"
eval "$(cs java --env --jvm temurin:17 || cs java --env --jvm openjdk:1.17.0)"

exec /usr/bin/env bash "$DIR/millw" "$@"
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ object ConsoleBloopBuildClient {
private val red = Console.RED
private val yellow = Console.YELLOW

private def getPrefix(severity: Severity) =
if (severity == Severity.Error) s"[${red}error$reset] "
def diagnosticPrefix(isError: Boolean): String =
if (isError) s"[${red}error$reset] "
else s"[${yellow}warn$reset] "

def printFileDiagnostic(
Expand All @@ -163,9 +163,7 @@ object ConsoleBloopBuildClient {
val isWarningOrError = diag.getSeverity == bsp4j.DiagnosticSeverity.ERROR ||
diag.getSeverity == bsp4j.DiagnosticSeverity.WARNING
if (isWarningOrError) {
val prefix =
if (diag.getSeverity == bsp4j.DiagnosticSeverity.ERROR) s"[${red}error$reset] "
else s"[${yellow}warn$reset] "
val prefix = diagnosticPrefix(diag.getSeverity == bsp4j.DiagnosticSeverity.ERROR)

val line = (diag.getRange.getStart.getLine + 1).toString + ":"
val col = (diag.getRange.getStart.getCharacter + 1).toString + ":"
Expand Down Expand Up @@ -217,7 +215,7 @@ object ConsoleBloopBuildClient {
val isWarningOrError = true
if (isWarningOrError) {
val msgIt = message.linesIterator
val prefix = getPrefix(severity)
val prefix = diagnosticPrefix(severity == Severity.Error)
out.println(prefix + (if (msgIt.hasNext) " " + msgIt.next() else ""))
msgIt.foreach(line => out.println(prefix + line))

Expand Down
9 changes: 8 additions & 1 deletion modules/build/src/main/scala/scala/build/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package scala.build
import java.io.{OutputStream, PrintStream}

import scala.build.blooprifle.BloopRifleLogger
import scala.build.errors.{BuildException, Diagnostic}
import scala.build.errors.{BuildException, Diagnostic, Severity}
import scala.scalanative.{build => sn}

trait Logger {
Expand All @@ -14,6 +14,13 @@ trait Logger {
def debug(s: => String): Unit

def log(diagnostics: Seq[Diagnostic]): Unit

def diagnostic(
message: String,
severity: Severity = Severity.Warning,
positions: Seq[Position] = Nil
): Unit = log(Seq(Diagnostic(message, severity, positions)))

def log(ex: BuildException): Unit
def exit(ex: BuildException): Nothing

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package scala.build

import java.io.PrintStream

import scala.build.blooprifle.BloopRifleLogger
import scala.build.errors.{BuildException, Diagnostic}
import scala.scalanative.{build => sn}

class PersistentDiagnosticLogger(parent: Logger) extends Logger {
private val diagBuilder = List.newBuilder[Diagnostic]

def diagnostics = diagBuilder.result()

// TODO Use macros for log and debug calls to have zero cost when verbosity <= 0
def message(message: => String): Unit = parent.message(message)
def log(s: => String): Unit = parent.log(s)
def log(s: => String, debug: => String): Unit = parent.log(s, debug)
def debug(s: => String): Unit = parent.debug(s)

def log(diagnostics: Seq[Diagnostic]): Unit = {
parent.log(diagnostics)
diagBuilder ++= diagnostics
}

def log(ex: BuildException): Unit = parent.log(ex)
def exit(ex: BuildException): Nothing = parent.exit(ex)

def coursierLogger: coursier.cache.CacheLogger = parent.coursierLogger
def bloopRifleLogger: BloopRifleLogger = parent.bloopRifleLogger
def scalaNativeTestLogger: sn.Logger = parent.scalaNativeTestLogger
def scalaNativeCliInternalLoggerOptions: List[String] = parent.scalaNativeCliInternalLoggerOptions

def compilerOutputStream: PrintStream = parent.compilerOutputStream
}
62 changes: 35 additions & 27 deletions modules/build/src/main/scala/scala/build/bsp/BspClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import java.net.URI
import java.nio.file.Paths
import java.util.concurrent.{ConcurrentHashMap, ExecutorService}

import scala.build.errors.{BuildException, CompositeBuildException}
import scala.build.Position.File
import scala.build.errors.{BuildException, CompositeBuildException, Diagnostic, Severity}
import scala.build.postprocessing.LineConversion
import scala.build.{BloopBuildClient, GeneratedSource, Logger, Position}
import scala.build.{BloopBuildClient, GeneratedSource, Logger}
import scala.jdk.CollectionConverters._

class BspClient(
Expand Down Expand Up @@ -145,7 +146,7 @@ class BspClient(
def reportBuildException(
targetIdOpt: Option[b.BuildTargetIdentifier],
ex: BuildException,
isFirst: Boolean = true
reset: Boolean = true
): Unit =
targetIdOpt match {
case None =>
Expand All @@ -155,30 +156,8 @@ class BspClient(
case c: CompositeBuildException => c.exceptions
case _ => Seq(ex)
}
var touchedFiles = Set.empty[os.Path]
for {
ex <- allExceptions
pos <- ex.positions.distinct.collect { case f: Position.File => f }
path <- pos.path.toOption
} {
val id = new b.TextDocumentIdentifier(path.toNIO.toUri.toASCIIString)
val diag = {
val startPos = new b.Position(pos.startPos._1, pos.startPos._2)
val endPos = new b.Position(pos.endPos._1, pos.endPos._2)
val range = new b.Range(startPos, endPos)
new b.Diagnostic(range, ex.message)
}
diag.setSeverity(b.DiagnosticSeverity.ERROR)
val params = new b.PublishDiagnosticsParams(
id,
targetId,
List(diag).asJava,
isFirst
)
touchedFiles = touchedFiles + path
buildExceptionDiagnosticsDocs.put((path, targetId), JBoolean.TRUE)
actualBuildPublishDiagnostics(params)
}
val touchedFiles =
allExceptions.flatMap(reportDiagnosticForFiles(targetId, reset = reset)).toSet

// Small chance of us wiping some Bloop diagnostics, if these happen
// between the call to remove and the call to actualBuildPublishDiagnostics.
Expand All @@ -200,4 +179,33 @@ class BspClient(
}
}

def reportDiagnosticForFiles(
targetId: b.BuildTargetIdentifier,
reset: Boolean = false
)(diag: Diagnostic): Seq[os.Path] =
diag.positions.flatMap {
case File(Right(path), (startLine, startC), (endL, endC)) =>
val id = new b.TextDocumentIdentifier(path.toNIO.toUri.toASCIIString)
val bDiag = {
val startPos = new b.Position(startLine, startC)
val endPos = new b.Position(endL, endC)
val range = new b.Range(startPos, endPos)
new b.Diagnostic(range, diag.message)
}
val severity = diag.severity match {
case Severity.Error => b.DiagnosticSeverity.ERROR
case Severity.Warning => b.DiagnosticSeverity.WARNING
}
bDiag.setSeverity(severity)
val params = new b.PublishDiagnosticsParams(
id,
targetId,
List(bDiag).asJava,
reset
)
buildExceptionDiagnosticsDocs.put((path, targetId), JBoolean.TRUE)
actualBuildPublishDiagnostics(params)
Seq(path)
case _ => Nil
}
}
Loading