scalabuild-toolsmill

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


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" }

Solution

  • 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"
    ]