Skip to content

Commit

Permalink
Return build exception to propagate error using EitherCps
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Jul 13, 2022
1 parent b5b415a commit bce1ca2
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 27 deletions.
11 changes: 6 additions & 5 deletions modules/build/src/main/scala/scala/build/Inputs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.security.MessageDigest

import scala.annotation.tailrec
import scala.build.Inputs.WorkspaceOrigin
import scala.build.errors.{BuildException, InputsException}
import scala.build.internal.Constants
import scala.build.internal.zip.WrappedZipInputStream
import scala.build.options.Scope
Expand Down Expand Up @@ -414,7 +415,7 @@ object Inputs {
javaSnippetOpt: Option[String],
acceptFds: Boolean,
forcedWorkspace: Option[os.Path]
): Either[String, Inputs] = {
): Either[BuildException, Inputs] = {
val validatedArgs: Seq[Either[String, Seq[Element]]] =
validateArgs(args, cwd, download, stdinOpt, acceptFds)
val validatedExpressions: Seq[Either[String, Seq[Element]]] =
Expand All @@ -432,7 +433,7 @@ object Inputs {
Right(forValidatedElems(validElems, baseProjectName, directories, forcedWorkspace))
}
else
Left(invalid.mkString(System.lineSeparator()))
Left(new InputsException(invalid.mkString(System.lineSeparator())))
}

def apply(
Expand All @@ -448,13 +449,13 @@ object Inputs {
javaSnippetOpt: Option[String] = None,
acceptFds: Boolean = false,
forcedWorkspace: Option[os.Path] = None
): Either[String, Inputs] =
): Either[BuildException, Inputs] =
if (
args.isEmpty && scriptSnippetOpt.isEmpty && scalaSnippetOpt.isEmpty && javaSnippetOpt.isEmpty
)
defaultInputs().toRight(
defaultInputs().toRight(new InputsException(
"No inputs provided (expected files with .scala or .sc extensions, and / or directories)."
)
))
else
forNonEmptyArgs(
args,
Expand Down
3 changes: 2 additions & 1 deletion modules/build/src/main/scala/scala/build/bsp/Bsp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package scala.build.bsp
import java.io.{InputStream, OutputStream}

import scala.build.Inputs
import scala.build.errors.BuildException
import scala.concurrent.Future

trait Bsp {
Expand All @@ -12,7 +13,7 @@ trait Bsp {

object Bsp {
def create(
argsToInputs: Seq[String] => Either[String, Inputs],
argsToInputs: Seq[String] => Either[BuildException, Inputs],
bspReloadableOptionsReference: BspReloadableOptions.Reference,
threads: BspThreads,
in: InputStream,
Expand Down
8 changes: 4 additions & 4 deletions modules/build/src/main/scala/scala/build/bsp/BspImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import scala.build.EitherCps.{either, value}
import scala.build._
import scala.build.bloop.{BloopServer, ScalaDebugServer}
import scala.build.compiler.BloopCompiler
import scala.build.errors.{BuildException, Diagnostic}
import scala.build.errors.{BuildException, Diagnostic, ParsingInputsException}
import scala.build.internal.{Constants, CustomCodeWrapper}
import scala.build.options.{BuildOptions, Scope}
import scala.collection.mutable.ListBuffer
Expand All @@ -23,7 +23,7 @@ import scala.jdk.CollectionConverters._
import scala.util.{Failure, Success}

final class BspImpl(
argsToInputs: Seq[String] => Either[String, Inputs],
argsToInputs: Seq[String] => Either[BuildException, Inputs],
bspReloadableOptionsReference: BspReloadableOptions.Reference,
threads: BspThreads,
in: InputStream,
Expand Down Expand Up @@ -459,13 +459,13 @@ final class BspImpl(
val ideInputsJsonPath =
currentBloopSession.inputs.workspace / Constants.workspaceDirName / "ide-inputs.json"
if (os.isFile(ideInputsJsonPath)) {
val maybeResponse = either[String] {
val maybeResponse = either[BuildException] {
val ideInputs = value {
try Right(readFromArray(os.read.bytes(ideInputsJsonPath))(IdeInputs.codec))
catch {
case e: JsonReaderException =>
logger.debug(s"Caught $e while decoding $ideInputsJsonPath")
Left(e.getMessage)
Left(new ParsingInputsException(e.getMessage, e))
}
}
val newInputs = value(argsToInputs(ideInputs.args))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final case class TestInputs(
forcedWorkspace = forcedWorkspaceOpt.map(_.resolveFrom(tmpDir))
)
res match {
case Left(err) => sys.error(err)
case Left(err) => throw new Exception(err)
case Right(inputs) => f(tmpDir, inputs)
}
}
Expand Down
26 changes: 15 additions & 11 deletions modules/cli/src/main/scala/scala/cli/commands/Bsp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package scala.cli.commands
import caseapp._
import com.github.plokhotnyuk.jsoniter_scala.core._

import scala.build.EitherCps.{either, value}
import scala.build.bsp.{BspReloadableOptions, BspThreads}
import scala.build.errors.BuildException
import scala.build.options.BuildOptions
import scala.build.{Build, Inputs}
import scala.cli.CurrentParams
Expand All @@ -25,16 +27,17 @@ object Bsp extends ScalaCommand[BspOptions] {
readFromArray(content)(SharedOptions.jsonCodec)
}.getOrElse(options.shared)

val argsToInputs: Seq[String] => Either[String, Inputs] =
argsSeq => {
val sharedOptions = getSharedOptions()
sharedOptions.inputs(argsSeq, () => Inputs.default())
.map { i =>
if (sharedOptions.logging.verbosity >= 3)
pprint.err.log(i)
Build.updateInputs(i, buildOptions(sharedOptions))
}
}
val argsToInputs: Seq[String] => Either[BuildException, Inputs] =
argsSeq =>
either {
val sharedOptions = getSharedOptions()
val initialInputs = value(sharedOptions.inputs(argsSeq, () => Inputs.default()))

if (sharedOptions.logging.verbosity >= 3)
pprint.err.log(initialInputs)

Build.updateInputs(initialInputs, buildOptions(sharedOptions))
}

val bspReloadableOptionsReference = BspReloadableOptions.Reference { () =>
val sharedOptions = getSharedOptions()
Expand All @@ -46,7 +49,8 @@ object Bsp extends ScalaCommand[BspOptions] {
)
}

val inputs = getSharedOptions().inputsOrExit(argsToInputs(args.all))
val logger = getSharedOptions().logging.logger
val inputs = argsToInputs(args.all).orExit(logger)
CurrentParams.workspaceOpt = Some(inputs.workspace)
BspThreads.withThreads { threads =>
val bsp = scala.build.bsp.Bsp.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import java.io.{File, InputStream}
import scala.build._
import scala.build.blooprifle.BloopRifleConfig
import scala.build.compiler.{BloopCompilerMaker, ScalaCompilerMaker, SimpleScalaCompilerMaker}
import scala.build.errors.BuildException
import scala.build.internal.CsLoggerUtil._
import scala.build.internal.{Constants, FetchExternalBinary, OsLibc}
import scala.build.internal.{Constants, FetchExternalBinary, OsLibc, Util}
import scala.build.options.{Platform, ScalacOpt, ShadowingSeq}
import scala.build.{options => bo}
import scala.cli.commands.ScalaJsOptions
Expand Down Expand Up @@ -229,9 +230,9 @@ object SharedOptionsUtil {
): Inputs =
inputsOrExit(inputs(args, defaultInputs))

def inputsOrExit(maybeInputs: Either[String, Inputs]): Inputs = maybeInputs match {
case Left(message) =>
System.err.println(message)
def inputsOrExit(maybeInputs: Either[BuildException, Inputs]): Inputs = maybeInputs match {
case Left(exception) =>
Util.printException(exception)
sys.exit(1)
case Right(i) => i
}
Expand All @@ -252,7 +253,7 @@ object SharedOptionsUtil {
def inputs(
args: Seq[String],
defaultInputs: () => Option[Inputs]
): Either[String, Inputs] = {
): Either[BuildException, Inputs] = {
val resourceInputs = resourceDirs
.map(os.Path(_, Os.pwd))
.map { path =>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package scala.build.errors

class InputsException(message: String)
extends BuildException(
message = message
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package scala.build.errors

class ParsingInputsException(exceptionMessage: String, cause: Throwable)
extends BuildException(
message = exceptionMessage,
cause = cause
)

0 comments on commit bce1ca2

Please sign in to comment.