Skip to content

Commit

Permalink
Improve builds (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Jul 7, 2024
1 parent 332c407 commit e843c42
Show file tree
Hide file tree
Showing 52 changed files with 2,741 additions and 1,125 deletions.
4 changes: 2 additions & 2 deletions docs/blog/posts/release-hatch-160.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ Hatch [v1.6.0](https://github.com/pypa/hatch/releases/tag/hatch-v1.6.0) brings i

## Build environments

Originally, the environment interface method for [providing builder sub-environments](../../plugins/environment/reference.md#hatch.env.plugin.interface.EnvironmentInterface.build_environment) was intended to be used in conjunction with some cleanup logic in order to provide a fresh setup every time. However, this is unnecessary in practice because build dependencies rarely change.
Originally, the environment interface method for providing builder sub-environments was intended to be used in conjunction with some cleanup logic in order to provide a fresh setup every time. However, this is unnecessary in practice because build dependencies rarely change.

Without caching, repeat build environment use is slow which affects the following scenarios:

- the [`build`](../../cli/reference.md#hatch-build) command
- commands that read project metadata, like [`dep hash`](../../cli/reference.md#hatch-dep-hash), if any fields are [set dynamically](../../config/metadata.md#dynamic)

Now a new environment interface method [`build_environment_exists`](../../plugins/environment/reference.md#hatch.env.plugin.interface.EnvironmentInterface.build_environment_exists) is used by Hatch to determine whether or not it has already been created, for implementations that have a caching mechanism.
Now a new environment interface method `build_environment_exists` is used by Hatch to determine whether or not it has already been created, for implementations that have a caching mechanism.

The [`virtual`](../../plugins/environment/virtual.md) environment type now uses this method to cache build environments.

Expand Down
4 changes: 2 additions & 2 deletions docs/config/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ dev-mode-exact = true

A build target can be provided by any [builder plugin](../plugins/builder/reference.md). There are three built-in build targets: [wheel](../plugins/builder/wheel.md), [sdist](../plugins/builder/sdist.md), and [custom](../plugins/builder/custom.md).

### Dependencies
### Dependencies ### {: #target-dependencies }

You can specify additional dependencies that will be installed in each build environment, such as for third party builders:

Expand Down Expand Up @@ -283,7 +283,7 @@ or to specific build targets:
[tool.hatch.build.targets.<TARGET_NAME>.hooks.<HOOK_NAME>]
```

### Dependencies
### Dependencies ### {: #hook-dependencies }

You can specify additional dependencies that will be installed in each build environment, such as for third party build hooks:

Expand Down
39 changes: 39 additions & 0 deletions docs/config/internal/build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Build environment configuration

-----

You can fully alter the behavior of the environment used by the [`build`](../../cli/reference.md#hatch-build) command.

## Dependencies

Build environments will always have what is required by the [build system](../build.md#build-system), [targets](../build.md#target-dependencies), and [hooks](../build.md#hook-dependencies).

You can define [dependencies](../environment/overview.md#dependencies) that your builds may require in the environment as well:

```toml config-example
[tool.hatch.envs.hatch-build]
dependencies = [
"cython",
]
```

!!! warning "caution"
It's recommended to only use the standard mechanisms to define build dependencies for better compatibility with other tools.

## Environment variables

You can define [environment variables](../environment/overview.md#environment-variables) that will be set during builds:

```toml config-example
[tool.hatch.envs.hatch-build.env-vars]
SOURCE_DATE_EPOCH = "1580601600"
```

## Installer

By default, [UV is enabled](../../how-to/environment/select-installer.md). You may disable that behavior as follows:

```toml config-example
[tool.hatch.envs.hatch-build]
installer = "pip"
```
10 changes: 10 additions & 0 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

## Unreleased

***Changed:***

- Environment type plugins are now no longer expected to support a pseudo-build environment as any environment now may be used for building. The following methods have been removed: `build_environment`, `build_environment_exists`, `run_builder`, `construct_build_command`

***Added:***

- The `version` and `project metadata` commands now support projects that do not use Hatchling as the build backend
- Build environments can now be configured, the default build environment is `hatch-build`
- The environment interface now has the following methods and properties in order to better support builds on remote machines: `project_root`, `sep`, `pathsep`, `fs_context`

## [1.12.0](https://github.com/pypa/hatch/releases/tag/hatch-v1.12.0) - 2024-05-28 ## {: #hatch-v1.12.0 }

***Changed:***
Expand Down
12 changes: 6 additions & 6 deletions docs/plugins/environment/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ requires = [

Whenever an environment is used, the following logic is performed:

::: hatch.cli.application.Application.prepare_environment
::: hatch.project.core.Project.prepare_environment
options:
show_root_heading: false
show_root_toc_entry: false

## Build environments

All environment types should [offer support](#hatch.env.plugin.interface.EnvironmentInterface.build_environment) for a special sub-environment in which projects can be built. This environment is used in the following scenarios:
All environment types should [offer support](#hatch.env.plugin.interface.EnvironmentInterface.fs_context) for synchronized storage between the local file system and the environment. This functionality is used in the following scenarios:

- the [`build`](../../cli/reference.md#hatch-build) command
- commands that read dependencies, like [`dep hash`](../../cli/reference.md#hatch-dep-hash), if any [project dependencies](../../config/metadata.md#dependencies) are [set dynamically](../../config/metadata.md#dynamic)
Expand All @@ -51,8 +51,10 @@ All environment types should [offer support](#hatch.env.plugin.interface.Environ
- dependencies_in_sync
- sync_dependencies
- dependency_hash
- build_environment
- build_environment_exists
- project_root
- sep
- pathsep
- fs_context
- activate
- deactivate
- app_status_creation
Expand All @@ -78,8 +80,6 @@ All environment types should [offer support](#hatch.env.plugin.interface.Environ
- skip_install
- dev_mode
- description
- run_builder
- construct_build_command
- command_context
- enter_shell
- run_shell_command
Expand Down
11 changes: 11 additions & 0 deletions docs/plugins/utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@
show_source: false
members:
- formatters

::: hatch.env.plugin.interface.FileSystemContext
options:
show_source: false
members:
- env
- sync_local
- sync_env
- local_path
- env_path
- join
2 changes: 2 additions & 0 deletions hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ post-install-commands = [
[envs.hatch-test]
extra-dependencies = [
"filelock",
"flit-core",
"hatchling",
"pyfakefs",
"trustme",
# Hatchling dynamic dependency
Expand Down
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ nav:
- Internal:
- Testing: config/internal/testing.md
- Static analysis: config/internal/static-analysis.md
- Building: config/internal/build.md
- Context formatting: config/context.md
- Project templates: config/project-templates.md
- Hatch: config/hatch.md
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies = [
"packaging>=23.2",
"pexpect~=4.8",
"platformdirs>=2.5.0",
"pyproject-hooks",
"rich>=11.2.0",
"shellingham>=1.4.0",
"tomli-w>=1.0",
Expand Down Expand Up @@ -116,6 +117,7 @@ omit = [
"backend/src/hatchling/ouroboros.py",
"src/hatch/__main__.py",
"src/hatch/cli/new/migrate.py",
"src/hatch/project/frontend/scripts/*",
"src/hatch/utils/shells.py",
]

Expand Down
12 changes: 10 additions & 2 deletions src/hatch/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from __future__ import annotations

import os
from typing import cast

import click

Expand Down Expand Up @@ -159,13 +162,16 @@ def hatch(ctx: click.Context, env_name, project, verbose, quiet, color, interact
app.cache_dir = Path(cache_dir or app.config.dirs.cache).expand()

if project:
app.project = Project.from_config(app.config, project)
if app.project is None or app.project.root is None:
potential_project = Project.from_config(app.config, project)
if potential_project is None or potential_project.root is None:
app.abort(f'Unable to locate project {project}')

app.project = cast(Project, potential_project)
app.project.set_app(app)
return

app.project = Project(Path.cwd())
app.project.set_app(app)

if app.config.mode == 'local':
return
Expand All @@ -182,6 +188,7 @@ def hatch(ctx: click.Context, env_name, project, verbose, quiet, color, interact
else:
app.project = possible_project

app.project.set_app(app)
return

if app.config.mode == 'aware' and app.project.root is None:
Expand All @@ -195,6 +202,7 @@ def hatch(ctx: click.Context, env_name, project, verbose, quiet, color, interact
else:
app.project = possible_project

app.project.set_app(app)
return


Expand Down
Loading

0 comments on commit e843c42

Please sign in to comment.