Skip to content

Commit 8ba258c

Browse files
stevedlawrenceckipp01
authored andcommitted
Allow sbt-scoverage to work on Windows
The -Xplugin option expects a classpath string with multiple paths separated by a semicolon or colon depending on the operating system. However, this currently always uses a colon, which doesn't work on Windows. This means scalac cannot find the scoverage plugin jars and leads to errors about "bad options". This modifies the -Xplugin logic to use File.pathSeparator when building the Xplugin classpath, allowing it to work regardless of operating system. Also modifies excludedPackages when on Windows to replace Unix path separators with Windows path separators (which are escaped because it is treated as a regular expression). Modifies tests to remove unnecessary escaping of Unix separators that results in an invalid regex due to this replacement on Windows. Note that two tests currently prevent CI from working on Windows: coverage-off and preserve-set. The reason for these failures on Windows is not clear, but these changes at least allow the common uses to work on Windows.
1 parent 98c69fb commit 8ba258c

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ on:
1111

1212
jobs:
1313
test:
14-
runs-on: ubuntu-latest
14+
runs-on: ${{ matrix.os }}
1515
strategy:
1616
matrix:
1717
java: [ '8', '17' ]
18+
os: [ 'ubuntu-latest' ]
1819

1920
steps:
2021
- name: checkout the repo

src/main/scala/scoverage/ScoverageSbtPlugin.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scoverage
22

33
import sbt.Keys._
44
import sbt._
5+
import sbt.internal.util.Util.isWindows
56
import sbt.plugins.JvmPlugin
67
import scoverage.reporter.CoberturaXmlWriter
78
import scoverage.domain.Constants
@@ -147,7 +148,7 @@ object ScoverageSbtPlugin extends AutoPlugin {
147148

148149
Seq(
149150
Some(
150-
s"-Xplugin:${pluginPaths.mkString(":")}"
151+
s"-Xplugin:${pluginPaths.mkString(java.io.File.pathSeparator)}"
151152
),
152153
Some(
153154
s"-P:scoverage:dataDir:${coverageDataDir.value.getAbsolutePath}/scoverage-data"
@@ -160,6 +161,13 @@ object ScoverageSbtPlugin extends AutoPlugin {
160161
.map(v => s"-P:scoverage:excludedPackages:$v"),
161162
Option(coverageExcludedFiles.value.trim)
162163
.filter(_.nonEmpty)
164+
.map(v =>
165+
// On windows, replace unix file separators with windows file
166+
// separators. Note that we need to replace / with \\ because
167+
// the plugin treats this string as a regular expression and
168+
// backslashes must be escaped in regular expressions.
169+
if (isWindows) v.replace("/", """\\""") else v
170+
)
163171
.map(v => s"-P:scoverage:excludedFiles:$v"),
164172
Some("-P:scoverage:reportTestName"),
165173
// rangepos is broken in some releases of scala so option to turn it off

src/sbt-test/scoverage/coverage-excluded-files/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ scalaVersion := "2.13.6"
44

55
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test
66

7-
coverageExcludedFiles := ".*\\/two\\/GoodCoverage"
7+
coverageExcludedFiles := ".*/two/GoodCoverage"
88

99
resolvers ++= {
1010
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))

src/sbt-test/scoverage/scala3-coverage-excluded-files/build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ scalaVersion := "3.2.0-RC1"
44

55
libraryDependencies += "org.scalameta" %% "munit" % "0.7.29" % Test
66

7-
coverageExcludedFiles := ".*\\/two\\/GoodCoverage"
7+
coverageExcludedFiles := ".*/two/GoodCoverage"
88

99
resolvers ++= {
1010
if (sys.props.get("plugin.version").exists(_.endsWith("-SNAPSHOT")))

0 commit comments

Comments
 (0)