Skip to content

Implement :jar (deprecate :require) #22343

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add wrapper around :jar to avoid crashing repl on invalid jar
  • Loading branch information
aherlihy committed Mar 10, 2025
commit 42c4e89c738042f32690de7a2a70fad68bfeb213
50 changes: 27 additions & 23 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,6 @@ class ReplDriver(settings: Array[String],
if (f.isClassContainer) f.iterator.flatMap(flatten)
else Iterator(f)

val entries = flatten(jarFile)

def tryClassLoad(classFile: AbstractFile): Option[String] = {
val input = classFile.input
try {
Expand All @@ -543,27 +541,33 @@ class ReplDriver(settings: Array[String],
}
}

val existingClass = entries.filter(_.ext.isClass).find(tryClassLoad(_).isDefined)
if (existingClass.nonEmpty)
out.println(s"The path '$path' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
state
else inContext(state.context):
val jarClassPath = ClassPathFactory.newClassPath(jarFile)
val prevOutputDir = ctx.settings.outputDir.value

// add to compiler class path
ctx.platform.addToClassPath(jarClassPath)
SymbolLoaders.mergeNewEntries(defn.RootClass, ClassPath.RootPackage, jarClassPath, ctx.platform.classPath)

// new class loader with previous output dir and specified jar
val prevClassLoader = rendering.classLoader()
val jarClassLoader = fromURLsParallelCapable(
jarClassPath.asURLs, prevClassLoader)
rendering.myClassLoader = new AbstractFileClassLoader(
prevOutputDir, jarClassLoader)

out.println(s"Added '$path' to classpath.")
state
try {
val entries = flatten(jarFile)

val existingClass = entries.filter(_.ext.isClass).find(tryClassLoad(_).isDefined)
if (existingClass.nonEmpty)
out.println(s"The path '$path' cannot be loaded, it contains a classfile that already exists on the classpath: ${existingClass.get}")
else inContext(state.context):
val jarClassPath = ClassPathFactory.newClassPath(jarFile)
val prevOutputDir = ctx.settings.outputDir.value

// add to compiler class path
ctx.platform.addToClassPath(jarClassPath)
SymbolLoaders.mergeNewEntries(defn.RootClass, ClassPath.RootPackage, jarClassPath, ctx.platform.classPath)

// new class loader with previous output dir and specified jar
val prevClassLoader = rendering.classLoader()
val jarClassLoader = fromURLsParallelCapable(
jarClassPath.asURLs, prevClassLoader)
rendering.myClassLoader = new AbstractFileClassLoader(
prevOutputDir, jarClassLoader)

out.println(s"Added '$path' to classpath.")
} catch {
case e: Throwable =>
out.println(s"Failed to load '$path' to classpath: ${e.getMessage}")
}
state

case KindOf(expr) =>
out.println(s"""The :kind command is not currently supported.""")
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/jar-errors
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ scala>:jar sbt-test/source-dependencies/canon/actual/a.jar
The path 'sbt-test/source-dependencies/canon/actual/a.jar' cannot be loaded, it contains a classfile that already exists on the classpath: sbt-test/source-dependencies/canon/actual/a.jar(A.class)

scala>:require sbt-test/source-dependencies/canon/actual/a.jar
:require has been deprecated and replaced with :jar. Please use :jar
:require is no longer supported, but has been replaced with :jar. Please use :jar
Loading