Skip to content

Commit bbd0cca

Browse files
committed
remove redundant import; extend io/ClasspathTest.scala ; add classpathTest.sc
1 parent c58106c commit bbd0cca

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!dist/target/pack/bin/scala -classpath "dist/target/pack/lib/*"
2+
!#
3+
4+
/*
5+
* a single wildcard -classpath entry is vulnerable to being converted by Windows jdk
6+
* to a list of jar files. To prevent this Windows-only behavior, a semicolon is
7+
* added to the classpath entry by dist/bin/scala to prevent globbing.
8+
*
9+
* The symptom of failure is that args contains lots of jar file paths.
10+
*/
11+
import java.nio.file.Paths
12+
13+
// expecting classpath to contain scala compiler lib jars
14+
def main(args: Array[String]): Unit =
15+
assert(args.isEmpty,s"args contains ${args.length} arguments, but was expecting none")
16+
17+
val expected = Paths.get("dist/target/pack/lib").toFile.listFiles.toList.map { _.getName }.filter { _.endsWith(".jar") }
18+
def psep = java.io.File.pathSeparator
19+
val classpath = sys.props("java.class.path")
20+
val entries = classpath.split(psep).map { Paths.get(_).toFile.getName }
21+
22+
val expectSet = expected.toSet
23+
val entrySet = entries.toSet
24+
25+
if ((expectSet intersect entrySet).isEmpty) then
26+
for entry <- expected do
27+
printf("expect:%s\n",entry)
28+
for entry <- entries do
29+
printf("found:%s\n",entry)
30+
31+
assert ((expectSet intersect entrySet).nonEmpty, s"expected[${expected.size}] but found [${entries.size}]")
32+
33+
printf("success!\n")

compiler/test/dotty/tools/io/ClasspathTest.scala

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,43 @@ class ClasspathTest {
1919
// Verify that Windows users not forced to use backslash in classpath.
2020
//
2121
@Test def testWildcards(): Unit =
22-
import dotty.tools.io.ClassPath
2322
val outDir = Files.createTempDirectory("classpath-test")
2423
try
2524
val compilerLib = "dist/target/pack/lib"
2625
val libdir = Paths.get(compilerLib).toFile
2726
if libdir.exists then
28-
try for src <- libdir.listFiles.toList.take(5) do
29-
val dest = Paths.get(s"$outDir/${src.getName}")
30-
printf("copy: %s\n",Files.copy(src.toPath,dest)) // ,REPLACE_EXISTING,COPY_ATTRIBUTES))
27+
val libjarFiles = libdir.listFiles.toList.take(5)
28+
try
29+
for src <- libjarFiles do
30+
val dest = Paths.get(s"$outDir/${src.getName}")
31+
printf("copy: %s\n",Files.copy(src.toPath,dest)) // ,REPLACE_EXISTING,COPY_ATTRIBUTES))
32+
33+
val cp = Seq(s"$outDir/*","not-a-real-directory/*").mkString(pathsep).replace('\\','/')
34+
35+
val libjars = libjarFiles.map { _.getName }.toSet
36+
37+
// expand wildcard classpath entries, ignoring invalid entries
38+
val entries = ClassPath.expandPath(cp).map { Paths.get(_).toFile.getName }
39+
40+
// require one-to-one matches
41+
assert(libjars == entries.toSet)
42+
43+
printf("%d entries\n",entries.size)
44+
printf("%d libjars\n",libjars.size)
45+
46+
for entry <- libjars do
47+
printf("libdir[%s]\n",entry)
48+
49+
for entry <- entries do
50+
printf("expand[%s]\n",entry)
51+
52+
// verify that expanded classpath has expected jar names
53+
for jar <- libjars do
54+
assert(entries.contains(jar))
55+
3156
catch
32-
case _:NullPointerException => // ignore errors adding jars to outDir
33-
34-
//outDir.toFile.listFiles.toList.foreach { printf("%s\n",_) }
35-
val cp = Seq(s"$compilerLib/*",s"$outDir/*","not-a-real-directory/*").mkString(pathsep).replace('\\','/')
36-
37-
// need to expand wildcard classpath entries
38-
val entries = ClassPath.expandPath(cp)
39-
for entry <- entries.take(10) do
40-
println(entry)
57+
case _:NullPointerException => // no test if unable to copy jars to outDir
58+
4159

4260
finally
4361
deleteFile(outDir.toFile)

dist/bin/scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ truncated_params="${*#-}"
5353
# options_indicator != 0 if at least one parameter is not an option
5454
options_indicator=$(( ${#all_params} - ${#truncated_params} - $# ))
5555

56-
[ -n "$SCALA_OPTS" ] && set -- $SCALA_OPTS "$@"
56+
[ -n "$SCALA_OPTS" ] && set -- $SCALA_OPTS $@
5757

5858
while [[ $# -gt 0 ]]; do
5959
case "$1" in
@@ -67,6 +67,13 @@ while [[ $# -gt 0 ]]; do
6767
;;
6868
-cp | -classpath)
6969
CLASS_PATH="$2"
70+
if [[ $cygwin || $mingw || $msys ]]; then
71+
# in Windows, need to protect a single wildcard entry from jdk globbing.
72+
# (jdk globbing replaces a single wildcard arg to multiple jar file args!)
73+
if [[ $CLASS_PATH =~ \* && ! $CLASS_PATH =~ \; ]]; then
74+
CLASS_PATH=";$CLASS_PATH"
75+
fi
76+
fi
7077
class_path_count+=1
7178
shift
7279
shift

0 commit comments

Comments
 (0)