Repository navigation
Normalize The Environment And Allow Atom Windows To Have Different Environments #11037
Description
Prerequisites
-
Can you reproduce the problem in safe mode? - Are you running the latest version of Atom?
-
Did you check the debugging guide? -
Did you check the FAQs on Discuss? - Are you reporting to the correct repository?
- Did you perform a cursory search to see if your bug or enhancement is already reported?
Description
There are some issues with the way the user's environment is made available within Atom:
- On OS X, if a user launches
Atom.appfromFinder,Spotlight,Dock,open, or anotherlaunchdlaunched process, the user's environment is anaemic and potentially varies from the user's intended environment - If a user launches
atomfrom the command line multiple times, any changes in the environment for attempts2...nare not reflected in Atom's environment
I am conflating these two issues because I propose they should be solved by adding a project-level API to functionality to Atom that:atom.project (e.g. atom.project.getEnv() or atom.project.env)
- Is aware of the OS X "anaemic environment" issue and is capable of patching the environment for the implicit project that exists on first launch of Atom, if Atom was indeed launched from
Finder,Dock,Spotlight,open, et a - Allows projects to have a varying environment, so that multiple invocations of
atomfrom the command line can result in some change in the environment, if changes were made
Note that this specifically does not endorse changing process.env nor does it seek to patch it. The actual environment for the running Atom process should be preserved, unpatched as it is still a valid environment for the original process. This also allows existing functionality to work as it always has, while providing a mechanism for package authors to opt-in to the new project-level API.
Open Questions
- Is
atom.projectthe right place for this? What "marker" can be used to determine - on OS X - that the environment should be patched (see https://github.com/atom-community/environment/blob/3ccde732f213452e9b897482e1a3fba1f7a4a3e2/lib/environment.js#L113-L120 for prior art)We are going to use the presence ofTERMPWDas a marker on OSX that the environment does not need to be patchedShouldprocess.env will be patched directly.atom.project.getEnv()return a promise to permit the patching, or should any patching be done synchronously (e.g.atom.project.env)?Should there be lifecycle events that are associated with changes in a project's environment (e.g.No.onWillChangeEnv,onDidChangeEnv, etc.)?- I'm certain there are a bunch of issues that this proposal would solve; I'd love help from @50Wliu and @lee-dohm to find them all
Prior Art
- https;//atom.io/packages/environment
- https://www.npmjs.com/package/consistent-env
The Anaemic OS X Environment Issue
Steps to Reproduce
- Use OS X
- Launch Atom from
Finder,Spotlight,Dock,open - Open the Atom console, and type
process.env - Observe the environment is anaemic
- Quit Atom
- Launch Atom from the terminal (
atom .) - Open the Atom console, and type
process.env - Observe the environment is as you would expect
Expected behavior: The environment is the same, regardless of how Atom is launched on OS X.
Actual behavior: The environment is different, depending on how Atom is launched.
### VersionsAll, <= 1.7.x.
The Multiple-Launch Issue
Steps to Reproduce
- Atom does not update its environment when a project is opened twice
- Use any operating system
- Launch a terminal
- Make a directory (e.g.
directory-1), and change your working directory to it - Set an environment variable called
SOMEVARtotrue(e.g.export SOMEVAR=trueon OS X / Linux;SET SOMEVAR=trueon Windows) - Launch
atomfrom the terminal (e.g.atom .) - Observe a new Window launches, with a new project in the tree view, for the
directory-1directory - Open the Atom console, and type
process.env - Observe the environment has the
SOMEVARkey set with a value oftrue - Go back to the terminal, and set the SOMEVAR environment variable to
false(e.g.export SOMEVAR=trueon OS X / Linux;SET SOMEVAR=trueon Windows) - Launch Atom from the terminal (
atom .) - Observe a new process launches temporarily, but then exits, and the original atom window focuses, with the
directory-1project open - Open the Atom console, and type
process.env - Observe the environment has the
SOMEVARkey set with a value oftrue(although one would expect it to have a value offalsenow)

- Atom does not allow different environments for different projects / windows
- (Continued from above)
- Change your working directory to the parent folder of
directory-1 - Make a directory (e.g.
directory-2) and change your working directory to it - Set an environment variable called
SOMEOTHERVARtotrue(e.g.export SOMEOTHERVAR=trueon OS X / Linux;SET SOMEOTHERVAR=trueon Windows) - Launch
atomfrom the terminal (e.g.atom .) - Observe a new Window launches, with a new project in the tree view, for the
directory-2directory - Observe that the existing Window for
directory-1is still open - Open the Atom console, and type
process.env - Observe the environment has the
SOMEVARkey still set to a value oftrue - Observe the environment has the
SOMEOTHERVARkey undefined

Expected behavior: There is some way for a package author to get access to the state of the environment as it existed when atom was last invoked to launch a Window.
Actual behavior: Any changes in the environment for atom invocation 2...n are not available to the package author.
Versions
All, <= 1.7.x.
/cc @lee-dohm


Activity
lee-dohm commentedon Mar 3, 2016
Thanks for documenting all of this @joefitzgerald!
Regarding the "marker", I think that we can determine if the
PATHis equal to the concatenation of the contents of/etc/pathsand all entries in/etc/paths.d. This way if some application changed their system environment, we should still be able to detect if they're using the "system default"PATH. For example, on my system, my "system default"PATHis/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin. The first entry in that list is new for OS X 10.11. The last one is because I installed XQuartz for something (that I'm probably not using anymore) and I'm afraid to remove it 😆joefitzgerald commentedon Mar 3, 2016
@lee-dohm just to summarize our conversation on Slack, we believe that the presence of
TERMis the most appropriate marker that atom was launched from a terminal on OS X (e.g. iTerm2, Terminal.app); it is not present when Atom is launched fromFinder,Spotlight,Dock, etc.joshaber commentedon Mar 7, 2016
I'm curious why we wouldn't patch
process.envitself. Isn't the unreliability of$PATHa general problem/bug?joefitzgerald commentedon Mar 7, 2016
Quite simply, because process.env is simply reflecting the actual state of the process's environment. We shouldn't mask that or patch that. While it is desirable to have a consistent environment (regardless of how atom was launched), that's different to mucking with the actual environment for the root atom process.
joshaber commentedon Mar 7, 2016
I expect what package authors would like is for
child_processto Just Work. For better or worse, VS Code directly patches the env: https://github.com/Microsoft/vscode/blob/80d53d89a341f8ba7caffebc39b7e4c8df73b613/src/vs/workbench/electron-main/main.ts#L251joefitzgerald commentedon Mar 7, 2016
@joshaber the thing is, I think we need a project level (or window level) api anyway, because we are also trying to solve for the scenario when you launch
atommultiple times from the terminal, and each invocation has a different environment.So I guess yes, we could also patch process.env. Would there be any valid use cases where people need access to the unpatched env?
thedaniel commentedon Mar 8, 2016
If so, we could just make the original env available somewhere else for those rare cases. I tend to agree with @joshaber that package authors that want to use child_process probably don't want to think about getting different kinds of env. I do however share @joefitzgerald's view that we shouldn't throw away the original env - it might be useful for debugging user env issues at least.. we could patch it onto process as
_originalEnvor something, or provide it in some other way.joefitzgerald commentedon Mar 11, 2016
@thedaniel @joshaber FYI we did end up doing what you requested in the PR.
[-]Add An Environment API[/-][+]Normalize The Environment And Allow Windows To Have Different Environments[/+][-]Normalize The Environment And Allow Windows To Have Different Environments[/-][+]Normalize The Environment And Allow Atom Windows To Have Different Environments[/+]joshaber commentedon Mar 11, 2016
💖
lock commentedon Apr 11, 2018
This issue has been automatically locked since there has not been any recent activity after it was closed. If you can still reproduce this issue in Safe Mode then please open a new issue and fill out the entire issue template to ensure that we have enough information to address your issue. Thanks!