Skip to content

Commit 59b5fd9

Browse files
authored
Add API to redirect os.Inherited streams globally to be consistent with std streams redirections (#283)
Fixes #282 Unlike usage of `System.in`/`System.out`/`System.err` programmatically, or the `scala.Console` equivalents, output from subprocesses with `os.Inherit` is not affected by `System.setIn`/`setOut`/`setErr`. This is often counterintuitive, because you expect redirecting these streams to redirect all output, only to find output from subprocess leaking through a side channel to the original process `in`/`out`/`err`. This PR adds the `os.Inherit.in`/`out`/`err` dynamic variables, which can be used to re-assign `os.Inherit` on a scoped threadlocal basis. This allows developers who use `System.setOut` or `Console.withOut` to also redirect subprocess output within the scope of the stdout redirect, without users working within those scopes to have to remember to redirect them manually at every callsite. Because we do not control `System.setOut` or `Console.withOut`, there isn't really a way for us to detect and do this automatically (e.g. redirects may happen even before OS-Lib has been even classloaded) and so we have to rely on it being opt-in. As an escape hatch, in case users do not want this behavior, we provide a `os.Inherit0` redirect which performs identically to the original `os.Inherit` even in the process of redirecting `os.Inherit.{in,out,err}` Covered by an added unit test
1 parent 6c3a300 commit 59b5fd9

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

Readme.adoc

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

22252225
== Changelog
22262226

2227+
[#main]
2228+
=== main
2229+
2230+
* `os.Inherit`
2231+
22272232
[#0-10-2]
22282233
=== 0.10.2
22292234

os/src/ProcessOps.scala

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,31 @@ case class proc(command: Shellable*) {
114114
mergeErrIntoOut: Boolean = false,
115115
propagateEnv: Boolean = true
116116
): SubProcess = {
117-
val builder =
118-
buildProcess(commandChunks, cwd, env, stdin, stdout, stderr, mergeErrIntoOut, propagateEnv)
119117

120118
val cmdChunks = commandChunks
121119
val commandStr = cmdChunks.mkString(" ")
120+
121+
def resolve[T](x: T, y: T) = if (x == os.Inherit) y else x
122+
val resolvedStdin = resolve(stdin, os.Inherit.in.value)
123+
val resolvedStdout = resolve(stdout, os.Inherit.out.value)
124+
val resolvedStderr = resolve(stderr, os.Inherit.err.value)
125+
126+
val builder = buildProcess(
127+
commandChunks,
128+
cwd,
129+
env,
130+
resolvedStdin,
131+
resolvedStdout,
132+
resolvedStderr,
133+
mergeErrIntoOut,
134+
propagateEnv
135+
)
136+
122137
lazy val proc: SubProcess = new SubProcess(
123138
builder.start(),
124-
stdin.processInput(proc.stdin).map(new Thread(_, commandStr + " stdin thread")),
125-
stdout.processOutput(proc.stdout).map(new Thread(_, commandStr + " stdout thread")),
126-
stderr.processOutput(proc.stderr).map(new Thread(_, commandStr + " stderr thread"))
139+
resolvedStdin.processInput(proc.stdin).map(new Thread(_, commandStr + " stdin thread")),
140+
resolvedStdout.processOutput(proc.stdout).map(new Thread(_, commandStr + " stdout thread")),
141+
resolvedStderr.processOutput(proc.stderr).map(new Thread(_, commandStr + " stderr thread"))
127142
)
128143

129144
proc.inputPumperThread.foreach(_.start())

os/src/SubProcess.scala

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,31 @@ object ProcessOutput {
440440
}
441441

442442
/**
443-
* Inherit the input/output stream from the current process
443+
* Inherit the input/output stream from the current process.
444+
*
445+
* Can be overriden on a thread local basis for the various
446+
* kinds of streams (stdin, stdout, stderr) via [[in]], [[out]], and [[err]]
444447
*/
445448
object Inherit extends ProcessInput with ProcessOutput {
446449
def redirectTo = ProcessBuilder.Redirect.INHERIT
447450
def redirectFrom = ProcessBuilder.Redirect.INHERIT
448451
def processInput(stdin: => SubProcess.InputStream) = None
449452
def processOutput(stdin: => SubProcess.OutputStream) = None
453+
454+
val in = new scala.util.DynamicVariable[ProcessInput](Inherit)
455+
val out = new scala.util.DynamicVariable[ProcessOutput](Inherit)
456+
val err = new scala.util.DynamicVariable[ProcessOutput](Inherit)
457+
}
458+
459+
/**
460+
* Inherit the input/output stream from the current process.
461+
* Identical of [[os.Inherit]], except it cannot be redirected globally
462+
*/
463+
object InheritRaw extends ProcessInput with ProcessOutput {
464+
def redirectTo = ProcessBuilder.Redirect.INHERIT
465+
def redirectFrom = ProcessBuilder.Redirect.INHERIT
466+
def processInput(stdin: => SubProcess.InputStream) = None
467+
def processOutput(stdin: => SubProcess.OutputStream) = None
450468
}
451469

452470
/**

os/test/src-jvm/OpTestsJvmOnly.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,26 @@ object OpTestsJvmOnly extends TestSuite {
7070
val d = testFolder / "readWrite"
7171
intercept[nio.NoSuchFileException](os.list(d / "nonexistent"))
7272
}
73+
74+
// Not sure why this doesn't work on native
75+
test("redirectSubprocessInheritedOutput") {
76+
if (Unix()) { // relies on bash scripts that don't run on windows
77+
val scriptFolder = os.pwd / "os" / "test" / "resources" / "test"
78+
val lines = collection.mutable.Buffer.empty[String]
79+
os.Inherit.out.withValue(os.ProcessOutput.Readlines(lines.append(_))) {
80+
// Redirected
81+
os.proc(scriptFolder / "misc" / "echo_with_wd", "HELLO\nWorld").call(
82+
cwd = os.root / "usr",
83+
stdout = os.Inherit
84+
)
85+
// Not Redirected
86+
os.proc(scriptFolder / "misc" / "echo_with_wd", "hello\nWORLD").call(
87+
cwd = os.root / "usr",
88+
stdout = os.InheritRaw
89+
)
90+
}
91+
assert(lines == Seq("HELLO", "World /usr"))
92+
}
93+
}
7394
}
7495
}

0 commit comments

Comments
 (0)