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

Make install_xbuildenv work even if user doesn't source the venv #4518

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

hoodmane
Copy link
Member

This was written assuming that the user sourced the virtual environment explicitly, but it fails if they do .venv/bin/pyodide venv .venv-pyodide. There's probably some other places with similar problems.

@henryiii does this look reasonable to you?

This was written assuming that the user sourced the virtual environment
explicitly, but it fails if they do `.venv/bin/pyodide venv .venv-pyodide`.
@hoodmane
Copy link
Member Author

Probably the added functions should be in build_env.py instead of bash_runner.py...

@henryiii
Copy link
Contributor

henryiii commented Feb 14, 2024

The standard way to detect a venv is to check for pyvenv.cfg at the base of the environment. If it exists, it's a venv (and the contents tell you various details about the venv). Is that helpful here?

@hoodmane
Copy link
Member Author

The standard way to detect a venv is to check for pyvenv.cfg at the base of the environment.

Well the situation is that I want someone to be able to say .venv/bin/some-pyodide-cmd and have the implementation of some-pyodide-cmd invoke .venv/bin/pip. If the user ran source .venv/bin/activate then that placed .venv/bin/ at the beginning of their path and so pip will resolve to .venv/bin/pip. Otherwise we need to add it.

I thought sys.prefix != sys.base_prefix was a good enough way to test for a virtual environment. But you're saying I should do something more like (Path(sys.prefix) / "pyvenv.cfg").exists()?

Copy link
Member

@ryanking13 ryanking13 left a comment

Choose a reason for hiding this comment

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

Generally looks good to me.

@@ -363,3 +372,27 @@ def check_emscripten_version() -> None:
raise RuntimeError(
f"Incorrect Emscripten version {installed_version}. Need Emscripten version {needed_version}"
)


def calculate_venv_environment(env: _ENV | None = None) -> dict[str, str]:
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to have some tests for this.

@henryiii
Copy link
Contributor

henryiii commented Feb 16, 2024

Here's a recent example: https://github.com/astral-sh/uv/pull/1504/files. (Edit, that looks problematic, I like the way you did it better)

@henryiii
Copy link
Contributor

Do you need to compute bin/pip? Couldn't you use {sys.executable} -m pip?

@hoodmane
Copy link
Member Author

hoodmane commented Feb 16, 2024

I guess we could also do

{sys.executable} -m pyodide_cli ...

instead of pyodide ... and get rid of this environment calculation entirely.

@hoodmane
Copy link
Member Author

@henryiii I think I found the most to-the-point version of this that I can come up with:

def _calculate_venv_path() -> str:
    """Make sure that the current executable is the first one on the path."""
    path = os.environ["PATH"]
    if shutil.which(Path(sys.executable).name) == sys.executable:
        return path
    # We aren't the first entry on the path. Add current executable directory to
    # the front of path to correct this.
    bin_dir = str(Path(sys.executable).parent)
    return f"{bin_dir}:{path}"

Though perhaps an even simpler option would be to unconditionally prepend bin_dir to the path, since it's not like doubling it up harms anyone much.

@hoodmane
Copy link
Member Author

Okay, just using sys.executable -m some_module everywhere is definitely the simplest thing we can do so I'll stick with that. This was surprisingly confusing...

@hoodmane
Copy link
Member Author

Hmm well the last iteration seems particularly broken...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants