Skip to content

Commit 855a033

Browse files
authored
Fix BSP buildTarget/wrappedSources for GraalVM native image (#4153)
1 parent 35b56b5 commit 855a033

7 files changed

Lines changed: 129 additions & 7 deletions

File tree

modules/cli/src/main/resources/META-INF/native-image/org.virtuslab/scala-cli-core/reflect-config.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,42 @@
12421242
"allDeclaredMethods": true,
12431243
"allDeclaredFields": true
12441244
},
1245+
{
1246+
"name": "scala.build.bsp.ScalaScriptBuildServer",
1247+
"queryAllDeclaredMethods": true,
1248+
"allDeclaredConstructors": true,
1249+
"allPublicConstructors": true,
1250+
"allDeclaredMethods": true,
1251+
"allDeclaredFields": true
1252+
},
1253+
{
1254+
"name": "scala.build.bsp.WrappedSourceItem",
1255+
"allDeclaredConstructors": true,
1256+
"allPublicConstructors": true,
1257+
"allDeclaredMethods": true,
1258+
"allDeclaredFields": true
1259+
},
1260+
{
1261+
"name": "scala.build.bsp.WrappedSourcesItem",
1262+
"allDeclaredConstructors": true,
1263+
"allPublicConstructors": true,
1264+
"allDeclaredMethods": true,
1265+
"allDeclaredFields": true
1266+
},
1267+
{
1268+
"name": "scala.build.bsp.WrappedSourcesParams",
1269+
"allDeclaredConstructors": true,
1270+
"allPublicConstructors": true,
1271+
"allDeclaredMethods": true,
1272+
"allDeclaredFields": true
1273+
},
1274+
{
1275+
"name": "scala.build.bsp.WrappedSourcesResult",
1276+
"allDeclaredConstructors": true,
1277+
"allPublicConstructors": true,
1278+
"allDeclaredMethods": true,
1279+
"allDeclaredFields": true
1280+
},
12451281
{
12461282
"name": "scala.build.bsp.protocol.TextEdit",
12471283
"allDeclaredConstructors": true,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package scala.cli.integration.bsp;
2+
3+
public class WrappedSourceItem {
4+
public String uri;
5+
public String generatedUri;
6+
public String topWrapper;
7+
public String bottomWrapper;
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package scala.cli.integration.bsp;
2+
3+
import ch.epfl.scala.bsp4j.BuildTargetIdentifier;
4+
import java.util.List;
5+
6+
public class WrappedSourcesItem {
7+
public BuildTargetIdentifier target;
8+
public List<WrappedSourceItem> sources;
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package scala.cli.integration.bsp;
2+
3+
import java.util.List;
4+
5+
public class WrappedSourcesResult {
6+
public List<WrappedSourcesItem> items;
7+
}

modules/integration/src/test/scala/scala/cli/integration/BspSuite.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ trait BspSuite { this: ScalaCliSuite =>
7979
f: (
8080
os.Path,
8181
TestBspClient,
82-
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer
82+
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer &
83+
TestBspClient.WrappedSourcesBuildServer
8384
) => Future[T]
8485
): T = withBspInitResults(
8586
inputs,
@@ -107,7 +108,8 @@ trait BspSuite { this: ScalaCliSuite =>
107108
f: (
108109
os.Path,
109110
TestBspClient,
110-
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer,
111+
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer &
112+
TestBspClient.WrappedSourcesBuildServer,
111113
b.InitializeBuildResult
112114
) => Future[T]
113115
): T = {
@@ -120,8 +122,9 @@ trait BspSuite { this: ScalaCliSuite =>
120122

121123
val proc = os.proc(TestUtil.cli, "bsp", bspOptions ++ extraOptionsOverride, args)
122124
.spawn(cwd = root, stderr = stderr, env = bspEnvs)
123-
var remoteServer: b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer =
124-
null
125+
var remoteServer
126+
: b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer &
127+
TestBspClient.WrappedSourcesBuildServer = null
125128

126129
val bspServerExited = Promise[Unit]()
127130
val t = new Thread("bsp-server-watcher") {

modules/integration/src/test/scala/scala/cli/integration/BspTestDefinitions.scala

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,4 +2394,55 @@ abstract class BspTestDefinitions extends ScalaCliSuite
23942394
}
23952395
}
23962396
}
2397+
2398+
test("buildTarget/wrappedSources") {
2399+
val inputs = TestInputs(
2400+
os.rel / "simple.sc" ->
2401+
s"""val msg = "Hello"
2402+
|println(msg)
2403+
|""".stripMargin
2404+
)
2405+
2406+
withBsp(inputs, Seq(".")) { (root, _, remoteServer) =>
2407+
Future {
2408+
val buildTargetsResp = remoteServer.workspaceBuildTargets().asScala.await
2409+
val target = {
2410+
val targets = buildTargetsResp.getTargets.asScala.map(_.getId).toSeq
2411+
expect(targets.length == 2)
2412+
extractMainTargets(targets)
2413+
}
2414+
2415+
val targets = List(target).asJava
2416+
2417+
val compileResp =
2418+
remoteServer.buildTargetCompile(new b.CompileParams(targets)).asScala.await
2419+
expect(compileResp.getStatusCode == b.StatusCode.OK)
2420+
2421+
val resp = remoteServer
2422+
.buildTargetWrappedSources(new b.SourcesParams(targets))
2423+
.asScala
2424+
.await
2425+
2426+
val items = resp.items.asScala
2427+
expect(items.nonEmpty)
2428+
2429+
val mainItem = items.find(_.target.getUri == target.getUri).get
2430+
2431+
val sources = mainItem.sources.asScala
2432+
expect(sources.size == 1)
2433+
2434+
val wrappedSource = sources.head
2435+
val sourceUri = TestUtil.normalizeUri(wrappedSource.uri)
2436+
val expectedUri =
2437+
TestUtil.normalizeUri((root / "simple.sc").toNIO.toUri.toASCIIString)
2438+
expect(sourceUri == expectedUri)
2439+
2440+
expect(wrappedSource.topWrapper.contains("simple"))
2441+
expect(wrappedSource.topWrapper.contains("scriptPath"))
2442+
expect(wrappedSource.bottomWrapper == "}")
2443+
expect(wrappedSource.generatedUri.endsWith(".scala"))
2444+
expect(wrappedSource.generatedUri.contains("simple"))
2445+
}
2446+
}
2447+
}
23972448
}

modules/integration/src/test/scala/scala/cli/integration/TestBspClient.scala

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import ch.epfl.scala.bsp4j as b
44
import org.eclipse.lsp4j.jsonrpc
55

66
import java.io.{InputStream, OutputStream}
7-
import java.util.concurrent.ExecutorService
7+
import java.util.concurrent.{CompletableFuture, ExecutorService}
88

9+
import scala.cli.integration.bsp.WrappedSourcesResult
910
import scala.collection.mutable
1011
import scala.concurrent.{ExecutionContext, Future, Promise}
1112
import scala.util.{Failure, Success}
@@ -100,16 +101,23 @@ class TestBspClient extends b.BuildClient {
100101

101102
object TestBspClient {
102103

104+
trait WrappedSourcesBuildServer {
105+
@org.eclipse.lsp4j.jsonrpc.services.JsonRequest("buildTarget/wrappedSources")
106+
def buildTargetWrappedSources(params: b.SourcesParams)
107+
: CompletableFuture[WrappedSourcesResult]
108+
}
109+
103110
private trait BuildServer extends b.BuildServer with b.ScalaBuildServer with b.JavaBuildServer
104-
with b.JvmBuildServer
111+
with b.JvmBuildServer with WrappedSourcesBuildServer
105112

106113
def connect(
107114
in: InputStream,
108115
out: OutputStream,
109116
es: ExecutorService
110117
): (
111118
TestBspClient,
112-
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer,
119+
b.BuildServer & b.ScalaBuildServer & b.JavaBuildServer & b.JvmBuildServer &
120+
WrappedSourcesBuildServer,
113121
Future[Unit]
114122
) = {
115123

0 commit comments

Comments
 (0)