Skip to content

Commit 0b237c2

Browse files
committed
Fix --offline mode for scala-cli as scala installation via coursier
1 parent 5744e9c commit 0b237c2

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

modules/cli/src/main/scala/scala/cli/commands/shared/SharedOptions.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ final case class SharedOptions(
411411
extraCompileOnlyJars = extraCompileOnlyClassPath,
412412
extraSourceJars = extraSourceJars.extractedClassPath ++ assumedSourceJars,
413413
extraRepositories =
414-
(dependencies.repository ++ ScalaCli.launcherOptions.scalaRunner.cliPredefinedRepository)
414+
(ScalaCli.launcherOptions.scalaRunner.cliPredefinedRepository ++ dependencies.repository)
415415
.map(_.trim)
416416
.filter(_.nonEmpty),
417417
extraDependencies = ShadowingSeq.from(

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

+57
Original file line numberDiff line numberDiff line change
@@ -847,4 +847,61 @@ class SipScalaTests extends ScalaCliSuite with SbtTestHelper with MillTestHelper
847847
expect(res.out.trim().contains(scalaVersion))
848848
}
849849
}
850+
851+
if (!Properties.isWin) // FIXME: run this test on Windows
852+
test("coursier scala installation works in --offline mode") {
853+
TestInputs.empty.fromRoot { root =>
854+
val localCache = root / "local-cache"
855+
val localBin = root / "local-bin"
856+
val sv = "3.5.0-RC4"
857+
os.proc(
858+
TestUtil.cs,
859+
"install",
860+
"--cache",
861+
localCache,
862+
"--install-dir",
863+
localBin,
864+
s"scala:$sv"
865+
).call(cwd = root)
866+
val scalaBinary: os.Path = localBin / "scala"
867+
val fileBytes = os.read.bytes(scalaBinary)
868+
val shebang = new String(fileBytes.takeWhile(_ != '\n'), "UTF-8")
869+
val binaryData = fileBytes.drop(shebang.length + 1)
870+
val execLine = new String(binaryData.takeWhile(_ != '\n'), "UTF-8")
871+
val scriptPathRegex = """exec "([^"]+/bin/scala).*"""".r
872+
val scalaScript = execLine match { case scriptPathRegex(extractedPath) => extractedPath }
873+
val scalaScriptPath = os.Path(scalaScript)
874+
val lineToChange = "eval \"${SCALA_CLI_CMD_BASH[@]}\" \\"
875+
// FIXME: the way the scala script calls the launcher currently ignores the --debug flag
876+
val newContent = os.read(scalaScriptPath).replace(
877+
lineToChange,
878+
s"""SCALA_CLI_CMD_BASH=(\"\\\"${TestUtil.cliPath}\\\"\")
879+
|$lineToChange""".stripMargin
880+
)
881+
os.write.over(scalaScriptPath, newContent)
882+
val r =
883+
os.proc(
884+
scalaScript,
885+
"--offline",
886+
"--power",
887+
"--with-compiler",
888+
"-e",
889+
"println(dotty.tools.dotc.config.Properties.versionNumberString)"
890+
).call(
891+
cwd = root,
892+
env = Map("COURSIER_CACHE" -> localCache.toString),
893+
check = false // need to clean up even on failure
894+
)
895+
// clean up cs local binaries
896+
val csPrebuiltBinaryDir =
897+
os.Path(scalaScript.substring(0, scalaScript.indexOf(sv) + sv.length))
898+
try os.remove.all(csPrebuiltBinaryDir)
899+
catch {
900+
case ex: java.nio.file.FileSystemException =>
901+
println(s"Failed to remove $csPrebuiltBinaryDir: $ex")
902+
}
903+
expect(r.exitCode == 0)
904+
expect(r.out.trim() == sv)
905+
}
906+
}
850907
}

modules/options/src/main/scala/scala/build/Artifacts.scala

+4-2
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,11 @@ object Artifacts {
624624
val forceVersion = forceScalaVersions ++ forcedVersions
625625

626626
// FIXME Many parameters that we could allow to customize here
627-
var fetcher = coursier.Fetch()
627+
val defaultFetcher = coursier.Fetch()
628+
var fetcher = defaultFetcher
628629
.withCache(cache)
629-
.addRepositories(extraRepositoriesWithFallback*)
630+
// repository order matters here, since in some cases coursier resolves only the head
631+
.withRepositories(extraRepositoriesWithFallback ++ defaultFetcher.repositories)
630632
.addDependencies(dependencies.map(_.value)*)
631633
.mapResolutionParams(_.addForceVersion(forceVersion*))
632634
for (classifiers <- classifiersOpt) {

0 commit comments

Comments
 (0)