Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure virtualenv activates #10618

Merged
merged 1 commit into from Jul 18, 2016
Merged

Ensure virtualenv activates #10618

merged 1 commit into from Jul 18, 2016

Conversation

@zwn
Copy link
Contributor

zwn commented Apr 14, 2016

Fixes #10595. For more info see #10595 or the commit message.


This change is Reviewable

@highfive
Copy link

highfive commented Apr 14, 2016

Thanks for the pull request, and welcome! The Servo team is excited to review your changes, and you should hear from @Ms2ger (or someone else) soon.

@highfive
Copy link

highfive commented Apr 14, 2016

Heads up! This PR modifies the following files:

@highfive
Copy link

highfive commented Apr 14, 2016

warning Warning warning

  • These commits modify script code, but no tests are modified. Please consider adding a test!
@Manishearth
Copy link
Member

Manishearth commented Apr 14, 2016

@larsbergstrom
Copy link
Contributor

larsbergstrom commented Apr 14, 2016

Reviewed 2 of 3 files at r1.
Review status: 2 of 3 files reviewed at latest revision, 1 unresolved discussion.


README.md, line 99 [r1] (raw file):
If you are going to remove the steps that rename the existing binaries, please provide explicit instructions on how to set up the user's PATH so that it will always be correct - either in the form of a shell profile file modification that appends the correct Python directory to the front of the path or explicit shortcut modifications.

I'm also really, really surprised that you were able to get Servo to build at all from a normal MSYS shell window. The various python and shell scripts do not all detect environment variables correctly in that configuration. I will need to test this change myself on a clean machine to ensure that it really does work correctly, as there were a huge number of virtualenv-related problems with using an MSYS environment in the past, even with the correct MINGW64 python.

cc @vvuk


Comments from Reviewable

@larsbergstrom
Copy link
Contributor

larsbergstrom commented Apr 14, 2016

Reviewed 1 of 3 files at r1.
Review status: all files reviewed at latest revision, 1 unresolved discussion.


Comments from Reviewable

@zwn
Copy link
Contributor Author

zwn commented Apr 14, 2016

The native windows python can be installed in a user selected directory. I am using simple modification of the PATH variable using my python install directory:

$ echo $PATH
/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl
$ PATH=/c/software/Python27/:/c/software/Python27/Scripts/:$PATH
$ which python
/c/software/Python27/python
$ which virtualenv
/c/software/Python27/Scripts/virtualenv

It works for mach and mozjs as well. I am not sure how to write a script to detect the python installation directory and I also do not know what "explicit shortcut modifications" is. 😞

I have found no reason to rename the /mingw64/bin/python. If anything /usr/bin/python is in the PATH before. But I agree, it is a total mess.

One more thing - I call mach as python mach to have a bit of control.

I'd be really glad if someone else have tried it as it is all funky.

@zwn
Copy link
Contributor Author

zwn commented Apr 15, 2016

I can take the commit 7621d65 out of this PR and make separate one just for the python version issue. Would it make it easier to review?

@larsbergstrom
Copy link
Contributor

larsbergstrom commented Apr 15, 2016

Yes, definitely!

Sorry for the delay - I'm trying to get a Windows buildbot instance online, and that's taking up a lot of my testing time right now.

@@ -111,7 +110,7 @@ def _activate_virtualenv(topdir):
except (subprocess.CalledProcessError, OSError):
sys.exit("Python virtualenv failed to execute properly.")

execfile(activate_path, dict(__file__=quote(activate_path)))
execfile(activate_path, dict(__file__=activate_path))

This comment has been minimized.

@frewsxcv

frewsxcv Apr 16, 2016

Member

Someone should check to see if this breaks Linux/OSX paths with spaces

This comment has been minimized.

@jdm

jdm Apr 16, 2016

Member

When I get rid of the explicit check for paths with spaces that prevents building, removing the quote does in fact give me:

Running virtualenv with interpreter /usr/local/bin/python2.7
New python executable in /Users/jdm/servo test spaces/servo/python/_virtualenv/bin/python2.7
Also creating executable in /Users/jdm/servo test spaces/servo/python/_virtualenv/bin/python
Installing setuptools, pip...done.
Pip failed to execute properly.

on OS X.

This comment has been minimized.

@frewsxcv

frewsxcv Apr 16, 2016

Member

@zwn What happens if you keep the quoting here? How is it "counterproductive"?

This comment has been minimized.

@zwn

zwn Apr 17, 2016

Author Contributor

I tried to explain the problem in #10595. The __file__ is used for os.paht.abspath. It expects unquoted path. I do not see how it could have ever worked. Let me know if I should add anything. I tested it on linux before the commit like this:

$ mkdir "test spaces"
$ virtualenv test\ spaces/_virtualenv
New python executable in test spaces/_virtualenv/bin/python
Installing setuptools, pip...done.
$ virtualenv --version
1.11.6
$ python --version
Python 2.7.9

This comment has been minimized.

@zwn

zwn Apr 17, 2016

Author Contributor

The problem with spaces seems to be more deeply ingrained. See http://lists.gnu.org/archive/html/bug-bash/2008-05/msg00053.html. The pip script contains shebang with full path to the python interpreter. This is interpreted directly by the kernel and apparently there is no way to escape the whitespace.

Python itself does not quote the path it sets to the __file__ variable so it should be ok not to quote it in execfile.

When putting the quote call there, the environment fails to activate properly. That means the following call to pip will not execute the pip from the virtualenv but the global one (for me that was /usr/bin/pip2). That is how you can end up with the requirements installed globally (for more discussion see #8968).

This comment has been minimized.

@zwn

zwn Apr 17, 2016

Author Contributor

To support building in a directory with a spaces we would need to stop relying on shebang scripts.

This comment has been minimized.

@larsbergstrom

larsbergstrom Apr 23, 2016

Contributor

@zwn I'm not super exited to take a fix that trades working with spaces in paths on OSX+Linux for working with spaces in paths on Windows. Can you do something per-platform in your changes?

In general, I would have preferred that we not support paths with spaces in them for the build environment on any platform (which is why I requested the addition of an error message to mach). We can get them somewhat working for Servo's Rust code proper, but there are a lot of pieces of code - especially native libraries we rely on from others - where building with spaces in the path is explicitly a non-goal and may never work out for us.

This comment has been minimized.

@zwn

zwn Apr 23, 2016

Author Contributor

@larsbergstrom If that is the only concern, that is great! 😃 Because by merging this change we are not trading anything. The original code had one of two possible effects:

  • did nothing when no quoting was required
  • caused virtualenv not to activate when quoting was deemed necessary (on any platform)

Especially it did not help with the "space in the path" problem in any way. I've went into great lengths to better explain it in the issue #10595 and in the commit message.

This comment has been minimized.

@frewsxcv

frewsxcv Apr 23, 2016

Member

Because by merging this change we are not trading anything.

Yes, we are. Previously:

  • Works on OSX
  • Doesn't work on Windows

With your changes:

  • Doesn't work on OSX
  • Works on Windows

As jdm demonstrated earlier, your changes break OSX. And to put the nail in the coffin, I tried this out myself:

diff --git a/python/mach_bootstrap.py b/python/mach_bootstrap.py
index e77c78e..d143a1b 100644
--- a/python/mach_bootstrap.py
+++ b/python/mach_bootstrap.py
@@ -112,7 +112,7 @@ def _activate_virtualenv(topdir):
         except (subprocess.CalledProcessError, OSError):
             sys.exit("Python virtualenv failed to execute properly.")

-    execfile(activate_path, dict(__file__=quote(activate_path)))
+    execfile(activate_path, dict(__file__=activate_path))

     # TODO: Right now, we iteratively install all the requirements by invoking
     # `pip install` each time. If it were the case that there were conflicting
@@ -161,10 +161,10 @@ def bootstrap(topdir):

     # We don't support paths with spaces for now
     # https://github.com/servo/servo/issues/9442
-    if ' ' in topdir:
-        print('Cannot run mach in a path with spaces.')
-        print('Current path:', topdir)
-        sys.exit(1)
+    #if ' ' in topdir:
+    #    print('Cannot run mach in a path with spaces.')
+    #    print('Current path:', topdir)
+    #    sys.exit(1)

     # Ensure we are running Python 2.7+. We put this check here so we generate a
     # user-friendly error message rather than a cryptic stack trace on module
coreyf@frewbook-pro ~/D/r/f/servo (master)> ./mach build -d
Pip failed to execute properly.

@zwn Do you have this library installed? https://virtualenv.pypa.io/en/latest/userguide.html#windows-notes

This comment has been minimized.

@zwn

zwn Apr 23, 2016

Author Contributor

What you are seeing is the correct behavior. It was already broken however the problem was hidden. It appeared that it works because it called the globally installed pip and not the one from the virtualenv.

With the change I am proposing the virtualenv successfully activates but the pip script fails to execute due to the way exec call is defined. To quote myself

The problem with spaces seems to be more deeply ingrained. See http://lists.gnu.org/archive/html/bug-bash/2008-05/msg00053.html. The pip script contains shebang with full path to the python interpreter. This is interpreted directly by the kernel and apparently there is no way to escape the whitespace.

You can test it on a linux box too. This is what happens when you create virtualenv in a directory with a space in the name:

$ virtualenv 'env spaces'
Running virtualenv with interpreter /usr/bin/python2
New python executable in env spaces/bin/python2
Also creating executable in env spaces/bin/python
Installing setuptools, pip...done.
$ . ./env\ spaces/bin/activate
$ echo $PATH
/tmp/env spaces/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
$ which pip
/tmp/env spaces/bin/pip
$ pip
bash: /tmp/env spaces/bin/pip: "/tmp/env: bad interpreter: No such file or directory
$ cat env\ spaces/bin/pip
#!"/tmp/env spaces/bin/python2"

# -*- coding: utf-8 -*-
import re
import sys

from pip import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())

Activating it differently just breaks the PATH so the wrong pip (=global) is found. I am not sure how to explain it differently 😞 It is not windows problem, it is quoting problem.

bors-servo added a commit that referenced this pull request Apr 17, 2016
Use os.path.basename instead of split('/')[-1]

Fixes #10596. I have split up this commit from  #10618 as it seem the easiest to review.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10664)
<!-- Reviewable:end -->
@zwn
Copy link
Contributor Author

zwn commented Apr 17, 2016

I have started to split the commits to separate PRs. I am going to keep only 8fbd416 in this PR and force push. I'll do separate PR for @larsbergstrom and the README stuff. If this does not seem ok, please, feel free to stop me 😃

@zwn
Copy link
Contributor Author

zwn commented Apr 20, 2016

What else is needed to merge this? I have updated the issue #10595 to better reflect my understanding of the problem (I can copy it to the commit message if that is the custom here).

There are two changes:
 * remove quoting which causes virtuaenv not activate
 * check virtualenv actually activated

If the quoting added in the fix in #8394 (4ff8d3a) kicks in, it
causes virtualenv to fail to activate.

For the common case it is no op:
```python
>>> from pipes import quote
>>> print quote('common/case')
common/case
```

When the path actually needs quoting, this is what happens:

```python
>>> print quote('test spaces')
'test spaces'
>>> print quote('windows\\path')
'windows\\path'
```

Note the embedded quotes.

Virtualenv in activate_this.py uses __file__ to build the path that
should be added to PATH:

```python
>>> print os.getcwd()
C:\software\git
>>> print os.path.abspath(quote('windows\\path'))
C:\software\git\'windows\path'
>>>
```

The constructed path is not valid. Adding it at the beginning of PATH
has no effect. This issue affects any case when the call to `quote`
kicks in.
@zwn zwn changed the title Fix windows build Ensure virtualenv activates Apr 21, 2016

python = find_executable("python")
if python is None or not python.startswith(virtualenv_path):
sys.exit("Python virtualenv failed to activate.")

This comment has been minimized.

@zwn

zwn Apr 23, 2016

Author Contributor

@frewsxcv I appreciate you taking the time to test this. Could you please include these 3 lines in your test as well? It might shine some light on the problem.

@larsbergstrom
Copy link
Contributor

larsbergstrom commented May 3, 2016

@bors-servo try

  • Trying this, now that we have a windows buildbot
@bors-servo
Copy link
Contributor

bors-servo commented May 3, 2016

Trying commit 7002fbe with merge 306dd55...

bors-servo added a commit that referenced this pull request May 3, 2016
Ensure virtualenv activates

Fixes #10595. For more info see #10595 or the commit message.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10618)
<!-- Reviewable:end -->
@frewsxcv
Copy link
Member

frewsxcv commented May 3, 2016

I don't think 'try'ing will do much since the issue at hand is related to spaces in path names, which I'm assuming isn't a factor on the builders? (at least on Linux)

@larsbergstrom
Copy link
Contributor

larsbergstrom commented May 3, 2016

@frewsxcv Based on the conversation thread above, it was not clear to me that this worked even on non-space-containing pathnames on all platforms.

We should perhaps have another conversation about this problem in general. I'm not really excited about ensuring our build environment supports spaces in the path name, as it feels like it will either be an endless bug farm or we'll have to spawn up extra per-platform build tests with spaces in the path to ensure it keeps working.

@bors-servo
Copy link
Contributor

bors-servo commented May 3, 2016

@jdm
Copy link
Member

jdm commented May 26, 2016

We need to make a decision here.

@metajack
Copy link
Contributor

metajack commented May 26, 2016

It sounds like Mac was working by accident, and spaces-based builds are disabled anyway. If this makes them work on windows and doesn't break non-spaces builds on OS X, we can toggle the check for spaces to be a check for spaces and sys.platform not in ('msys', 'win32').

I share Lars' concern that will will just uncover more weird bugs, but it seems harmless to try this and if it doesn't work we can go back to immediate failure in paths with spaces.

Did I miss anything? Are there any objections to landing this as I have described?

@larsbergstrom
Copy link
Contributor

larsbergstrom commented May 27, 2016

@bors-servo
Copy link
Contributor

bors-servo commented May 27, 2016

📌 Commit 7002fbe has been approved by larsbergstrom

@frewsxcv
Copy link
Member

frewsxcv commented Jul 17, 2016

Did @bors-servo decide not to run this? Is this still good to go?

@KiChjang
Copy link
Member

KiChjang commented Jul 17, 2016

@bors-servo try- clean r=larsbergstrom

@bors-servo
Copy link
Contributor

bors-servo commented Jul 17, 2016

💡 This pull request was already approved, no need to approve it again.

@bors-servo
Copy link
Contributor

bors-servo commented Jul 17, 2016

📌 Commit 7002fbe has been approved by larsbergstrom

@KiChjang
Copy link
Member

KiChjang commented Jul 17, 2016

@bors-servo r- try- clean force

@KiChjang
Copy link
Member

KiChjang commented Jul 17, 2016

@bors-servo r=larsbergstrom

@bors-servo
Copy link
Contributor

bors-servo commented Jul 17, 2016

📌 Commit 7002fbe has been approved by larsbergstrom

@KiChjang
Copy link
Member

KiChjang commented Jul 17, 2016

@zwn It looks like we'll need you to push a different commit hash onto your branch. Switch to your branch and do git commit --amend -C HEAD and then git push -f to push a new commit hash. This should reset the CI status on github.

@cbrewster
Copy link
Member

cbrewster commented Jul 17, 2016

@bors-servo retry

@bors-servo
Copy link
Contributor

bors-servo commented Jul 17, 2016

Testing commit 7002fbe with merge e7a55ae...

bors-servo added a commit that referenced this pull request Jul 17, 2016
Ensure virtualenv activates

Fixes #10595. For more info see #10595 or the commit message.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10618)
<!-- Reviewable:end -->
@bors-servo
Copy link
Contributor

bors-servo commented Jul 18, 2016

@bors-servo bors-servo merged commit 7002fbe into servo:master Jul 18, 2016
2 of 3 checks passed
2 of 3 checks passed
continuous-integration/appveyor/pr AppVeyor build failed
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

10 participants
You can’t perform that action at this time.