VirtusLab / scala-cli

Scala CLI is a command-line tool to interact with the Scala language. It lets you compile, run, test, and package your Scala code (and more!)
https://scala-cli.virtuslab.org
Apache License 2.0
550 stars 129 forks source link

problems if one or more jars in classpath have "Main-Class" defined in MANIFEST.MF #3137

Closed philwalk closed 1 month ago

philwalk commented 2 months ago

Scala-Cli Version

# scala-cli version
Scala CLI version: 1.5.0
Scala version (default): 3.5.0

When attempting to test the new @arguments file feature, as suggested here, it fails if any entries in the classpath have a Main-Class property defined in the MANIFEST.MF.

To Reproduce

Create an args.txt to define a classpath having one or more executable jar files (any jar file with Main-Class defined in the MANIFEST.MF).

-cp postgresql-42.7.4.jar

scala-cli run -e 'println("Hey, I am using an @args file!")' @args.txt

Here's what happens with the above example @args file:

# scala-cli run -e 'println("Hey, I am using an @args file!")' @args.txt
Compiling project (Scala 3.5.0, JVM (21))
Compiled project (Scala 3.5.0, JVM (21))
Running org.postgresql.util.PGJDBCMain. Also detected script main classes: snippet_sc
You can run any one of them by passing option --main-class, i.e. --main-class snippet_sc
All available main classes can always be listed by passing option --list-main-classes

PostgreSQL JDBC Driver 42.7.4
Found in: jar:file:/C:/Users/philwalk/AppData/Local/https/repo1.maven.org/maven2/org/postgresql/postgresql/42.7.4/postgresql-42.7.4.jar!/org/postgresql/Driver.class

The PgJDBC driver is not an executable Java program.

You must install it according to the JDBC driver installation instructions for your application / container / appserver, then use it by specifying a JDBC URL of the form
    jdbc:postgresql://
or using an application specific method.

See the PgJDBC documentation: http://jdbc.postgresql.org/documentation/head/index.html

This command has had no effect.

Here are a few common libraries that are executable, no doubt there are many more:

rscala_2.13-3.2.19.jar
postgresql-42.7.4.jar
mdoc_3-2.3.6.jar
ldtp-4.0.jar
jna-5.14.0.jar
itext-2.1.5.jar
icu4j-59.1.jar
commons-compress-1.26.2.jar
byte-buddy-1.15.0.jar
xnio-nio-3.8.8.Final.jar
xnio-api-3.8.8.Final.jar
scala3-compiler_3-3.1.3.jar

Expected behaviour A classpath defined in an @arguments file should be prepended to the classpath, but should not otherwise affect the behavior of scala-cli.

Gedochao commented 2 months ago

This is likely caused by https://github.com/VirtusLab/scala-cli/pull/3079 We are now looking for main classes in dependency JARs as well, which should perhaps be opt in behind a flag, unless no explicit inputs are passed.