0

What is the preferred way in mill to point to the directory where build.sc file is located?

In the mill documentation os.pwd is used e.g. here, but if it is possible to start/run mill from elsewhere, then os.pwd points to incorrect location.

import mill._, mill.modules.Jvm

def sourceRoot = T.sources { os.pwd / "src" }

def resourceRoot = T.sources { os.pwd / "resources" }
user4955663
  • 1,039
  • 2
  • 13
  • 21

1 Answers1

0

The root location of each defined module is defined in def millSourcePath: os.Path, and it's the preferred way to access the module path.

Example:

import mill._, mill.scalalib._

object mymodule extends JavaModule {
  def sources = T.sources(millSourcePath / "src")
}
$ mill show __.sources
No mill version specified.
You should provide a version via '.mill-version' file or --mill-version option.
Using mill version 0.9.9
Compiling /tmp/stackoverflow/build.sc
[1/1] show 
[1/1] show > [1/1] mymodule.sources 
[
    "ref:c984eca8:/tmp/stackoverflow/mymodule/src"
]

If you need to access the path of project itself, notice, that it's a mill.define.Module itself and you can access it via the variable build. The project directory is therefore accessible via build.millSourcePath.

Example:

import mill._, mill.scalalib._

val baseDir = build.millSourcePath

object mymodule extends JavaModule {
  def sources = T.sources {
    super.sources() ++ Seq(PathRef(baseDir / "src"))
  }
}
$ mill show __.sources
No mill version specified.
You should provide a version via '.mill-version' file or --mill-version option.
Using mill version 0.9.9
Compiling /tmp/stackoverflow/build.sc
[1/1] show 
[1/1] show > [2/2] mymodule.sources 
[
    "ref:c984eca8:/tmp/stackoverflow/mymodule/src",
    "ref:c984eca8:/tmp/stackoverflow/src"
]
Tobias Roeser
  • 431
  • 2
  • 8
  • Thank you! build.millSourcePath was exactly what I was looking for! It works but for some reason my Idea v2021.2.1 does not find it and wants to import mill.modules.Jvm.JarManifest.Default.build, which is def returning Manifest. – user4955663 Aug 31 '21 at 09:28
  • IntelliJ IDEA is using its Ammonite support to "understand" the `build.sc`. (Mill build scripts are also Ammonite scripts.) The exposed `build` object is Mill specific and therefore unknown to IJ. – Tobias Roeser Sep 01 '21 at 08:27