Skip to content

Commit

Permalink
Limit exposure to ConcurrentModificationException when sys props are …
Browse files Browse the repository at this point in the history
…replaced or mutated (#22180)

port of
scala/scala@f6859f2
to fix sbt/sbt#7873
  • Loading branch information
SethTisue authored Dec 20, 2024
2 parents 7ae2d65 + 31690d4 commit 0bfa1af
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions compiler/src/dotty/tools/dotc/config/PathResolver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,25 @@ object PathResolver {
/** Values found solely by inspecting environment or property variables.
*/
object Environment {
private def searchForBootClasspath = (
systemProperties find (_._1 endsWith ".boot.class.path") map (_._2) getOrElse ""
)
private def searchForBootClasspath = {
import scala.jdk.CollectionConverters.*
val props = System.getProperties
// This formulation should be immune to ConcurrentModificationExceptions when system properties
// we're unlucky enough to witness a partially published result of System.setProperty or direct
// mutation of the System property map. stringPropertyNames internally uses the Enumeration interface,
// rather than Iterator, and this disables the fail-fast ConcurrentModificationException.
val propNames = props.stringPropertyNames()
propNames.asScala collectFirst { case k if k endsWith ".boot.class.path" => props.getProperty(k) } getOrElse ""
}

/** Environment variables which java pays attention to so it
* seems we do as well.
*/
def classPathEnv: String = envOrElse("CLASSPATH", "")
def sourcePathEnv: String = envOrElse("SOURCEPATH", "")

def javaBootClassPath: String = propOrElse("sun.boot.class.path", searchForBootClasspath)
//using propOrNone/getOrElse instead of propOrElse so that searchForBootClasspath is lazy evaluated
def javaBootClassPath: String = propOrNone("sun.boot.class.path") getOrElse searchForBootClasspath

def javaExtDirs: String = propOrEmpty("java.ext.dirs")
def scalaHome: String = propOrEmpty("scala.home")
Expand Down

0 comments on commit 0bfa1af

Please sign in to comment.