Sitelet https://github.com/pre-commit/pre-commit/commit/c6366cbeea756bf73912780e49d3dbef25fe098a
Skip to content

Commit c6366cb

Browse files
author
marsha
committed
Change Rust to install environment with cargo add over toml
1 parent 5c9e844 commit c6366cb

2 files changed

Lines changed: 7 additions & 14 deletions

File tree

‎pre_commit/languages/rust.py‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
from typing import Generator
1212
from typing import Sequence
1313

14-
import toml
15-
1614
import pre_commit.constants as C
1715
from pre_commit import parse_shebang
1816
from pre_commit.envcontext import envcontext
@@ -82,18 +80,13 @@ def in_env(
8280

8381

8482
def _add_dependencies(
85-
cargo_toml_path: str,
83+
prefix: Prefix,
8684
additional_dependencies: set[str],
8785
) -> None:
88-
with open(cargo_toml_path, 'r+') as f:
89-
cargo_toml = toml.load(f)
90-
cargo_toml.setdefault('dependencies', {})
91-
for dep in additional_dependencies:
92-
name, _, spec = dep.partition(':')
93-
cargo_toml['dependencies'][name] = spec or '*'
94-
f.seek(0)
95-
toml.dump(cargo_toml, f)
96-
f.truncate()
86+
for dep in additional_dependencies:
87+
name, _, spec = dep.partition(':')
88+
crate = name + (f'@{spec}' if spec else '')
89+
helpers.run_setup_cmd(prefix, ('cargo', 'add', crate))
9790

9891

9992
def install_rust_with_toolchain(toolchain: str) -> None:
@@ -152,7 +145,7 @@ def install_environment(
152145
lib_deps = set(additional_dependencies) - cli_deps
153146

154147
if len(lib_deps) > 0:
155-
_add_dependencies(prefix.path('Cargo.toml'), lib_deps)
148+
_add_dependencies(prefix, lib_deps)
156149

157150
with clean_path_on_failure(directory):
158151
packages_to_install: set[tuple[str, ...]] = {('--path', '.')}

‎tests/repository_test.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ def test_additional_rust_lib_dependencies_installed(
485485
path = make_repo(tempdir_factory, 'rust_hooks_repo')
486486
config = make_config_from_repo(path)
487487
# A small rust package with no dependencies.
488-
deps = ['shellharden:3.1.0']
488+
deps = ['shellharden:3.1.0', 'git-version']
489489
config['hooks'][0]['additional_dependencies'] = deps
490490
hook = _get_hook(config, store, 'rust-hook')
491491
binaries = os.listdir(

0 commit comments

Comments
 (0)