GHC 2017-07-28

8 comments.

, https://git.io/v7Wf2 in Homebrew/homebrew-core
caddy 0.10.6
============

Created with `brew bump-formula-pr`.

---

<img src="https://cloud.githubusercontent.com/assets/1128849/25305033/12916fce-2731-11e7-86ec-580d4d31cb16.png" width="75" height="20">

, https://git.io/v7cAB in Homebrew/brew
Yep that's the point.

, https://git.io/v7cAR in Homebrew/brew
@ilovezfs Cool. As I said I'm confused about Dom's reverse issue since `:python3` explicitly looks for `python3`. The intermediate variables should most likely reveal what's going on.

, https://git.io/v7cAE in Homebrew/brew
> And presumably you do not have a PYTHONPATH set manually, yes?

No, I don't.

I'd imagine a reproducer could simply be:

```sh
brew install python python3
mkdir -p /tmp/debug-brew-python
ln -s /usr/local/bin/python3 /tmp/debug-brew-python/python
PATH=/tmp/debug-brew-python:$PATH
unset PYTHONPATH  # probably shouldn't matter; didn't check
brew install -s ...  # some formula that depends_on => :python and has python resources, e.g., ipython@5
```

Let me know if that doesn't cut it.

, https://git.io/v7cAu in Homebrew/brew
For me, I have `~/.pyenv/versions/3.6.1/bin` in my `PATH`, which provides the first `python`. I also have both brewed `python` and `python3`. (Update: I don't typically have `PYTHONPATH` set when I run brew.) The diagnostics in my original post should explain this scenario fairly clearly; the question is how to fix it.

I'm confused about Dom's scenario though, and printing out some intermediate variables should shed some light.

, https://git.io/v7cQU in Homebrew/brew
@DomT4 Good old printf debugging may help:

```diff
diff --git a/Library/Homebrew/requirement.rb b/Library/Homebrew/requirement.rb
index cfb925b49a..bc76d2e194 100644
--- a/Library/Homebrew/requirement.rb
+++ b/Library/Homebrew/requirement.rb
@@ -151,6 +151,7 @@ class Requirement
   end
 
   def which(cmd)
+    puts "PATH for which: #{ORIGINAL_PATHS}"
     super(cmd, PATH.new(ORIGINAL_PATHS))
   end
 
diff --git a/Library/Homebrew/requirements/python_requirement.rb b/Library/Homebrew/requirements/python_requirement.rb
index c162b072c9..3cc8f367ab 100644
--- a/Library/Homebrew/requirements/python_requirement.rb
+++ b/Library/Homebrew/requirements/python_requirement.rb
@@ -15,6 +15,7 @@ class PythonRequirement < Requirement
 
   env do
     short_version = python_short_version
+    puts "python_short_version: #{python_short_version}"
 
     if !system_python? && short_version == Version.create("2.7")
       ENV.prepend_path "PATH", which_python.dirname
@@ -32,7 +33,9 @@ class PythonRequirement < Requirement
   end
 
   def which_python
+    puts "python_binary: #{python_binary}"
     python = which python_binary
+    puts "python: #{python}"
     return unless python
     Pathname.new Utils.popen_read(python, "-c", "import sys; print(sys.executable)").strip
   end
```

, https://git.io/v7cvk in Homebrew/brew
Oh I just realized I was running into a py3x site-packages dir for py2x problem, while Dom ran into the opposite py2x site-packages dir for py3x problem. That's kinda funny, I'm slightly confused as to how he managed to get a 2.7 short version when `python_binary` is explicitly `"python3"`. Anyway, I don't have the time to investigate now.

, https://git.io/v7cvI in Homebrew/brew
PythonRequirement woes: Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x
===================================================================================================================

See https://github.com/Homebrew/brew/pull/2917#issuecomment-317272358. I ran into this today myself, even with `HOMEBREW_ENV_FILTERING` turned on:

```
==> python -c import setuptools... --no-user-cfg install --prefix=/usr/local/Cellar/gnuradio/3.7.11_1/libexec/vendor --single-version-externally-managed --record=installed.txt
Your PYTHONPATH points to a site-packages dir for Python 3.x but you are running Python 2.x!
     PYTHONPATH is currently: "/usr/local/Cellar/gnuradio/3.7.11_1/libexec/vendor/lib/python2.7/site-packages:/usr/local/lib/python3.6/site-packages"
     You should `unset PYTHONPATH` to fix this.
```

## Reproducers

- site-packages dir for Python 3.x but you are running Python 2.x: https://github.com/Homebrew/brew/issues/2958#issuecomment-318661866
- site-packages dir for Python 2.x but you are running Python 3.x: https://github.com/Homebrew/brew/issues/2958#issuecomment-318917264

## Diagnostics

I looked into `python_requirement.rb` and apparently it's picking up a wrong `python_short_version` **when you have python3 as the default python on your PATH**, which in turn inserts the wrong site-packages path into `PYTHONPATH`:

```rb
ENV["PYTHONPATH"] = "#{HOMEBREW_PREFIX}/lib/python#{short_version}/site-packages"
```

Note that `python_short_version` is decided from `which_python`, which in turn is basically `which python_binary`, where `python_binary` is `"python"`; and `which` is defined in `requirement.rb`:

```rb
  def which(cmd)
    super(cmd, PATH.new(ORIGINAL_PATHS))
  end
```
You immediately see what's wrong here: it's looking inside `ORIGINAL_PATHS`, and no wonder `HOMEBREW_ENV_FILTERING` doesn't help a bit.

## Resolution

I'm on a tight deadline for something else so I stopped my investigation there — I didn't do a bisect, and nor did I really think the problem through. However, it seems that since we're doing

```rb
# Homebrew Python should take precedence over other Pythons in the PATH
ENV.prepend_path "PATH", Formula["python"].opt_bin
ENV.prepend_path "PATH", Formula["python"].opt_libexec/"bin"
```

we should really be putting those paths in front of `ORIGINAL_PATHS` when we do the `which`. This might be the source of inconsistency here.

In the meantime, my quick and dirty workaround is setting `python_binary` to `"python2.7"`.

---

CC @DomT4 @ilovezfs