Skip to content

Commit

Permalink
Avoid calling Discover[T] macro for sub-folder root modules (#3656)
Browse files Browse the repository at this point in the history
They already get processed as part of the root `build.mill`'s
`Discover[T]` macro call, so no need to duplicate the work
  • Loading branch information
lihaoyi authored Oct 3, 2024
1 parent 51f1429 commit e0a2c93
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
4 changes: 1 addition & 3 deletions main/src/mill/main/RootModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ object RootModule {
fileName = millFile0,
enclosing = Caller(null)
)
) with Module {
def millDiscover: Discover
}
) with Module {}

@deprecated
abstract class Foreign(foreign0: Option[Segments])(implicit
Expand Down
19 changes: 14 additions & 5 deletions runner/src/mill/runner/CodeGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,20 @@ object CodeGen {
newScriptCode = objectData.name.applyTo(newScriptCode, wrapperObjectName)
newScriptCode = objectData.obj.applyTo(newScriptCode, "abstract class")

val millDiscover =
if (segments.nonEmpty) ""
else
"""@_root_.scala.annotation.nowarn
| override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]""".stripMargin

s"""$pkgLine
|$aliasImports
|$prelude
|$markerComment
|$newScriptCode
|object $wrapperObjectName extends $wrapperObjectName {
| $childAliases
| @_root_.scala.annotation.nowarn
| override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]
| $millDiscover
|}""".stripMargin
case None =>
s"""$pkgLine
Expand Down Expand Up @@ -216,16 +221,20 @@ object CodeGen {
s"extends _root_.mill.runner.MillBuildRootModule() "
}
} else {

s"extends _root_.mill.main.RootModule.Subfolder "
}

val millDiscover =
if (segments.nonEmpty) ""
else
"""@_root_.scala.annotation.nowarn
| override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]""".stripMargin

// User code needs to be put in a separate class for proper submodule
// object initialization due to https://github.com/scala/scala3/issues/21444
s"""object $wrapperObjectName extends $wrapperObjectName{
| $childAliases
| @_root_.scala.annotation.nowarn
| override lazy val millDiscover: _root_.mill.define.Discover = _root_.mill.define.Discover[this.type]
| $millDiscover
|}
|abstract class $wrapperObjectName $extendsClause {""".stripMargin

Expand Down

0 comments on commit e0a2c93

Please sign in to comment.