Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/hatch/cli/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ def config(self) -> RootConfig:
def get_environment(self, env_name: str | None = None) -> EnvironmentInterface:
return self.project.get_environment(env_name)

def prepare_environment(self, environment: EnvironmentInterface, *, keep_env: bool = False):
self.project.prepare_environment(environment, keep_env=keep_env)
def prepare_environment(
self, environment: EnvironmentInterface, *, keep_env: bool = False, reinstall: bool = False
):
self.project.prepare_environment(environment, keep_env=keep_env, reinstall=reinstall)

def run_shell_commands(self, context: ExecutionContext) -> None:
with context.env.command_context():
Expand Down Expand Up @@ -110,6 +112,7 @@ def runner_context(
ignore_compat: bool = False,
display_header: bool = False,
keep_env: bool = False,
reinstall: bool = False,
) -> Generator[ExecutionContext, None, None]:
if self.verbose or len(environments) > 1:
display_header = True
Expand All @@ -136,7 +139,7 @@ def runner_context(
context = ExecutionContext(environment)
yield context

self.prepare_environment(environment, keep_env=keep_env)
self.prepare_environment(environment, keep_env=keep_env, reinstall=reinstall)
self.execute_context(context)

if incompatible:
Expand Down
3 changes: 3 additions & 0 deletions src/hatch/cli/env/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def filter_environments(environments, filter_data):
"--force-continue", is_flag=True, help="Run every command and if there were any errors exit with the first code"
)
@click.option("--ignore-compat", is_flag=True, help="Ignore incompatibility when selecting specific environments")
@click.option("--reinstall", is_flag=True, help="Reinstall the project before running the command")
@click.pass_obj
def run(
app: Application,
Expand All @@ -44,6 +45,7 @@ def run(
filter_json: str | None,
force_continue: bool,
ignore_compat: bool,
reinstall: bool,
):
"""
Run commands within project environments.
Expand Down Expand Up @@ -139,6 +141,7 @@ def run(
ignore_compat=ignore_compat or matrix_selected,
display_header=matrix_selected,
keep_env=bool(os.environ.get(AppEnvVars.KEEP_ENV)),
reinstall=reinstall,
):
if context.env.name == "system":
context.env.exists = lambda: True # type: ignore[method-assign]
Expand Down
4 changes: 3 additions & 1 deletion src/hatch/cli/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
context_settings={"help_option_names": [], "ignore_unknown_options": True},
)
@click.argument("args", metavar="[ENV:]ARGS...", required=True, nargs=-1)
@click.option("--reinstall", is_flag=True, help="Reinstall the project before running the command")
@click.pass_context
def run(ctx: click.Context, args: tuple[str, ...]):
def run(ctx: click.Context, args: tuple[str, ...], reinstall: bool):
"""
Run commands within project environments.
This is a convenience wrapper around the [`env run`](#hatch-env-run) command.
Expand Down Expand Up @@ -160,4 +161,5 @@ def run(ctx: click.Context, args: tuple[str, ...]):
env_names=[env_name],
included_variable_specs=included_variables,
excluded_variable_specs=excluded_variables,
reinstall=reinstall,
)
9 changes: 7 additions & 2 deletions src/hatch/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ def managed_environment(

# Ensure that this method is clearly written since it is
# used for documenting the life cycle of environments.
def prepare_environment(self, environment: EnvironmentInterface, *, keep_env: bool):
if not environment.exists():
def prepare_environment(self, environment: EnvironmentInterface, *, keep_env: bool, reinstall: bool = False):
environment_existed = environment.exists()
if not environment_existed:
with self.managed_environment(environment, keep_env=keep_env):
self.env_metadata.reset(environment)

Expand Down Expand Up @@ -255,6 +256,10 @@ def prepare_environment(self, environment: EnvironmentInterface, *, keep_env: bo

self.env_metadata.update_dependency_hash(environment, new_dep_hash)

if reinstall and environment_existed and not environment.dev_mode and not environment.skip_install:
with environment.app_status_project_installation():
environment.install_project()

def prepare_build_environment(self, *, targets: list[str] | None = None, keep_env: bool = False) -> None:
from hatch.project.constants import BUILD_BACKEND, BuildEnvVars
from hatch.utils.structures import EnvVars
Expand Down
47 changes: 47 additions & 0 deletions tests/cli/run/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,53 @@ def test_help(hatch):
assert long.exit_code == 0, long.output

assert short.output == long.output
assert "--reinstall" in long.output


@pytest.mark.requires_internet
def test_non_editable_project_is_reinstalled_when_source_changes(hatch, temp_dir):
project_path = temp_dir / "project"
(project_path / "src" / "demo").mkdir(parents=True)
(project_path / "src" / "demo" / "__init__.py").write_text("VALUE = 'first'\n")
(project_path / "pyproject.toml").write_text(
"""\
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[project]
name = "demo"
version = "0.0.1"

[tool.hatch.envs.default]
dev-mode = false
"""
)

with project_path.as_cwd():
first = hatch(
"run",
"python",
"-c",
"import demo,pathlib; pathlib.Path('result.txt').write_text(demo.VALUE)",
)

assert first.exit_code == 0, first.output
assert (project_path / "result.txt").read_text() == "first"

(project_path / "src" / "demo" / "__init__.py").write_text("VALUE = 'second'\n")

with project_path.as_cwd():
second = hatch(
"run",
"--reinstall",
"python",
"-c",
"import demo,pathlib; pathlib.Path('result.txt').write_text(demo.VALUE)",
)

assert second.exit_code == 0, second.output
assert (project_path / "result.txt").read_text() == "second"


def test_automatic_creation(hatch, helpers, temp_dir, config_file):
Expand Down