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

update: venv detection #78

Closed
metaist opened this issue Aug 23, 2024 · 9 comments
Closed

update: venv detection #78

metaist opened this issue Aug 23, 2024 · 9 comments

Comments

@metaist
Copy link
Owner

metaist commented Aug 23, 2024

The way uv does this might work better:
https://github.com/astral-sh/uv/blob/main/python/uv/__main__.py#L7

But what about walking the tree looking for a .venv?

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

Maybe we need to walk the parents looking for glob like */pyvenv.cfg.

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

Actually we might similarly be looking for node_modules/.bin as a glob.

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

https://pnpm.io/cli/run

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

If we're in a venv, don't even search for a venv.

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

One problem with the VIRTUAL_ENV approach is that I'm not exactly sure how uvx and pipx will set that env. I think it's only set by the activate script in which case it's superior because the whole point is to figure out whether we should add the activate script.

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

The uv approach doesn't actually work for ds:

$ deactivate
$ ds --debug 'echo'
ds.main: DEBUG: Args(help=False, version=False, debug=True, dry_run=False, no_config=False, no_project=False, list_=False, cwd=None, env={}, env_file=None, file=None, workspace=[], task=Task(origin=None, origin_key='', name='', help='', verbatim=False, depends=[Task(origin=None, origin_key='', name='#composite', help='', verbatim=False, depends=[], cmd='echo', code=0, args=[], cwd=None, env={}, _env={}, keep_going=False)], cmd='', code=0, args=[], cwd=None, env={}, _env={}, keep_going=False))
ds.main: WARNING: You are using a development version of ds.
ds.main: DEBUG: Loading config. To disable: add --no-config
ds.tasks.find_config: DEBUG: require_workspace=False
ds.searchers.glob_parents: DEBUG: check ~/github.com/ds/ds.toml
ds.searchers.glob_parents: DEBUG: check ~/github.com/ds/pyproject.toml
ds.pushd: DEBUG: staying in: ~/github.com/ds
ds.runner.find_project: INFO: Searching for project dependencies. To disable: add --no-project
ds.runner.find_project: DEBUG: [python] venv detected: ~/.local/share/uv/tools/ds-run
ds.runner.find_project: DEBUG: [node] Not in package.json; not searching for node_modules/.bin

> ds echo
$ echo

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

For posterity:

# See: https://github.com/astral-sh/uv/blob/3f147eeb06b46571ab8113e465c842d33221f473/python/uv/__main__.py#L7C1-L23C14
def get_venv() -> str:
    """Return the path to the current virtual environment."""
    if value := ENV.get("VIRTUAL_ENV"):  # use any set value
        return value

    # check if we're in a venv
    venv_marker = Path(sys.prefix) / "pyvenv.cfg"
    if venv_marker.exists():
        return sys.prefix

    return ""

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

The previous approach also doesn't work:

def get_venv() -> str:
    """Return the path to the current virtual environment."""
    if value := ENV.get("VIRTUAL_ENV"):  # use any set value
        return value

    # check if we're in a venv
    venv_marker = Path(sys.prefix) / "pyvenv.cfg"
    if venv_marker.exists():
        return sys.prefix

    if hasattr(sys, "real_prefix"):
        return sys.real_prefix

    if hasattr(sys, "base_prefix") and sys.base_prefix != sys.prefix:
        return sys.prefix

    return ""

@metaist
Copy link
Owner Author

metaist commented Aug 23, 2024

Ok, I think I'm just going to look at VIRTUAL_ENV.

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

No branches or pull requests

1 participant