Build hooks run for every selected version of build targets.
The initialization stage occurs immediately before each build and the finalization stage occurs immediately after. Each stage has the opportunity to view or modify build data.
Build data is a simple mapping whose contents can influence the behavior of builds. Which fields exist and are recognized depends on each build target.
The following fields are always present and recognized by the build system itself:
Field
Type
Description
artifacts
list[str]
This is a list of extra artifact patterns and should generally only be appended to
force_include
dict[str,str]
This is a mapping of extra forced inclusion paths, with this mapping taking precedence in case of conflicts
build_hooks
tuple[str,...]
This is an immutable sequence of the names of the configured build hooks and matches the order in which they run
Attention
While user-facing TOML options are hyphenated, build data fields should be named with underscores to allow plugins to use them as valid Python identifiers.
In some cases it may be necessary to use force_include rather than artifacts. For example, say that you want to install a lib.so directly at the root of site-packages and a project defines a packagesrc/foo. If you create src/lib.so, there will never be a match because the directory traversal starts at src/foo rather than src. In that case you must do either:
Source code in backend/src/hatchling/builders/hooks/plugin/interface.py
classBuildHookInterface(Generic[BuilderConfigBound]):# no cov""" Example usage: ```python tab="plugin.py" from hatchling.builders.hooks.plugin.interface import BuildHookInterface class SpecialBuildHook(BuildHookInterface): PLUGIN_NAME = 'special' ... ``` ```python tab="hooks.py" from hatchling.plugin import hookimpl from .plugin import SpecialBuildHook @hookimpl def hatch_register_build_hook(): return SpecialBuildHook ``` """PLUGIN_NAME=''"""The name used for selection."""def__init__(self,root:str,config:dict[str,Any],build_config:BuilderConfigBound,metadata:ProjectMetadata,directory:str,target_name:str,app:Application|None=None,)->None:self.__root=rootself.__config=configself.__build_config=build_configself.__metadata=metadataself.__directory=directoryself.__target_name=target_nameself.__app=app@propertydefapp(self)->Application:""" An instance of [Application](../utilities.md#hatchling.bridge.app.Application). """ifself.__appisNone:fromhatchling.bridge.appimportApplicationself.__app=cast(Application,Application().get_safe_application())returnself.__app@propertydefroot(self)->str:""" The root of the project tree. """returnself.__root@propertydefconfig(self)->dict[str,Any]:""" The cumulative hook configuration. ```toml config-example [tool.hatch.build.hooks.<PLUGIN_NAME>] [tool.hatch.build.targets.<TARGET_NAME>.hooks.<PLUGIN_NAME>] ``` """returnself.__config@propertydefmetadata(self)->ProjectMetadata:# Undocumented for nowreturnself.__metadata@propertydefbuild_config(self)->BuilderConfigBound:""" An instance of [BuilderConfig](../utilities.md#hatchling.builders.config.BuilderConfig). """returnself.__build_config@propertydefdirectory(self)->str:""" The build directory. """returnself.__directory@propertydeftarget_name(self)->str:""" The plugin name of the build target. """returnself.__target_namedefclean(self,versions:list[str])->None:""" This occurs before the build process if the `-c`/`--clean` flag was passed to the [`build`](../../cli/reference.md#hatch-build) command, or when invoking the [`clean`](../../cli/reference.md#hatch-clean) command. """definitialize(self,version:str,build_data:dict[str,Any])->None:""" This occurs immediately before each build. Any modifications to the build data will be seen by the build target. """deffinalize(self,version:str,build_data:dict[str,Any],artifact_path:str)->None:""" This occurs immediately after each build and will not run if the `--hooks-only` flag was passed to the [`build`](../../cli/reference.md#hatch-build) command. The build data will reflect any modifications done by the target during the build. """
This occurs before the build process if the -c/--clean flag was passed to the build command, or when invoking the clean command.
Source code in backend/src/hatchling/builders/hooks/plugin/interface.py
defclean(self,versions:list[str])->None:""" This occurs before the build process if the `-c`/`--clean` flag was passed to the [`build`](../../cli/reference.md#hatch-build) command, or when invoking the [`clean`](../../cli/reference.md#hatch-clean) command. """
Any modifications to the build data will be seen by the build target.
Source code in backend/src/hatchling/builders/hooks/plugin/interface.py
definitialize(self,version:str,build_data:dict[str,Any])->None:""" This occurs immediately before each build. Any modifications to the build data will be seen by the build target. """
This occurs immediately after each build and will not run if the --hooks-only flag was passed to the build command.
The build data will reflect any modifications done by the target during the build.
Source code in backend/src/hatchling/builders/hooks/plugin/interface.py
deffinalize(self,version:str,build_data:dict[str,Any],artifact_path:str)->None:""" This occurs immediately after each build and will not run if the `--hooks-only` flag was passed to the [`build`](../../cli/reference.md#hatch-build) command. The build data will reflect any modifications done by the target during the build. """