Skip to content

Commit 31a88e4

Browse files
authored
Fix setup-ide for --cli-version (#3161)
1 parent 1f7fb77 commit 31a88e4

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

modules/cli/src/main/scala/scala/cli/ScalaCli.scala

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import java.util.Locale
1212
import scala.build.Directories
1313
import scala.build.internal.Constants
1414
import scala.build.internals.EnvVar
15+
import scala.cli.commands.CommandUtils
1516
import scala.cli.config.{ConfigDb, Keys}
1617
import scala.cli.internal.Argv0
1718
import scala.cli.javaLauncher.JavaLauncherCli
@@ -244,10 +245,26 @@ object ScalaCli {
244245
case Some(ver) =>
245246
val powerArgs = launcherOpts.powerOptions.toCliArgs
246247
val initialScalaRunnerArgs = launcherOpts.scalaRunner
247-
val finalScalaRunnerArgs =
248-
// if the version was specified, it doesn't make sense to check for CLI updates
249-
(if Version(ver) < Version("1.4.0") then initialScalaRunnerArgs
250-
else initialScalaRunnerArgs.copy(skipCliUpdates = Some(true))).toCliArgs
248+
val finalScalaRunnerArgs = (Version(ver) match
249+
case v if v < Version("1.4.0") && !ver.contains("nightly") =>
250+
initialScalaRunnerArgs.copy(
251+
skipCliUpdates = None,
252+
predefinedCliVersion = None,
253+
initialLauncherPath = None
254+
)
255+
case v if v < Version("1.5.1") && !ver.contains("nightly") =>
256+
initialScalaRunnerArgs.copy(
257+
predefinedCliVersion = None,
258+
initialLauncherPath = None
259+
)
260+
case _ if initialScalaRunnerArgs.initialLauncherPath.nonEmpty =>
261+
initialScalaRunnerArgs
262+
case _ =>
263+
initialScalaRunnerArgs.copy(
264+
predefinedCliVersion = Some(ver),
265+
initialLauncherPath = Some(CommandUtils.getAbsolutePathToScalaCli(progName))
266+
)
267+
).toCliArgs
251268
val newArgs = powerArgs ++ finalScalaRunnerArgs ++ args0
252269
LauncherCli.runAndExit(ver, launcherOpts, newArgs)
253270
case _ if

modules/cli/src/main/scala/scala/cli/commands/setupide/SetupIde.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,14 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
154154
s"-J-agentlib:jdwp=transport=dt_socket,server=n,address=localhost:$port,suspend=y"
155155
)
156156

157+
val launcher = launcherOptions.scalaRunner.initialLauncherPath
158+
.getOrElse(CommandUtils.getAbsolutePathToScalaCli(progName))
159+
val finalLauncherOptions = launcherOptions.copy(cliVersion =
160+
launcherOptions.cliVersion.orElse(launcherOptions.scalaRunner.predefinedCliVersion)
161+
)
157162
val bspArgs =
158-
List(CommandUtils.getAbsolutePathToScalaCli(progName)) ++
159-
launcherOptions.toCliArgs ++
163+
List(launcher) ++
164+
finalLauncherOptions.toCliArgs ++
160165
List("bsp") ++
161166
debugOpt ++
162167
List("--json-options", scalaCliBspJsonDestination.toString) ++
@@ -184,7 +189,7 @@ object SetupIde extends ScalaCommand[SetupIdeOptions] {
184189

185190
val json = gson.toJson(details)
186191
val scalaCliOptionsForBspJson = writeToArray(options.shared)(SharedOptions.jsonCodec)
187-
val scalaCliLaunchOptsForBspJson = writeToArray(launcherOptions)(LauncherOptions.jsonCodec)
192+
val scalaCliLaunchOptsForBspJson = writeToArray(finalLauncherOptions)(LauncherOptions.jsonCodec)
188193
val scalaCliBspInputsJson = writeToArray(ideInputs)
189194
val envsForBsp = sys.env.filter((key, _) => EnvVar.allBsp.map(_.name).contains(key))
190195
val scalaCliBspEnvsJson = writeToArray(envsForBsp)

modules/cli/src/main/scala/scala/cli/launcher/ScalaRunnerLauncherOptions.scala

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,22 @@ case class ScalaRunnerLauncherOptions(
3939
)
4040
@Hidden
4141
@Tag(tags.implementation)
42-
skipCliUpdates: Option[Boolean] = None
42+
skipCliUpdates: Option[Boolean] = None,
43+
@Hidden
44+
@Tag(tags.implementation)
45+
predefinedCliVersion: Option[String] = None,
46+
@Hidden
47+
@Tag(tags.implementation)
48+
@Name("initialLauncher")
49+
initialLauncherPath: Option[String] = None
4350
) {
4451
def toCliArgs: List[String] =
4552
cliUserScalaVersion.toList.flatMap(v => List("--cli-default-scala-version", v)) ++
4653
cliPredefinedRepository.flatMap(v => List("--repository", v)) ++
47-
progName.toList.flatMap(v => List("--prog-name", v))
54+
progName.toList.flatMap(v => List("--prog-name", v)) ++
55+
skipCliUpdates.toList.filter(v => v).map(_ => "--skip-cli-updates") ++
56+
predefinedCliVersion.toList.flatMap(v => List("--predefined-cli-version", v)) ++
57+
initialLauncherPath.toList.flatMap(v => List("--initial-launcher-path", v))
4858
}
4959

5060
object ScalaRunnerLauncherOptions {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,4 +2162,22 @@ abstract class BspTestDefinitions extends ScalaCliSuite with TestScalaVersionArg
21622162
}
21632163
}
21642164
}
2165+
2166+
// TODO: uncomment when the fix for https://github.com/VirtusLab/scala-cli/issues/3157 is on nightly
2167+
// test("setup-ide prepares a valid BSP configuration with --cli-version") {
2168+
// val scriptName = "cli-version.sc"
2169+
// val cliVersion = "nightly"
2170+
// val inputs = TestInputs(os.rel / scriptName -> s"""println("Hello from launcher v$cliVersion""")
2171+
// inputs.fromRoot { root =>
2172+
// val cliVersionArgs = List("--cli-version", cliVersion)
2173+
// os.proc(TestUtil.cli, cliVersionArgs, "setup-ide", scriptName, extraOptions).call(cwd = root)
2174+
// val expectedIdeLauncherFile = root / Constants.workspaceDirName / "ide-launcher-options.json"
2175+
// expect(expectedIdeLauncherFile.toNIO.toFile.exists())
2176+
// expect(os.read(expectedIdeLauncherFile).contains(cliVersion))
2177+
// val bspConfig = readBspConfig(root)
2178+
// expect(bspConfig.argv.head == TestUtil.cliPath)
2179+
// expect(bspConfig.argv.containsSlice(cliVersionArgs))
2180+
// expect(bspConfig.argv.indexOfSlice(cliVersionArgs) < bspConfig.argv.indexOf("bsp"))
2181+
// }
2182+
// }
21652183
}

0 commit comments

Comments
 (0)