Skip to content

Better compiler error when a build package and a task clash - #6903

Merged
lihaoyi merged 2 commits into
com-lihaoyi:mainfrom
alexarchambault:tweak-mill-files-wrapping
Mar 5, 2026
Merged

lihaoyi merged 2 commits into
com-lihaoyi:mainfrom
alexarchambault:tweak-mill-files-wrapping

Conversation

@alexarchambault

@alexarchambault alexarchambault commented Mar 5, 2026 •

Copy link
Copy Markdown
Collaborator

This very slightly changes the way Mill refers to other packages in the build, in the header it adds to *.mill files, in order to get better compilation errors in some cases.

Namely, when one has a thing package say

// thing/package.mill
package build.thing

object `package` extends Module

and mistakenly tries to define a thing task at the root of the build:

// build.mill
package build

import mill.*

def thing() = Task("")

Without this PR's changes, one gets an error looking like

[error] out/mill-build/generatedScriptSources.dest/wrapped/build_/build.mill:12:18
    final lazy val thing: _root_.build_.thing.package_.type = _root_.build_.thing.package_ // subfolder module reference
                   ^
  error overriding method thing in class package_ of type => mill.api.Task.Simple[String];
    lazy value thing of type build_.thing.package_.type needs `override` modifier

With this PR, one gets something like

[error] build.mill:5:5
def thing = Task("")
    ^
Conflicting definitions:
def thing: build_.thing.package_.type in class package_ at line 20 and
def thing: mill.api.Task.Simple[String] in class package_ at line 27
have the same type (): mill.api.Task.Simple after erasure.

Consider adding a @targetName annotation to one of the conflicting definitions
for disambiguation.

With this PR's changes, the error points at an error in the user code (user's build.mill) rather than in Mill generated sources. This helps make sense of the error, but is also important in IDEs, where we get an error diagnostic at the right location.

About changes in the wrapping, this basically adds one line per subfolder reference, at the beginning of abstract class package_, like

package build_

import build_.{package_ => build}

import MillMiscInfo.*
import mill.util.TokenReaders.given
import mill.api.JsonFormatters.given

object package_ extends package_ {
  final lazy val foo: build_.foo.package_.type = build_.foo.package_

  private lazy val __millPackageObjectRefs: Array[mill.api.Module] =
    Array(build_.foo.package_.asInstanceOf[mill.api.Module])
  override lazy val millDiscover: mill.api.Discover = mill.api.Discover[this.type]
}

abstract class package_ extends mill.util.MainRootModule { this: package_.type =>
  def foo: build_.foo.package_.type // <-- added line by this PR's changes
//SOURCECODE_ORIGINAL_FILE_PATH=…/build.mill
//SOURCECODE_ORIGINAL_CODE_START_MARKER

// …
// build.mill content
// …

}

(edited for clarity)

This basically makes the user task try to redefine an existing method, rather than Mill's generated code doing it.

@alexarchambault
alexarchambault marked this pull request as ready for review March 5, 2026 19:49
@alexarchambault alexarchambault changed the title Better compilerr error when a build package and a task clash Better compiler error when a build package and a task clash Mar 5, 2026
@lihaoyi
lihaoyi merged commit 6d14ed1 into com-lihaoyi:main Mar 5, 2026
46 checks passed
@lefou lefou added this to the 1.1.3 milestone Mar 6, 2026
@alexarchambault
alexarchambault deleted the tweak-mill-files-wrapping branch March 9, 2026 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants