Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Patch Environment On OSX And Allow A Different Environment Per Window - #11054

Merged
lee-dohm merged 16 commits into
masterfrom
ld-osx-environment
Mar 12, 2016
Merged

lee-dohm merged 16 commits into
masterfrom
ld-osx-environment

Conversation

@lee-dohm

@lee-dohm lee-dohm commented Mar 4, 2016

Copy link
Copy Markdown
Contributor
  • Create an atom.project.getEnv API
  • Fixup PATH on OS X when TERM is not set
  • Patch process.env on startup
  • Receive passed in environment variables on subsequent launches

Fixes #11037
Fixes #6956

@thomasjo

thomasjo commented Mar 4, 2016

Copy link
Copy Markdown
Contributor

giphy

Comment thread src/project.coffee Outdated
env.PATH

getShellEnv: ->
shell = process.env.SHELL ? "/bin/bash"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried it and it doesn't give accurate terminal results for $PATH me on my mac

# From terminal
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/steel/n/bin:/usr/games/bin
# From executing this
/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We ran into the same problem when working with linters, we ended up creating consistent-path and consistent-env. Both are very battle tested and used by almost all of the linter providers.

consistent-path is a consistent-env dependent package which provides just the PATH, but looks like you could use consistent-env here

Note: Both of these are npm packages and not apm packages

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lee-dohm If you replace the --login flag with --interactive this should work as intended ✨

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steelbrain the shell launching should not be done on any platform other than OS X. Consequently, nothing should break on Linux.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joefitzgerald There are several pairs of Desktop Environments and shells on Linux that do not supply ENV correctly to apps not run from terminal, most notably GNOME Shell and ZSH. Therefore we do need to fix it there as well

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we don't need to in this PR; this PR is intended to solve for the OS X issue, and add a project level API for the environment.

That doesn't preclude more cases beyond OS X; can you please open up (or link) an issue for each additional scenario, describing steps to reproduce, and desired behavior?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@steelbrain Correct me if I'm wrong, but that has nothing to do with the DE nor the shell. What you're describing would be distribution-specific configuration problems. There are lots of silly *nix distributions out there with horrible defaults.

But if this truly is a problem (in particular the HUP scenario described earlier), then we'll just have to deal with it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well we don't need to in this PR; this PR is intended to solve for the OS X issue, and add a project level API for the environment.

Agreed! We can patch other problems at a later point, once we can reliably reproduce the anecdotal reports.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joefitzgerald @thomasjo What do you guys have against the npm modules I suggested? It takes care of all the scenarios and fixes this for everyone not just the handful few

@lee-dohm

lee-dohm commented Mar 8, 2016

Copy link
Copy Markdown
Contributor Author

This is still not complete but I wanted to push up what I have so far. I decided to include patching process.env on startup per @joshaber's suggestion on #11037 (though the approach I chose is probably wrong). I also did a bit more reading on #4126 (and there's still #6956 that I haven't caught up on in a while). There are a lot of nuances that will need to be covered for this to be acceptable. I'm going to keep plugging away at it 😀

Comment thread src/atom-environment.coffee Outdated

# Patch the `process.env` on startup to fix the problem first documented
# in #4126
process.env = @project.getEnv()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to make it less clean, but this should happen prior to packages being loaded. I.e. prior to @project being defined. Think of all the activities that packages may do on activation that they should not be doing during activation (but still are doing) that are dependent on process.env.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move the patching logic such that it is executed in the first atom-environment, then maybe @project.getEnv() could be renamed to be @project.env. Perhaps we could also externalize the logic into a file that is only required if patching is needed (javascript FTW?).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the ideas. I'll take a deeper look at this later tonight or tomorrow.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

then maybe @project.getEnv() could be renamed to be @project.env

That would lose us the current feature that packages can't muck up the environment for other packages (at least through the public API). Right now, everyone gets a copy and then they can do what they want with their copy.

I'll definitely move the first initialization further up though.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, fair enough on making it a function:

  • project.getEnv()?
  • project.env()?
  • project.environment()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking deeper, it appears that packages aren't loaded until AtomEnvironment.startEditorWindow:

https://github.com/atom/atom/blob/ld-osx-environment/src/atom-environment.coffee#L674

So doing the patching of the environment in the AtomEnvironment constructor does make it happen before packages are loaded.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I was sticking with just "env" as the main part of the name to make it familiar to Node developers and process.env. I added "get" to the front to make it clear it is a function rather than just an attribute (and all of the other public APIs have a verb in the front). I can totally be swayed though.

@nathansobo

Copy link
Copy Markdown
Contributor

I don't think we should make a new API for fetching environment variables. I think we should extend the existing API so it works correctly despite our multi-process setup, which shouldn't be that relevant to Atom users. The only justifications I could see for inventing something new would be the following:

  1. Someone might write into process.env. 🤷 People can always write places they shouldn't. Doesn't justify building a new construct in my mind.
  2. People don't expect process.env to have these particular variables. I think we can reinterpret a bit since we own the platform.

@joefitzgerald

Copy link
Copy Markdown
Contributor

Yep @nathansobo i've already made the requisite changes :) Pushing the commits shortly.

@joefitzgerald joefitzgerald changed the title atom.project.getEnv API Patch Environment On OSX And Allow A Different Environment Per Window Mar 10, 2016
Comment thread spec/environment-spec.js Outdated
delete process._originalEnv
options = {
platform: process.platform,
env: _.clone(process.env)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.assign?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try it out. I haven't used it before. If it works the same, I'll update it.

@lierdakil

Copy link
Copy Markdown
Contributor

Should -i be used when spawning the shell?

I found that many Linux/OSX users tend to customize their environment in rc file (.bashrc/.zshrc), which only works for interactive shells. This is certainly not a preferred way, but it's used by many people nonetheless. Not all of them understand differences between login and interactive shell, and not all of them care.

It's true that running interactive shell can lead to problems. But running login shell can too. For example, I run ssh-agent from .profile -- not something you want to do when starting Atom (I'm on Linux though, so make of it what you will)

@ioquatix

ioquatix commented Apr 9, 2016

Copy link
Copy Markdown

Hey, since I tried to solve this problem in script-runner I thought I'd chime in here.

Firstly, for people running things like RVM, or the equivalent for python, you definitely want to get the environment from an interactive login shell, or you won't get the right things.

We tried tied to extract this into a separate module, called shell-environment, ideally so that other "script runner" packages could use it.. or in general, anyone who wanted to get a useful "environment" for running user commands.

We also had some issues on Linux where things like DISPLAY were not getting set and this might be unintuitive for users who are expecting things to "just work".

If you look at the approach we took in shell-environment, you'll see we avoided many of the issues of using an interactive shell by piping the results of env back through a custom pipe.

@gnestor

gnestor commented Apr 20, 2016 •

Copy link
Copy Markdown

I have discovered another minutia missing from this patch. Unless Atom is launched from the terminal, process.env.PWD is set to '/'. It's a hack but this can be easily patched by running process.env.PWD = atom.workspace.project.getPaths()[0]. This should be patched because it broke functionality for a package that I am working on and will do the same for any other packages that depend on process.env.PWD. @lee-dohm What do you say?

@lee-dohm

Copy link
Copy Markdown
Contributor Author

@gnestor I think PWD is set correctly. It is set by the environment that Atom is launched within. The goal of this fix isn't to make the environment identical to what it would be if it were launched from the terminal at some pre-determined location. It is to import the user's standard profile. This change achieved that.

@gnestor

gnestor commented Apr 20, 2016

Copy link
Copy Markdown

@lee-dohm Ok so it sounds like this is a separate issue. Do you agree though that Atom should correctly set process.env.PWD to the project's root (e.g. atom.workspace.project.getPaths()[0]) vs. "/" when Atom is launched outside of the terminal? If so, I will open a new issue.

@lee-dohm

Copy link
Copy Markdown
Contributor Author

@gnestor No, I don't agree that PWD should be set to the project's root. The concept of this change wasn't to make it as if someone launched from the project directory but to import the user's standard environment (as much as possible).

@driskell

driskell commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

In most of 1.12 betas (I'm on 1.12.0-beta7 at the moment on Sierra 10.12) there's a problem with new windows.

If you open from OS X dock and inspect process.env, all is fine and wonderful in the world/window. When you go to File -> New Window, and inspect process.env on the new window, it does not include any of the environment variables, anything from login is gone. I haven't had change to inspect things but seems might be an issue with inheriting environment when creating new windows. I can't find a related issue to this either.

@thomasjo

thomasjo commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

I can reproduce the behavior described by @driskell. Unsure whether this is a regression or not — was this specific workflow tested/fixed? /cc @lee-dohm @atom/feedback

@driskell

driskell commented Nov 7, 2016

Copy link
Copy Markdown
Contributor

It looks like the new-window events etc don't pass in the current env to the openPaths (

@on 'application:new-window', -> @openPath(getLoadSettings())
) so it doesn't do any environment update, unlike with the first window opening (or window opened from command line), which does. It also means dragging a folder onto dock to open a project means you get no env.

I'll raise a new issue for it this week, maybe even attempt a fix.

@lee-dohm

Copy link
Copy Markdown
Contributor Author

@driskell Have you filed an Issue on this behavior?

@flujan

flujan commented Nov 16, 2016

Copy link
Copy Markdown

Same issue here.

@joefitzgerald

Copy link
Copy Markdown
Contributor

@flujan have you updated to the latest version of atom? A fix for the issue described by @driskell was included in the latest patch.

@flujan

flujan commented Nov 16, 2016

Copy link
Copy Markdown

Yes, when I start ATOM from the terminal, it works. When I try to launch from the Dock it does not have the Path configured.

@joefitzgerald

joefitzgerald commented Nov 16, 2016 •

Copy link
Copy Markdown
Contributor

@flujan #13200 your package(s) may need to be updated to make use of the core:loaded-shell-environment activationHook. Which package(s) are complaining about the path?

@flujan

flujan commented Nov 16, 2016

Copy link
Copy Markdown

Guys, the Iterm2 plugin was causing the bug. Thanks.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.