-
-
Notifications
You must be signed in to change notification settings - Fork 360
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use OS-Lib's
pwdDynamicFunction
to allow sandboxing of os.pwd
, wr…
…ite sandboxing doc page, introduce `RunModule#runner` (#3479) Also did some misc cleanup: `synchronizedEval` is now unnecessary since #3243 added a coarse grained lock around BSP, prettied up the `#threadId` prefixes, remove empty `.dest/` folders for tidyness `RunModule#runner` continues the pattern estabilished with `CoursierModule#defaultResolver`: a convenient way to bundle up the various tasks related to running a modules code in a way that lets downstream callers pass things to `run` while being able to override the default configs (e.g. `forkEnv`, `forkArgs`, etc.) and without the hassle of wrapping things in `T.task`s. If this pattern works out, it would serve as a great alternative to the "pass tasks as arguments to command" pattern we've used in the past. This makes the `11-module-run-task` example much nicer Also more CI optimizations
- Loading branch information
Showing
24 changed files
with
418 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
= Mill Sandboxing | ||
|
||
== Task Sandboxing | ||
|
||
include::example/depth/sandbox/1-task.adoc[] | ||
|
||
== Test Sandboxing | ||
|
||
include::example/depth/sandbox/2-test.adoc[] | ||
|
||
== Limitations | ||
|
||
Mill's approach to filesystem sandboxing is designed to avoid accidental interference | ||
between different Mill tasks. It is not designed to block intentional misbehavior, and | ||
tasks are always able to traverse the filesystem and do whatever they want. Furthermore, | ||
Mill's redirection of `os.pwd` does not apply to `java.io` or `java.nio` APIs, which are | ||
outside of Mill's control. | ||
|
||
However, by setting `os.pwd` to safe sandbox folders, we hope to minimize the cases where | ||
someone accidentally causes issues with their build by doing the wrong thing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// In order to help manage your build, Mill performs some rudimentary filesystem | ||
// sandboxing to keep different tasks and modules from interfering with each other. | ||
// This tries to ensure your tasks only read and write from their designated `.dest/` | ||
// folders, which are unique to each task and thus guaranteed not to collide with | ||
// the filesystem operations of other tasks that may be occurring in parallel. | ||
// | ||
// | ||
// === `T.dest` | ||
// The standard way of working with a task's `.dest/` folder is through the `T.dest` | ||
// property. This is available within any task, and gives you access to the | ||
// `out/<module-names>/<task-name>.dest/` folder to use. The `.dest/` folder for | ||
// each task is lazily initialized when `T.dest` is referenced and used: | ||
|
||
package build | ||
import mill._ | ||
|
||
object foo extends Module{ | ||
def tDestTask = T { println(T.dest.toString) } | ||
} | ||
|
||
/** Usage | ||
> ./mill foo.tDestTask | ||
.../out/foo/tDestTask.dest | ||
*/ | ||
|
||
|
||
// === Task `os.pwd` redirection | ||
// Mill also redirects the `os.pwd` property from https://github.com/com-lihaoyi/os-lib[OS-Lib], | ||
// such that that also points towards a running task's own `.dest/` folder | ||
|
||
def osPwdTask = T { println(os.pwd.toString) } | ||
|
||
/** Usage | ||
> ./mill osPwdTask | ||
.../out/osPwdTask.dest | ||
*/ | ||
|
||
// The redirection of `os.pwd` applies to `os.proc`, `os.call`, and `os.spawn` methods | ||
// as well. In the example below, we can see the `python3` subprocess we spawn prints | ||
// its `os.getcwd()`, which is our `osProcTask.dest/` sandbox folder: | ||
|
||
def osProcTask = T { | ||
println(os.call(("python3", "-c", "import os; print(os.getcwd())"), cwd = T.dest).out.trim()) | ||
} | ||
|
||
/** Usage | ||
> ./mill osProcTask | ||
.../out/osProcTask.dest | ||
*/ | ||
|
||
// === Non-task `os.pwd` redirection | ||
// | ||
// Lastly, there is the possibily of calling `os.pwd` outside of a task. When outside of | ||
// a task there is no `.dest/` folder associated, so instead Mill will redirect `os.pwd` | ||
// towards an empty `sandbox/` folder in `out/mill-worker.../`: | ||
|
||
val externalPwd = os.pwd | ||
def externalPwdTask = T { println(externalPwd.toString) } | ||
|
||
/** Usage | ||
> ./mill externalPwdTask | ||
.../out/mill-worker-.../sandbox/sandbox | ||
*/ | ||
|
||
|
||
// === Limitations of Mill's Sandboxing | ||
// | ||
// Mill's approach to filesystem sandboxing is designed to avoid accidental interference | ||
// between different Mill tasks. It is not designed to block intentional misbehavior, and | ||
// tasks are always able to traverse the filesystem and do whatever they want. Furthermore, | ||
// Mill's redirection of `os.pwd` does not apply to `java.io` or `java.nio` APIs, which are | ||
// outside of Mill's control. | ||
// | ||
// However, by setting `os.pwd` to safe sandbox folders, we hope to minimize the cases where | ||
// someone accidentally causes issues with their build by doing the wrong thing. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package bar; | ||
|
||
public class Bar { | ||
public static String generateHtml(String text) { | ||
return "<p>" + text + "</p>"; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
example/depth/sandbox/2-test/bar/test/src/bar/BarTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package bar; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Test; | ||
import java.nio.file.*; | ||
|
||
public class BarTests { | ||
@Test | ||
public void simple() throws Exception { | ||
String result = Bar.generateHtml("world"); | ||
Path path = Paths.get("generated.html"); | ||
Files.write(path, result.getBytes()); | ||
assertEquals("<p>world</p>", new String(Files.readAllBytes(path))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Mill also creates sandbox folders for test suites to run in. Consider the | ||
// following build with two modules `foo` and `bar`, and their test suites | ||
// `foo.test` and `bar.test`: | ||
|
||
package build | ||
import mill._, javalib._ | ||
|
||
trait MyModule extends JavaModule{ | ||
object test extends JavaTests with TestModule.Junit4 | ||
} | ||
|
||
object foo extends MyModule{ | ||
def moduleDeps = Seq(bar) | ||
} | ||
|
||
object bar extends MyModule | ||
|
||
// For the sake of the example, both test modules contain tests that exercise the | ||
// logic in their corresponding non-test module, but also do some basic filesystem | ||
// operations at the same time, writing out a `generated.html` file and then reading it: | ||
|
||
/** See Also: foo/src/foo/Foo.java */ | ||
/** See Also: foo/test/src/foo/FooTests.java */ | ||
/** See Also: bar/src/bar/Bar.java */ | ||
/** See Also: bar/test/src/bar/BarTests.java */ | ||
|
||
// Both test suites can be run via | ||
|
||
/** Usage | ||
> ./mill __.test | ||
*/ | ||
|
||
// Without sandboxing, due to the tests running in parallel, there is a race condition: | ||
// it's possible that `FooTests` may write the file, `BarTests` write over it, before | ||
// `FooTests` reads the output from `BarTests`. That would cause non-deterministic | ||
// flaky failures in your test suite that can be very difficult to debug and resolve. | ||
// | ||
// With Mill's test sandboxing, each test runs in a separate folder: the `.dest` folder | ||
// of the respective task and module. For example: | ||
// | ||
// - `foo.test` runs in `out/foo/test/test.dest/` | ||
// - `bar.test` runs in `out/bar/test/test.dest/` | ||
// | ||
// As a result, each test's `generated.html` file is written to its own dedicated | ||
// working directory, without colliding with each other on disk: | ||
|
||
/** Usage | ||
|
||
> find . | grep generated.html | ||
.../out/foo/test/test.dest/sandbox/generated.html | ||
.../out/bar/test/test.dest/sandbox/generated.html | ||
|
||
> cat out/foo/test/test.dest/sandbox/generated.html | ||
<h1>hello</h1> | ||
|
||
> cat out/bar/test/test.dest/sandbox/generated.html | ||
<p>world</p> | ||
|
||
*/ | ||
|
||
// As each test suite runs in a different working directory by default, naive usage | ||
// reading and writing to the filesystem does not cause tests to interefere with | ||
// one another, which helps keep tests stable and deterministic even when run in | ||
// parallel | ||
// | ||
// Like Mill's Task sandboxing, Mill's Test sandboxing does not guard against | ||
// intentional misbehavior: tests can still walk the filesystem from the | ||
// sandbox folder via `..` or from the root folder `/` or home folder `~/`. | ||
// Nevertheless, it should add some simple guardrails to prevent many common | ||
// causes of inter-test interference, letting your test suite run in parallel both | ||
// quickly and reliably |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package foo; | ||
|
||
public class Foo { | ||
public static String generateHtml(String text) { | ||
return "<h1>" + text + "</h1>"; | ||
} | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
example/depth/sandbox/2-test/foo/test/src/foo/FooTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package foo; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import org.junit.Test; | ||
import java.nio.file.*; | ||
|
||
public class FooTests { | ||
@Test | ||
public void simple() throws Exception { | ||
String result = Foo.generateHtml("hello"); | ||
Path path = Paths.get("generated.html"); | ||
Files.write(path, result.getBytes()); | ||
assertEquals("<h1>hello</h1>", new String(Files.readAllBytes(path))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.