Skip to content

Commit 2a37fcd

Browse files
committed
Add support for Rust CLI dependencies
Also consistently build the hook using `cargo install`.
1 parent 7f85da1 commit 2a37fcd

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

pre_commit/languages/rust.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def get_env_patch(target_dir):
2222
return (
2323
(
2424
'PATH',
25-
(os.path.join(target_dir, 'release'), os.pathsep, Var('PATH')),
25+
(os.path.join(target_dir, 'bin'), os.pathsep, Var('PATH')),
2626
),
2727
)
2828

@@ -47,20 +47,36 @@ def _add_dependencies(cargo_toml_path, additional_dependencies):
4747
f.truncate()
4848

4949

50-
def install_environment(prefix, version, additional_dependencies):
50+
def install_environment(prefix, version, additional_deps):
5151
helpers.assert_version_default('rust', version)
5252
directory = prefix.path(
5353
helpers.environment_dir(ENVIRONMENT_DIR, 'default'),
5454
)
5555

56-
if len(additional_dependencies) > 0:
57-
_add_dependencies(prefix.path('Cargo.toml'), additional_dependencies)
56+
# There are two cases where we might want to specify more dependencies:
57+
# as dependencies for the library being built, and as binary packages
58+
# to be `cargo install`'d.
59+
#
60+
# Unlike e.g. Python, if we just `cargo install` a library, it won't be
61+
# used for compilation. And if we add a crate providing a binary to the
62+
# `Cargo.toml`, the binary won't be built.
63+
#
64+
# Because of this, we allow specifying "cli" dependencies by prefixing
65+
# with 'cli:'.
66+
cli_deps = {dep for dep in additional_deps if dep.startswith('cli:')}
67+
lib_deps = set(additional_deps) - cli_deps
68+
69+
if len(lib_deps) > 0:
70+
_add_dependencies(prefix.path('Cargo.toml'), lib_deps)
5871

5972
with clean_path_on_failure(directory):
60-
cmd_output(
61-
'cargo', 'build', '--release', '--bins', '--target-dir', directory,
62-
cwd=prefix.prefix_dir,
63-
)
73+
packages_to_install = {()} | {(dep[len('cli:'):],) for dep in cli_deps}
74+
75+
for package in packages_to_install:
76+
cmd_output(
77+
'cargo', 'install', '--bins', '--root', directory, *package,
78+
cwd=prefix.prefix_dir
79+
)
6480

6581

6682
def run_hook(prefix, hook, file_args):

0 commit comments

Comments
 (0)