Does Cargo have an equivalent of npm scripts?

59 points smthamazing

The package manager NPM, used by default in the NodeJS world, allows you to define project-specific scripts:

// package.json
"scripts": {
    "build": "cross-env env=production && npm run build && echo 'success'",
    "lint": "eslint src/**/*.js && some-custom-linter",
    ...
}

You can run them by executing npm run your_script_name.

They serve several purposes:

  • They allow you to define commonly used commands for a project in a single standardized place.
  • Locally installed tools and binary packages are added to the environment when running a script, so within the script you can simply execute eslint instead of node_modules/.bin/eslint. In general I never want to install binary packages globally, since every project might require a different version of a build or linting tool.
  • With some extra binary dependencies (like cross-env in my example) you can easily define environment variables before executing a script, and this will work everywhere, whether it's Bash, PowerShell or CMD. Without this I have to either provide 2-3 OS-specific scripts (.sh, .bat, .ps1) or write several different explanations about setting environment variables like RUST_BACKTRACE=1.

Does Cargo have something like this? I know there are packages that try to achieve something similar, but I wonder if there is a standardized solution for defining project-specific scripts and environment variables.

For a specific use case, let's say I want to add a script like cargo run debug that automatically sets RUST_BACKTRACE=1 on any operating system before building and running the program. Is there a way to do this with Cargo?

Thanks!