Skip to content

What is the rationale behind quietly ignoring Environment.SetEnvironmentVariable calls with targets User or Machine on Unix-like platforms? #27566

Closed

Description

@mklement0

Persistently updating environment variables is (at least currently) only supported on Windows, which means passing the User and Machine enumeration values to Environment.SetEnvironmentVariable are unsupported on macOS and Linux.

Surprisingly, however, doing the latter doesn't cause an exception, but is quietly ignored (an effective no-op).

Even though this behavior is documented, I wonder:

  • what the rationale for this behavior is, given that as an API user you generally want to be notified of attempts to use functionality that is unsupported on a given platform.

  • whether the behavior can be changed.

Activity

  1. changed the title [-]What is the rationale behind quietly ignoring Environment.SetEnvironmentVariable with targets User and Machine on Unix-like platforms?[/-] [+]What is the rationale behind quietly ignoring Environment.SetEnvironmentVariable calls with targets User or Machine on Unix-like platforms?[/+] on Oct 8, 2018
  2. mklement0 commented on Oct 8, 2018

    @mklement0
    Author

    Thanks, @jkotas.

    Reading the discussion and the linked issues I gather that the plan for addressing this is the following, correct?:

  3. jkotas commented on Oct 8, 2018

    @jkotas
    Member

    Right, that is the current plan.

  4. mklement0 commented on Oct 8, 2018

    @mklement0
    Author

    I see, thanks.

    Is the reason for not throwing a PNSE at runtime compatibility with Mono?

    Note that not all CoreFx consumers are compiled languages, so calls from PowerShell code, for instance, would not see the compile-time warning. A cmdlet that wraps this API, such as proposed in PowerShell/PowerShell#5340 (comment), is then faced with the following - suboptimal - choices:

    • Accept the quiet no-op runtime behavior on unsupported platforms.

    • Duplicate the compile-time warnings by turning them into PowerShell errors / warnings.

  5. jkotas commented on Oct 8, 2018

    @jkotas
    Member

    Is the reason for not throwing a PNSE at runtime compatibility with Mono?

    It is one of the reasons. Even without Mono in the equation, no-op seemed like the right choice for this one because of it makes more code "just work" (https://github.com/dotnet/corefx/issues/20147#issuecomment-303501844). There are other methods like this in the framework that we choose to turn into no-op on non-Windows platforms. The choices for these Windows-specific methods are always sub-optimal. Ideally, they would be in Windows-specific assembly.

    Changing the behavior to throw PNSE would be a breaking change.

  6. mklement0 commented on Oct 8, 2018

    @mklement0
    Author

    Thanks, @jkotas.

    Understood re breaking change, so I'm closing this.

    Sounds like moving such overloads / methods into Windows-specific assemblies is the way to go in the future.

    In cases where this is impractical, please reconsider the current "just works" approach, because its proper name should be "doesn't work, but pretends that it does", which I think is more harmful than throwing an exception.
    (A compiler warning, which is helpful irrespective of the runtime behavior, can mitigate this, but those warnings aren't always seen, e.g., in PowerShell).

  7. danmoseley commented on Oct 9, 2018

    @danmoseley
    Contributor

    Arguably it could at least update the process environment, since in some cases this would happen on Windows (depends on how many apps respect the WM_SETTINGCHANGE message)

  8. mklement0 commented on Oct 9, 2018

    @mklement0
    Author

    @danmosemsft:

    But the method call itself currently doesn't update the process-level variable directly, so wouldn't what you're proposing be a breaking change?

    On a side note: It has always struck me as inconvenient that you need a second call just to make the value effective in the current process as well.

    Perhaps it's worth consider extending the enumeration with UserAndProcess and MachineAndProcess values that set both the persisted and the current-process value.

  9. transferred this issue fromdotnet/corefxon Jan 31, 2020
  10. added this to the 3.0 milestone on Jan 31, 2020
  11. ghost locked as resolved and limited conversation to collaborators on Dec 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Runtimequestion

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions