Skip to content

Commit 5561ad6

Browse files
authored
Allow even more dynamic instrumentation of pwd via os.dynamicPwdFunction (#299)
Follow up to #298, which it turns out was not flexible enough to do what we need in Mill. Mill uses references to the `pwd` to create the `.dest` folder lazily, thus we cannot just assign a value to the `os.pwd` but actually need to assign a lambda that can be replaced to implement the lazy evaluation I pulled the trigger too quickly on releasing 0.10.6 with `os.dynamicPwd`, so this one has to be named differently. I chose `os.dynamicPwdFunction` and will make sure to test it with an unstable release before releasing a stable tag
1 parent 568bc4e commit 5561ad6

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

Readme.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,11 @@ string, int or set representations of the `os.PermSet` via:
22472247

22482248
== Changelog
22492249

2250+
[#main]
2251+
=== main
2252+
2253+
* Make `os.pwd` modifiable via the `os.dynamicPwd0`
2254+
22502255
[#0-10-6]
22512256
=== 0.10.6
22522257

os/src-jvm/package.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,19 @@ package object os {
3939
/**
4040
* The current working directory for this process.
4141
*/
42-
def pwd: Path = dynamicPwd.value
43-
val dynamicPwd: DynamicVariable[Path] =
44-
new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath))
42+
def pwd: Path = dynamicPwdFunction.value()
43+
44+
private val pwd0 = os.Path(java.nio.file.Paths.get(".").toAbsolutePath)
45+
46+
/**
47+
* Used to override `pwd` within a certain scope with a generated value
48+
*/
49+
val dynamicPwdFunction: DynamicVariable[() => Path] = new DynamicVariable(() => dynamicPwd.value)
50+
51+
/**
52+
* Used to override `pwd` within a certain scope with a fixed value
53+
*/
54+
val dynamicPwd: DynamicVariable[Path] = new DynamicVariable(pwd0)
4555

4656
val up: RelPath = RelPath.up
4757

os/src-native/package.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,19 @@ package object os {
3232
/**
3333
* The current working directory for this process.
3434
*/
35-
def pwd: Path = dynamicPwd.value
36-
val dynamicPwd: DynamicVariable[Path] =
37-
new DynamicVariable(os.Path(java.nio.file.Paths.get(".").toAbsolutePath))
35+
def pwd: Path = dynamicPwdFunction.value()
36+
37+
private val pwd0 = os.Path(java.nio.file.Paths.get(".").toAbsolutePath)
38+
39+
/**
40+
* Used to override `pwd` within a certain scope with a generated value
41+
*/
42+
val dynamicPwdFunction: DynamicVariable[() => Path] = new DynamicVariable(() => dynamicPwd.value)
43+
44+
/**
45+
* Used to override `pwd` within a certain scope with a fixed value
46+
*/
47+
val dynamicPwd: DynamicVariable[Path] = new DynamicVariable(pwd0)
3848

3949
val up: RelPath = RelPath.up
4050

0 commit comments

Comments
 (0)