Skip to content

pre-run and post-run hooks for cargo #15888

@bentheiii

Description

@bentheiii

Problem

My original intention, if this feature ever becomes available, is to automatically note dependency changes in a project's changelog with every command.

Proposed Solution

Hi there, coming from the python ecosystem, I'm used to having custom hooks running on dependency changes using poetry's plugin system. I was wondering if we could add the same capability to cargo.

I propose we add a new mapping to Cargo.toml manifests (and workspaces):

[hooks]
before_command="..."
after_command="..."

If these options are specified in Cargo.tomls, then cargo will execute the path specified, with the same arguments provided to cargo, as a child process, before and after any cargo command executes, respectively.

  • If the env var CARGO_SKIP_HOOKS is set to "1", cargo will skip running the hooks. Users could use this flag to call cargo from inside their hooks without the risk of infinite recursion.
  • If the before_command hook fails (exists with a non-zero status), cargo will not execute the command, and exit with a non-zero code.
  • If the cargo command itself fails, the after_command hook will not be called (a special hook for command failures may be considered in the future)
  • If the after_command hook fails, cargo will exit with a non-zero code.

Notes

For example, with the Cargo.toml

[package]
...

[hooks]
before_command="foo"
after_command="bar"

Then, when calling cargo add regex, cargo will do the following

  • execute a child process with executable foo with arguments add regex
  • add the regex crate dependency to the package, as normal
  • execute a child process with executable bar with arguments add regex

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-configurationArea: cargo config files and env varsC-feature-requestCategory: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`S-triageStatus: This issue is waiting on initial triage.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions