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

Running hatch build and then hatch shell with locally configured envs fails #751

Closed
mapio opened this issue Feb 9, 2023 · 1 comment
Closed

Comments

@mapio
Copy link

mapio commented Feb 9, 2023

With version 1.6.0, If you have

[dirs.env]
virtual = ".hatch"

in your config.toml and you create a new environment, say with

python3 -m venv venv
. ./venv/bin/activate
pip install hatch
hatch new test-project
cd test-project/

running a build and then entering the default environment with

hatch build
hatch shell

produces the following stack trace:

╭─────────────────────────────── Traceback (most recent call last) ────────────────────────────────╮
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/cli/__init__.py:205 in main                  │
│                                                                                                  │
│   202                                                                                            │
│   203 def main():  # no cov                                                                      │
│   204 │   try:                                                                                   │
│ ❱ 205 │   │   return hatch(windows_expand_args=False)                                            │
│   206 │   except Exception:                                                                      │
│   207 │   │   from rich.console import Console                                                   │
│   208                                                                                            │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/core.py:1130 in __call__                     │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/core.py:1055 in main                         │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/core.py:1657 in invoke                       │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/core.py:1404 in invoke                       │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/core.py:760 in invoke                        │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/click/decorators.py:38 in new_func                 │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/cli/shell/__init__.py:46 in shell            │
│                                                                                                  │
│   43 │                                                                                           │
│   44 │   with app.project.location.as_cwd():                                                     │
│   45 │   │   environment = app.get_environment()                                                 │
│ ❱ 46 │   │   app.prepare_environment(environment)                                                │
│   47 │   │                                                                                       │
│   48 │   │   first_run_indicator = app.cache_dir / 'shell' / 'first_run'                         │
│   49 │   │   if not first_run_indicator.is_file():                                               │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/cli/application.py:91 in prepare_environment │
│                                                                                                  │
│    88 │   │   │   │   │   │   self.run_shell_commands(environment, environment.post_install_co   │
│    89 │   │                                                                                      │
│    90 │   │   with self.status_waiting('Checking dependencies'):                                 │
│ ❱  91 │   │   │   dependencies_in_sync = environment.dependencies_in_sync()                      │
│    92 │   │                                                                                      │
│    93 │   │   if not dependencies_in_sync:                                                       │
│    94 │   │   │   with self.status_waiting('Syncing dependencies'):                              │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/env/virtual.py:109 in dependencies_in_sync   │
│                                                                                                  │
│   106 │   │                                                                                      │
│   107 │   │   from hatchling.dep.core import dependencies_in_sync                                │
│   108 │   │                                                                                      │
│ ❱ 109 │   │   with self.safe_activation():                                                       │
│   110 │   │   │   return dependencies_in_sync(                                                   │
│   111 │   │   │   │   self.dependencies_complex, sys_path=self.virtual_env.sys_path, environme   │
│   112 │   │   │   )                                                                              │
│                                                                                                  │
│ /usr/lib/python3.10/contextlib.py:135 in __enter__                                               │
│                                                                                                  │
│   132 │   │   # they are only needed for recreation, which is not possible anymore               │
│   133 │   │   del self.args, self.kwds, self.func                                                │
│   134 │   │   try:                                                                               │
│ ❱ 135 │   │   │   return next(self.gen)                                                          │
│   136 │   │   except StopIteration:                                                              │
│   137 │   │   │   raise RuntimeError("generator didn't yield") from None                         │
│   138                                                                                            │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/env/virtual.py:188 in safe_activation        │
│                                                                                                  │
│   185 │   @contextmanager                                                                        │
│   186 │   def safe_activation(self):                                                             │
│   187 │   │   # Set user-defined environment variables first so ours take precedence             │
│ ❱ 188 │   │   with self.get_env_vars(), self:                                                    │
│   189 │   │   │   yield                                                                          │
│   190                                                                                            │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/env/plugin/interface.py:890 in __enter__     │
│                                                                                                  │
│   887 │   │   │   yield                                                                          │
│   888 │                                                                                          │
│   889 │   def __enter__(self):                                                                   │
│ ❱ 890 │   │   self.activate()                                                                    │
│   891 │   │   return self                                                                        │
│   892 │                                                                                          │
│   893 │   def __exit__(self, exc_type, exc_value, traceback):                                    │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/env/virtual.py:56 in activate                │
│                                                                                                  │
│    53 │   │   return {'system-packages': bool, 'path': bool}                                     │
│    54 │                                                                                          │
│    55 │   def activate(self):                                                                    │
│ ❱  56 │   │   self.virtual_env.activate()                                                        │
│    57 │                                                                                          │
│    58 │   def deactivate(self):                                                                  │
│    59 │   │   self.virtual_env.deactivate()                                                      │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/venv/core.py:31 in activate                  │
│                                                                                                  │
│    28 │   │   if old_path is None:                                                               │
│    29 │   │   │   os.environ['PATH'] = str(self.executables_directory)                           │
│    30 │   │   else:                                                                              │
│ ❱  31 │   │   │   os.environ['PATH'] = f'{self.executables_directory}{os.pathsep}{old_path}'     │
│    32 │   │                                                                                      │
│    33 │   │   for env_var in self.IGNORED_ENV_VARS:                                              │
│    34 │   │   │   self._env_vars_to_restore[env_var] = os.environ.pop(env_var, None)             │
│                                                                                                  │
│ /tmp/BUGREP/venv/lib/python3.10/site-packages/hatch/venv/core.py:88 in executables_directory     │
│                                                                                                  │
│    85 │   │   │   │   else:                                                                      │
│    86 │   │   │   │   │   raise OSError(f'Unable to locate executables directory within: {self   │
│    87 │   │   │   else:                                                                          │
│ ❱  88 │   │   │   │   raise OSError(f'Unable to locate executables directory within: {self.dir   │
│    89 │   │                                                                                      │
│    90 │   │   return self._executables_directory                                                 │
│    91                                                                                            │
╰──────────────────────────────────────────────────────────────────────────────────────────────────╯
OSError: Unable to locate executables directory within: /tmp/BUGREP/test-project/.hatch/test-project

On the other hand, if you invert the order of the last two commands:

python3 -m venv venv
. ./venv/bin/activate
pip install hatch
hatch new test-project
cd test-project/
hatch shell
[exit the shell]
hatch build

everything runs as expected and you get

[sdist]
dist/test_project-0.0.1.tar.gz

[wheel]
dist/test_project-0.0.1-py3-none-any.whl
@ofek
Copy link
Collaborator

ofek commented Dec 5, 2023

Fixed by #1022

@ofek ofek closed this as completed Dec 5, 2023
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

2 participants