forked from scalacenter/bloop
-
Notifications
You must be signed in to change notification settings - Fork 8
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
Update jsonrpc4s (via a fork) #161
Merged
alexarchambault
merged 3 commits into
scala-cli:main
from
alexarchambault:bump-jsonrpc4s
Mar 15, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
integration/test/src/bloop/cli/integration/BspTests.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package bloop.cli.integration | ||
|
||
import java.net.{StandardProtocolFamily, UnixDomainSocketAddress} | ||
import java.nio.channels.SocketChannel | ||
import java.nio.charset.StandardCharsets | ||
import java.nio.ByteBuffer | ||
|
||
class BspTests extends munit.FunSuite { | ||
|
||
test("no JSON junk in errors") { | ||
TmpDir.fromTmpDir { root => | ||
val dirArgs = Seq[os.Shellable]("--daemon-dir", root / "daemon") | ||
val bspFile = root / "bsp-socket" | ||
|
||
val dummyMsg = | ||
"""{ | ||
| "jsonrpc": "2.0", | ||
| "method": "workspace/buildTargetz", | ||
| "params": null, | ||
| "id": 2 | ||
|}""".stripMargin | ||
|
||
var bspProc: os.SubProcess = null | ||
var socket: SocketChannel = null | ||
|
||
try { | ||
os.proc(Launcher.launcher, dirArgs, "about") | ||
.call(cwd = root, stdin = os.Inherit, stdout = os.Inherit, env = Launcher.extraEnv) | ||
|
||
bspProc = | ||
os.proc(Launcher.launcher, dirArgs, "bsp", "--protocol", "local", "--socket", bspFile) | ||
.spawn(cwd = root, stdin = os.Inherit, stdout = os.Inherit) | ||
|
||
val addr = UnixDomainSocketAddress.of(bspFile.toNIO) | ||
var connected = false | ||
var attemptCount = 0 | ||
|
||
while (!connected && bspProc.isAlive() && attemptCount < 10) { | ||
if (attemptCount > 0) | ||
Thread.sleep(1000L) | ||
attemptCount += 1 | ||
|
||
if (os.exists(bspFile)) { | ||
socket = SocketChannel.open(StandardProtocolFamily.UNIX) | ||
socket.connect(addr) | ||
socket.finishConnect() | ||
connected = true | ||
} | ||
} | ||
|
||
if (!connected) | ||
sys.error("Not connected to Bloop server via BSP :|") | ||
|
||
def sendMsg(msg: String): Unit = { | ||
val bytes = msg.getBytes(StandardCharsets.UTF_8) | ||
|
||
def doWrite(buf: ByteBuffer): Unit = | ||
if (buf.position() < buf.limit()) { | ||
val written = socket.write(buf) | ||
if (written == 0) | ||
Thread.sleep(100L) | ||
doWrite(buf) | ||
} | ||
|
||
doWrite(ByteBuffer.wrap( | ||
(s"Content-Length: ${bytes.length}" + "\r\n\r\n").getBytes(StandardCharsets.UTF_8) | ||
)) | ||
doWrite(ByteBuffer.wrap(bytes)) | ||
} | ||
|
||
sendMsg(dummyMsg) | ||
|
||
val arr = Array.ofDim[Byte](10 * 1024) | ||
val buf = ByteBuffer.wrap(arr) | ||
|
||
// seems to do the job… | ||
def doRead(buf: ByteBuffer, count: Int): Int = { | ||
val read = socket.read(buf) | ||
if (read <= 0 && count < 100) { | ||
Thread.sleep(100L) | ||
doRead(buf, count + 1) | ||
} | ||
else | ||
read | ||
} | ||
|
||
val read = doRead(buf, 0) | ||
assert(read > 0) | ||
|
||
val resp = new String(arr, 0, read, StandardCharsets.UTF_8) | ||
|
||
def validateJson(content: String): Boolean = { | ||
assert(content.startsWith("{")) | ||
assert(content.endsWith("}")) | ||
val chars = content.toCharArray | ||
val objCount = Array.ofDim[Int](chars.length) | ||
for (i <- 0 until chars.length) { | ||
val previous = if (i == 0) 0 else objCount(i - 1) | ||
val count = previous + (if (chars(i) == '{') 1 else if (chars(i) == '}') -1 else 0) | ||
objCount(i) = count | ||
} | ||
objCount.dropRight(1).forall(_ > 0) | ||
} | ||
|
||
resp.linesWithSeparators.toVector match { | ||
case Seq(cl, empty, other @ _*) | ||
if cl.startsWith("Content-Length:") && empty.trim.isEmpty => | ||
val json = other.mkString | ||
val validated = validateJson(json) | ||
if (!validated) | ||
pprint.err.log(json) | ||
assert(validated, "Unexpected JSON response shape") | ||
case _ => | ||
pprint.err.log(resp) | ||
sys.error("Unexpected response shape") | ||
} | ||
} | ||
finally { | ||
if (socket != null) | ||
socket.close() | ||
|
||
if (bspProc != null) { | ||
bspProc.waitFor(1000L) | ||
if (bspProc.isAlive()) | ||
bspProc.close() | ||
|
||
os.proc(Launcher.launcher, dirArgs, "exit") | ||
.call(cwd = root, stdin = os.Inherit, stdout = os.Inherit, check = false) | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the reason for the fork? Any chance to just include it in the library?
Btw. I just moved the fork to https://github.com/VirtusLab/bloop-core since I didn't want to bother you every time something broke (and release did actually break).
As a side note, I also plan to move most of the things back to original Bloop to get rid of the fork altogether, since we plan to switch it to JDK 17 soon. Would it be enough to just move bloop-rifle back? I don't see anything else that is hugely changed from the original Bloop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's just easier for me to have the fix be released and usable from here (I tried to upstream some changes in snailgun some time ago, but they're still not released…)
I intend to cut a release right after merging this, so I'll probably fix that
Good that the changes in shared / backend / frontend are upstreamed. But bloop-rifle should be used by the Bloop CLI, and so should live alongside it in the Bloop repo IMO. It doesn't really need to live in the Scala CLI repo anyway I think.