Skip to content

Commit 73a1cbe

Browse files
committed
Be better at starting Code
On Windows, use "cmd.exe /C", this seems to be necessary. On every OS, pass "-n" to Code to start a new instance, this makes things more reproducible.
1 parent 380be44 commit 73a1cbe

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

project/Build.scala

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import sbt.Package.ManifestAttributes
1515
import com.typesafe.sbteclipse.plugin.EclipsePlugin._
1616

1717
import dotty.tools.sbtplugin.DottyPlugin.autoImport._
18+
import dotty.tools.sbtplugin.DottyIDEPlugin.runProcess
1819
import dotty.tools.sbtplugin.DottyIDEPlugin.autoImport._
1920
import org.scalajs.sbtplugin.ScalaJSPlugin
2021
import org.scalajs.sbtplugin.ScalaJSPlugin.autoImport._
@@ -766,8 +767,13 @@ object Build {
766767
val mainClass = "dotty.tools.languageserver.Main"
767768
val extensionPath = (baseDirectory in `vscode-dotty`).value.getAbsolutePath
768769

769-
val codeArgs = if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs
770-
val allArgs = List("-client_command", "code", s"--extensionDevelopmentPath=$extensionPath") ++ codeArgs
770+
val codeArgs =
771+
s"--extensionDevelopmentPath=$extensionPath" +:
772+
(if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs)
773+
774+
val clientCommand = codeCommand.value ++ codeArgs
775+
776+
val allArgs = "-client_command" +: clientCommand
771777

772778
runTask(Runtime, mainClass, allArgs: _*)
773779
}.dependsOn(compile in (`vscode-dotty`, Compile)).evaluated
@@ -893,7 +899,7 @@ object Build {
893899

894900

895901
sbtPlugin := true,
896-
version := "0.1.2",
902+
version := "0.1.3-SNAPSHOT",
897903
ScriptedPlugin.scriptedSettings,
898904
ScriptedPlugin.sbtTestDirectory := baseDirectory.value / "sbt-test",
899905
ScriptedPlugin.scriptedBufferLog := false,
@@ -944,12 +950,7 @@ object Build {
944950
// Currently, vscode-dotty depends on daltonjorge.scala for syntax highlighting,
945951
// this is not automatically installed when starting the extension in development mode
946952
// (--extensionDevelopmentPath=...)
947-
val exitCodeInstall = new java.lang.ProcessBuilder("code", "--install-extension", "daltonjorge.scala")
948-
.inheritIO()
949-
.start()
950-
.waitFor()
951-
if (exitCodeInstall != 0)
952-
throw new MessageOnlyException("Installing dependency daltonjorge.scala failed")
953+
runProcess(codeCommand.value ++ Seq("--install-extension", "daltonjorge.scala"), wait = true)
953954

954955
sbt.inc.Analysis.Empty
955956
},
@@ -986,14 +987,9 @@ object Build {
986987
val inputArgs = spaceDelimited("<arg>").parsed
987988
val codeArgs = if (inputArgs.isEmpty) List((baseDirectory.value / "..").getAbsolutePath) else inputArgs
988989
val extensionPath = baseDirectory.value.getAbsolutePath
989-
val processArgs = List("code", s"--extensionDevelopmentPath=${extensionPath}") ++ codeArgs
990+
val processArgs = List(s"--extensionDevelopmentPath=${extensionPath}") ++ codeArgs
990991

991-
val exitCode = new java.lang.ProcessBuilder(processArgs: _*)
992-
.inheritIO()
993-
.start()
994-
.waitFor()
995-
if (exitCode != 0)
996-
throw new MessageOnlyException("Running Visual Studio Code failed")
992+
runProcess(codeCommand.value ++ processArgs, wait = true)
997993
}.dependsOn(compile in Compile).evaluated
998994
)
999995

sbt-dotty/src/dotty/tools/sbtplugin/DottyIDEPlugin.scala

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sbt.Keys._
55
import java.io._
66
import java.lang.ProcessBuilder
77
import scala.collection.mutable
8+
import scala.util.Properties.{ isWin, isMac }
89

910
import dotty.tools.languageserver.config.ProjectConfig
1011

@@ -123,9 +124,24 @@ object DottyIDEPlugin extends AutoPlugin {
123124
runTask(joinedTask, state)
124125
}
125126

127+
def runProcess(cmd: Seq[String], wait: Boolean = false, directory: File = null): Unit = {
128+
val pb0 = new ProcessBuilder(cmd: _*).inheritIO()
129+
val pb = if (directory != null) pb0.directory(directory) else pb0
130+
if (wait) {
131+
val exitCode = pb.start().waitFor()
132+
if (exitCode != 0) {
133+
val cmdString = cmd.mkString(" ")
134+
throw new MessageOnlyException("""Running command "${cmdString}" failed.""")
135+
}
136+
}
137+
else
138+
pb.start()
139+
}
140+
126141
private val projectConfig = taskKey[Option[ProjectConfig]]("")
127142

128143
object autoImport {
144+
val codeCommand = taskKey[Seq[String]]("Command to start VSCode")
129145
val runCode = taskKey[Unit]("Start VSCode, usually called from launchIDE")
130146
val launchIDE = taskKey[Unit]("Configure and run VSCode on this project")
131147
}
@@ -203,17 +219,16 @@ object DottyIDEPlugin extends AutoPlugin {
203219
override def buildSettings: Seq[Setting[_]] = Seq(
204220
commands ++= Seq(configureIDE, compileForIDE),
205221

222+
codeCommand := {
223+
if (isWin)
224+
Seq("cmd.exe", "/C", "code", "-n")
225+
else
226+
Seq("code", "-n")
227+
},
228+
206229
runCode := {
207-
val exitCode = new ProcessBuilder("code", "--install-extension", "lampepfl.dotty")
208-
.inheritIO()
209-
.start()
210-
.waitFor()
211-
if (exitCode != 0)
212-
throw new MessageOnlyException("Installing the Dotty support for VSCode failed")
213-
214-
new ProcessBuilder("code", baseDirectory.value.getAbsolutePath)
215-
.inheritIO()
216-
.start()
230+
runProcess(codeCommand.value ++ Seq("--install-extension", "lampepfl.dotty"), wait = true)
231+
runProcess(codeCommand.value ++ Seq("."), directory = baseDirectory.value)
217232
}
218233

219234
) ++ addCommandAlias("launchIDE", ";configureIDE;runCode")

0 commit comments

Comments
 (0)