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
2 changes: 1 addition & 1 deletion backend/src/hatchling/cli/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def version_impl(

if 'version' in metadata.config.get('project', {}):
if desired_version:
app.abort('Cannot set version when it is statically defined by the `project.version` field')
app.abort('Use `hatch version ...` to update version when it is statically defined by the `project.version` field')
else:
app.display(metadata.core.version)
return
Expand Down
16 changes: 13 additions & 3 deletions src/hatch/cli/version/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ def version(app: Application, *, desired_version: str | None, force: bool):
app.abort(f'Project {app.project.chosen_name} (not a project)')

if 'version' in app.project.metadata.config.get('project', {}):
version = app.project.metadata.config['project']['version']
if desired_version:
app.abort('Cannot set version when it is statically defined by the `project.version` field')
from hatchling.version.scheme.standard import StandardScheme

updated_version = StandardScheme(str(app.project.location), {}).update(
desired_version, version, {}
)
app.project.metadata.config['project']['version'] = updated_version
app.project.save_config(app.project.raw_config)

app.display_info(f'Old: {version}')
app.display_info(f'New: {updated_version}')
else:
app.display(app.project.metadata.config['project']['version'])
return
app.display(version)
return

from hatch.config.constants import VersionEnvVars
from hatch.project.constants import BUILD_BACKEND
Expand Down
7 changes: 5 additions & 2 deletions tests/cli/version/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,9 +361,12 @@ def test_set_static(hatch, helpers, temp_dir):
with path.as_cwd(env_vars={ConfigEnvVars.DATA: str(data_path)}):
result = hatch('version', 'minor,rc')

assert result.exit_code == 1, result.output
assert result.exit_code == 0, result.output
assert result.output == helpers.dedent(
"""
Cannot set version when it is statically defined by the `project.version` field
Old: 1.2.3
New: 1.3.0rc0
"""
)
project = Project(path)
assert project.raw_config['project']['version'] == '1.3.0rc0', 'should update static version'