I have a scala target defined in `backend/BUILD` l...
# general
f
I have a scala target defined in
backend/BUILD
like so:
Copy code
scala_sources(
  name = 'main',
  sources = ['src/main/scala/**/*.scala'],
  dependencies = [':resources'],
  main = "com.mycompany.some.package.name.Main$"
)
I thought I'd be able to run the thing with`pants run backend:main`, but that fails with an error:
Copy code
TooManyTargetsException: The `run` goal only works with one valid target, but was given multiple valid targets:

<complete list of source files here>

Please select one of these targets to run.
I can indeed run it by specifying the .scala file that my
Main
object is defined in explicitly, but then what's the point of
scala_sources
having a
main
setting? That's not how it's supposed to work, right?
h
Hmmm, I think you've found a bug, I'm just not sure if the bug is that this doesn't work as documented, or if
scala_sources()
shouldn't have a
main
field at all
A
jvm_artifact
has a
main
, and that makes sense to me. But I'm not sure why a
sources
target would.
cc @witty-crayon-22786
A
scala_sources()
target is a generator for many individual
scala_source()
targets, one for each source file
So (and this is confirmed by looking at the implementation), the
main
field is defined on each
scala_source()
, which doesn't seem to make any sense
If you run a single scala source then obviously that is file you intended to run, no
main
required
So I think the bug is that
scala_source
has a
main
to begin with
As do
java_source
and
kotlin_source
, but presumably this is an error that persisted via copy-pasta
OK, further digging: This was added relatively recently, by @ancient-vegetable-10556, in https://github.com/pantsbuild/pants/pull/17847, which I reviewed. So obviously something important slipped by me then (or is now)
So I think @ancient-vegetable-10556 has the most knowledge and context here
a
I don't remember the context, but I think sources (not source) may have multiple main methods, so you should be able to disambiguate
h
Sure, but AFAICT we define
main
on the underlying
scala_source()
(singular) which doesn't seem to make sense
a
There are some languages (kotlin at least) where you can define multiple public classes in the same source file. But you're probably right that it doesn't make sense in the general case
f
It is possible (and common) to declare more than one class in a single .scala file
h
Yes, but usually only one of them will have a main()?
I suppose it is possible for multiple of them to
Well anyway, putting that to one side, it sounds like the functionality from https://github.com/pantsbuild/pants/pull/17847 doesn't actually work?
f
@happy-kitchen-89482 Well, when I specify a single source file it works
But it's inconvenient, especially given the Maven/sbt-style paths that people tend to start out with.
like
subproject/main/src/scala/com/company/some/package/Main.scala
in sbt I can just run that with
subproject/run
h
Right, but it sounds like the
main
field doesn't work? You have to specify the file specifically?