Merge branch 'develop' into bsp-client

This commit is contained in:
Ethan Atkins 2020-10-28 10:07:12 -07:00 committed by GitHub
commit f0c6d72ec7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 376 additions and 33 deletions

View File

@ -391,6 +391,8 @@ object Defaults extends BuildCommon {
canonicalInput :== true,
echoInput :== true,
terminal := state.value.get(terminalKey).getOrElse(Terminal(ITerminal.get)),
InstallSbtn.installSbtn := InstallSbtn.installSbtnImpl.evaluated,
InstallSbtn.installSbtn / aggregate := false,
) ++ LintUnused.lintSettings
++ DefaultBackgroundJobService.backgroundJobServiceSettings
++ RemoteCache.globalSettings

View File

@ -0,0 +1,226 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package sbt
package internal
import Def._
import Keys.{ sbtVersion, state, terminal }
import java.io.{ File, FileInputStream, FileOutputStream, InputStream, IOException }
import java.net.URL
import java.nio.file.{ Files, Path }
import java.util.zip.ZipInputStream
import sbt.io.IO
import sbt.io.Path.userHome
import sbt.io.syntax._
import scala.util.{ Properties, Try }
private[sbt] object InstallSbtn {
private[sbt] val installSbtn =
Def.inputKey[Unit]("install sbtn and tab completions").withRank(KeyRanks.BTask)
private[sbt] def installSbtnImpl: Def.Initialize[InputTask[Unit]] = Def.inputTask {
val inputVersion = Def.spaceDelimited("version").parsed.headOption
val version = inputVersion.getOrElse(sbtVersion.value.replaceAllLiterally("-SNAPSHOT", ""))
val term = terminal.value
term.setMode(canonical = false, echo = false)
val baseDirectory = BuildPaths.getGlobalBase(state.value).toPath
val tmp = Files.createTempFile(s"sbt-$version", "zip")
val sbtn = if (Properties.isWin) "sbtn.exe" else "sbtn"
try extractSbtn(term, version, tmp, baseDirectory.resolve("bin").resolve(sbtn))
finally {
Files.deleteIfExists(tmp)
()
}
val shell = if (System.console != null) getShell(term) else "none"
shell match {
case "none" =>
case s =>
val completion = shellCompletions(s)
val completionLocation = baseDirectory.resolve("completions").resolve(completion)
downloadCompletion(completion, version, completionLocation)
s match {
case "bash" => setupBash(baseDirectory, term)
case "fish" => setupFish(baseDirectory, term)
case "zsh" => setupZsh(baseDirectory, term)
case "powershell" => setupPowershell(baseDirectory, term)
case _ => // should be unreachable
}
val msg = s"Successfully installed sbtn for $s. You may need to restart $s for the " +
"changes to take effect."
term.printStream.println(msg)
}
()
}
private[sbt] def extractSbtn(term: Terminal, version: String, sbtZip: Path, sbtn: Path): Unit = {
downloadRelease(term, version, sbtZip)
Files.createDirectories(sbtn.getParent)
val bin =
if (Properties.isWin) "pc-win32.exe"
else if (Properties.isLinux) "pc-linux"
else "apple-darwin"
val sbtnName = s"sbt/bin/sbtn-x86_64-$bin"
val fis = new FileInputStream(sbtZip.toFile)
val zipInputStream = new ZipInputStream(fis)
var foundBinary = false
try {
var entry = zipInputStream.getNextEntry
while (entry != null) {
if (entry.getName == sbtnName) {
foundBinary = true
term.printStream.println(s"extracting $sbtZip!$sbtnName to $sbtn")
transfer(zipInputStream, sbtn)
sbtn.toFile.setExecutable(true)
entry = null
} else {
entry = zipInputStream.getNextEntry
}
}
if (!foundBinary) throw new IllegalStateException(s"couldn't find $sbtnName in $sbtZip")
} finally {
fis.close()
zipInputStream.close()
}
()
}
private[this] def downloadRelease(term: Terminal, version: String, location: Path): Unit = {
val zip = s"https://github.com/sbt/sbt/releases/download/v$version/sbt-$version.zip"
val url = new URL(zip)
term.printStream.println(s"downloading $zip to $location")
transfer(url.openStream(), location)
}
private[this] def transfer(inputStream: InputStream, path: Path): Unit =
try {
val os = new FileOutputStream(path.toFile)
try {
val result = new Array[Byte](1024 * 1024)
var bytesRead = -1
do {
bytesRead = inputStream.read(result)
if (bytesRead > 0) os.write(result, 0, bytesRead)
} while (bytesRead > 0)
} finally os.close()
} finally inputStream.close()
private[this] def getShell(term: Terminal): String = {
term.printStream.print(s"""Setup sbtn for shell:
| [1] bash
| [2] fish
| [3] powershell
| [4] zsh
| [5] none
|Enter option: """.stripMargin)
term.printStream.flush()
val key = term.inputStream.read
term.printStream.println(key.toChar)
key match {
case 49 => "bash"
case 50 => "fish"
case 51 => "powershell"
case 52 => "zsh"
case _ => "none"
}
}
private[this] def downloadCompletion(completion: String, version: String, target: Path): Unit = {
Files.createDirectories(target.getParent)
val comp = s"https://raw.githubusercontent.com/sbt/sbt/v$version/client/completions/$completion"
transfer(new URL(comp).openStream, target)
}
private[this] def setupShell(
shell: String,
baseDirectory: Path,
term: Terminal,
configFile: File,
setPath: Path => String,
setCompletions: Path => String,
): Unit = {
val bin = baseDirectory.resolve("bin")
val export = setPath(bin)
val completions = baseDirectory.resolve("completions")
val sourceCompletions = setCompletions(completions)
val contents = try IO.read(configFile)
catch { case _: IOException => "" }
if (!contents.contains(export)) {
term.printStream.print(s"Add $bin to PATH in $configFile? y/n (y default): ")
term.printStream.flush()
term.inputStream.read() match {
case 110 => term.printStream.println()
case c =>
term.printStream.println(c.toChar)
// put the export at the bottom so that the ~/.sbt/1.0/bin/sbtn is least preferred
// but still on the path
IO.write(configFile, s"$contents\n$export")
}
}
val newContents = try IO.read(configFile)
catch { case _: IOException => "" }
if (!newContents.contains(sourceCompletions)) {
term.printStream.print(s"Add tab completions to $configFile? y/n (y default): ")
term.printStream.flush()
term.inputStream.read() match {
case 110 =>
case c =>
term.printStream.println(c.toChar)
if (shell == "zsh") {
// delete the .zcompdump file because it can prevent the new completions from
// being recognized
Files.deleteIfExists((userHome / ".zcompdump").toPath)
// put the completions at the top because it is effectively just a source
// so the order in the file doesn't really matter but we want to make sure
// that we set fpath before any autoload command in zsh
IO.write(configFile, s"$sourceCompletions\n$newContents")
} else {
IO.write(configFile, s"$newContents\n$sourceCompletions")
}
}
term.printStream.println()
}
}
private[this] def setupBash(baseDirectory: Path, term: Terminal): Unit =
setupShell(
"bash",
baseDirectory,
term,
userHome / ".bashrc",
bin => s"export PATH=$$PATH:$bin",
completions => s"source $completions/sbtn.bash"
)
private[this] def setupZsh(baseDirectory: Path, term: Terminal): Unit = {
val comp = (completions: Path) => {
"# The following two lines were added by the sbt installSbtn task:\n" +
s"fpath=($$fpath $completions)\nautoload -Uz compinit; compinit"
}
setupShell("zsh", baseDirectory, term, userHome / ".zshrc", bin => s"path=($$path $bin)", comp)
}
private[this] def setupFish(baseDirectory: Path, term: Terminal): Unit = {
val comp = (completions: Path) => s"source $completions/sbtn.fish"
val path = (bin: Path) => s"set PATH $$PATH $bin"
val config = userHome / ".config" / "fish" / "config.fish"
setupShell("fish", baseDirectory, term, config, path, comp)
}
private[this] def setupPowershell(baseDirectory: Path, term: Terminal): Unit = {
val comp = (completions: Path) => s""". "$completions\\sbtn.ps1""""
val path = (bin: Path) => s"""$$env:Path += ";$bin""""
import scala.sys.process._
Try(Seq("pwsh", "-Command", "echo $PROFILE").!!).foreach { output =>
output.linesIterator.toSeq.headOption.foreach { l =>
setupShell("pwsh", baseDirectory, term, new File(l), path, comp)
}
}
Try(Seq("powershell", "-Command", "echo $PROFILE").!!).foreach { output =>
output.linesIterator.toSeq.headOption.foreach { l =>
setupShell("pwsh", baseDirectory, term, new File(l), path, comp)
}
}
}
private[this] val shellCompletions = Map(
"bash" -> "sbtn.bash",
"fish" -> "sbtn.fish",
"powershell" -> "sbtn.ps1",
"zsh" -> "_sbtn",
)
}

View File

@ -139,6 +139,7 @@ private[sbt] class TaskProgress(
}
private[this] val skipReportTasks =
Set(
"installSbtn",
"run",
"runMain",
"bgRun",

View File

@ -7,6 +7,8 @@
package sbt.internal.server
import java.nio.file.Path
import sbt.StandardMain
import sbt.internal.bsp._
import sbt.internal.util.ManagedLogger
@ -69,7 +71,7 @@ final class BuildServerReporterImpl(
import sbt.internal.inc.JavaInterfaceUtil._
private lazy val exchange = StandardMain.exchange
private val problemsByFile = mutable.Map[VirtualFileRef, Vector[Diagnostic]]()
private val problemsByFile = mutable.Map[Path, Vector[Diagnostic]]()
override def sendSuccessReport(analysis: CompileAnalysis): Unit = {
for {
@ -90,9 +92,8 @@ final class BuildServerReporterImpl(
override def sendFailureReport(sources: Array[VirtualFile]): Unit = {
for (source <- sources) {
val ref = VirtualFileRef.of(source.id())
val diagnostics = problemsByFile.getOrElse(ref, Vector())
val filePath = converter.toPath(source)
val diagnostics = problemsByFile.getOrElse(filePath, Vector())
val params = PublishDiagnosticsParams(
textDocument = TextDocumentIdentifier(filePath.toUri),
buildTarget,
@ -106,14 +107,13 @@ final class BuildServerReporterImpl(
protected override def publishDiagnostic(problem: Problem): Unit = {
for {
path <- problem.position().sourcePath.toOption
source <- problem.position.sourceFile.toOption
id <- problem.position.sourcePath.toOption
diagnostic <- toDiagnostic(problem)
} {
val fileId = VirtualFileRef.of(path)
problemsByFile(fileId) = problemsByFile.getOrElse(fileId, Vector()) :+ diagnostic
val filePath = converter.toPath(VirtualFileRef.of(id))
problemsByFile(filePath) = problemsByFile.getOrElse(filePath, Vector()) :+ diagnostic
val params = PublishDiagnosticsParams(
TextDocumentIdentifier(source.toURI),
TextDocumentIdentifier(filePath.toUri),
buildTarget,
originId = None,
Vector(diagnostic),

View File

@ -0,0 +1,67 @@
/*
* sbt
* Copyright 2011 - 2018, Lightbend, Inc.
* Copyright 2008 - 2010, Mark Harrah
* Licensed under Apache License 2.0 (see LICENSE)
*/
package sbt
package internal
import java.io.{ InputStream, OutputStream, PrintStream }
import java.lang.ProcessBuilder
import java.lang.ProcessBuilder.Redirect
import java.nio.file.{ Files, Path }
import java.util.concurrent.TimeUnit
import org.scalatest.FlatSpec
import sbt.io.IO
class InstallSbtnSpec extends FlatSpec {
private def withTemp[R](ext: String)(f: Path => R): R = {
val tmp = Files.createTempFile("sbt-1.4.1-", ext)
try f(tmp)
finally {
Files.deleteIfExists(tmp)
()
}
}
private[this] val term = new Terminal {
def getHeight: Int = 0
def getWidth: Int = 0
def inputStream: InputStream = () => -1
def printStream: PrintStream = new PrintStream((_ => {}): OutputStream)
def setMode(canonical: Boolean, echo: Boolean): Unit = {}
}
// This test has issues in ci but runs ok locally on all platforms
"InstallSbtn" should "extract native sbtn" ignore
withTemp(".zip") { tmp =>
withTemp(".exe") { sbtn =>
InstallSbtn.extractSbtn(term, "1.4.1", tmp, sbtn)
val tmpDir = Files.createTempDirectory("sbtn-test").toRealPath()
Files.createDirectories(tmpDir.resolve("project"))
val foo = tmpDir.resolve("foo")
val fooPath = foo.toString.replaceAllLiterally("\\", "\\\\")
val build = s"""TaskKey[Unit]("foo") := IO.write(file("$fooPath"), "foo")"""
IO.write(tmpDir.resolve("build.sbt").toFile, build)
IO.write(
tmpDir.resolve("project").resolve("build.properties").toFile,
"sbt.version=1.4.1"
)
try {
val proc =
new ProcessBuilder(sbtn.toString, "foo;shutdown")
.redirectInput(Redirect.INHERIT)
.redirectOutput(Redirect.INHERIT)
.redirectError(Redirect.INHERIT)
.directory(tmpDir.toFile)
.start()
proc.waitFor(1, TimeUnit.MINUTES)
assert(proc.exitValue == 0)
assert(IO.read(foo.toFile) == "foo")
} finally {
sbt.io.IO.delete(tmpDir.toFile)
}
}
}
}

View File

@ -2,13 +2,17 @@ ThisBuild / scalaVersion := "2.13.1"
Global / serverLog / logLevel := Level.Debug
lazy val root = (project in file("."))
.aggregate(foo, util)
lazy val foo = project.in(file("foo"))
lazy val runAndTest = project.in(file("run-and-test"))
.settings(
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test",
)
.dependsOn(util)
lazy val reportError = project.in(file("report-error"))
lazy val reportWarning = project.in(file("report-warning"))
.settings(
scalacOptions += "-deprecation"
)
lazy val util = project

View File

@ -1,9 +0,0 @@
package foo
import org.scalatest.FreeSpec
class FooTest extends FreeSpec {
"test message" in {
assert(FooMain.message == "Hello World!")
}
}

View File

@ -0,0 +1,5 @@
package reportertests
object Error {
val version: String = 5
}

View File

@ -0,0 +1,7 @@
package reportertests
object Warning {
def print() {
prtinln("bar")
}
}

View File

@ -1,6 +1,6 @@
package foo
package main
object FooMain extends App {
object Main extends App {
lazy val message = "Hello World!"
println(message)

View File

@ -0,0 +1,9 @@
package tests
import org.scalatest.FreeSpec
class PassingTest extends FreeSpec {
"test message" in {
assert(main.Main.message == "Hello World!")
}
}

View File

@ -86,7 +86,7 @@ object BuildServerTest extends AbstractServerTest {
}
test("buildTarget/scalaMainClasses") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#foo/Compile"
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#runAndTest/Compile"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "16", "method": "buildTarget/scalaMainClasses", "params": {
| "targets": [{ "uri": "$x" }]
@ -95,17 +95,17 @@ object BuildServerTest extends AbstractServerTest {
assert(svr.waitForString(30.seconds) { s =>
println(s)
(s contains """"id":"16"""") &&
(s contains """"class":"foo.FooMain"""")
(s contains """"class":"main.Main"""")
})
}
test("buildTarget/run") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#foo/Compile"
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#runAndTest/Compile"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "17", "method": "buildTarget/run", "params": {
| "target": { "uri": "$x" },
| "dataKind": "scala-main-class",
| "data": { "class": "foo.FooMain" }
| "data": { "class": "main.Main" }
|} }""".stripMargin
)
assert(svr.waitForString(10.seconds) { s =>
@ -121,7 +121,7 @@ object BuildServerTest extends AbstractServerTest {
}
test("buildTarget/scalaTestClasses") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#foo/Test"
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#runAndTest/Test"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "18", "method": "buildTarget/scalaTestClasses", "params": {
| "targets": [{ "uri": "$x" }]
@ -130,12 +130,13 @@ object BuildServerTest extends AbstractServerTest {
assert(svr.waitForString(10.seconds) { s =>
println(s)
(s contains """"id":"18"""") &&
(s contains """"classes":["foo.FailingTest","foo.FooTest"]""")
(s contains """"tests.FailingTest"""") &&
(s contains """"tests.PassingTest"""")
})
}
test("buildTarget/test: run all tests") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#foo/Test"
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#runAndTest/Test"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "19", "method": "buildTarget/test", "params": {
| "targets": [{ "uri": "$x" }]
@ -149,7 +150,7 @@ object BuildServerTest extends AbstractServerTest {
}
test("buildTarget/test: run one test class") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#foo/Test"
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#runAndTest/Test"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "20", "method": "buildTarget/test", "params": {
| "targets": [{ "uri": "$x" }],
@ -158,7 +159,7 @@ object BuildServerTest extends AbstractServerTest {
| "testClasses": [
| {
| "target": { "uri": "$x" },
| "classes": ["foo.FooTest"]
| "classes": ["tests.PassingTest"]
| }
| ]
| }
@ -171,6 +172,36 @@ object BuildServerTest extends AbstractServerTest {
})
}
test("buildTarget/compile: report error") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#reportError/Compile"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "21", "method": "buildTarget/compile", "params": {
| "targets": [{ "uri": "$x" }]
|} }""".stripMargin
)
assert(svr.waitForString(10.seconds) { s =>
println(s)
(s contains s""""buildTarget":{"uri":"$x"}""") &&
(s contains """"severity":1""") &&
(s contains """"reset":true""")
})
}
test("buildTarget/compile: report warning") { _ =>
val x = s"${svr.baseDirectory.getAbsoluteFile.toURI}#reportWarning/Compile"
svr.sendJsonRpc(
s"""{ "jsonrpc": "2.0", "id": "22", "method": "buildTarget/compile", "params": {
| "targets": [{ "uri": "$x" }]
|} }""".stripMargin
)
assert(svr.waitForString(10.seconds) { s =>
println(s)
(s contains s""""buildTarget":{"uri":"$x"}""") &&
(s contains """"severity":2""") &&
(s contains """"reset":true""")
})
}
def initializeRequest(): Unit = {
svr.sendJsonRpc(
"""{ "jsonrpc": "2.0", "id": "10", "method": "build/initialize",