Add Environment Variable directive parsing #965
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
TL;DR
UseEnvironmentVariableDirective()
toCommandLineBuilder
enabling to control process-level environment variables for the invocation pipeline from command-line directives.The new directive is:
env
.Details
As part of the discussion in #951 the need for an environment variable directive could be benefitial for invocations as well.
This PR adds a parser that intercepts the
env
directive and extracts a=
separated key-value-pair from the directive value.Added to
UseDefaults()
Since the ability to easily control process-level environment variables is quite useful, I propose that we should add the environment variable directive to the
UseDefaults()
extension method onCommandLineBuilder
.This directive is especially useful in shells (most notably on Windows) where specifying variables during process invocation is not possible without modifying the environment variables of the hosting shell.
Behaviour
The value specified in the
env
directive is expected to contain a=
separated key-value-pair.=
.The first component is considered to be the environment variable to set, the second component is the new value for the variable.
System.Envrionment.SetEnvironmentVariable(string variable, string value)
is invoked, setting the variable at process-level, overriding the existing value (if any).Invalid Directives
An
env
-directive is ignored if::
in the directive argument) is an empty or white-space only string.=
character.=
in the directive value is an empty or white-space only string.Limitations
This directive cannot modify the behaviour of the .NET Runtime that is controlled by environment variables. When the
env
-directive is parsed, the .NET Runtime Host has already booted the runtime and envaluated the environment variables at process launch.Environment variables that are controlled by the .NET Generic Host can be affected by this directive, provided that the Generic Host is started after the invocation pipeline has parsed
env
-directives, or after the pipeline has finished.Invocation Pipeline precedence
The middle to parse the
env
-directives is added immediately following theExceptionHandler
middleware to ensure that environment variables are parsed as early in the pipeline as possible.Envrionment.SetEnvironmentVariable
might throw an exception for weird combinatinations for variable names, and these exception should be handled and printed properly using the exception-handling middleware (if present in the pipeline).