Git Product home page Git Product logo

setup-rust's Introduction

Setup Rust and Cargo

A one-stop-shop GitHub action for setting up Rust and Cargo. Will automatically setup the Rust toolchain with rustup, cache the ~/.cargo/registry and /target/debug directories, and install Cargo binaries (when applicable).

jobs:
  ci:
    name: CI
    runs-on: ubuntu-latest
    steps:
      # ...
      - uses: moonrepo/setup-rust@v1
      - run: cargo test

Inputs

The following inputs are available for the action, and can be passed via with. All inputs are optional.

  • bins - Comma-separated list of global binaries to install into Cargo.
  • cache - Toggle caching of directories. Defaults to true.
  • cache-base - Base branch/ref to save a warmup cache on. Other branches/refs will restore from this base.
  • cache-target - Name of the target profile to cache. Defaults to debug.
  • channel - Toolchain specification/channel to explicitly install.
  • components - Comma-separated list of additional components to install.
  • inherit-toolchain - Inherit all toolchain settings from the rust-toolchain.toml file. Defaults to false.
  • targets - Comma-separated list of additional targets to install.
  • target-dirs - Comma-separated list of target folder paths, relative from the repository root. Defaults to target.
  • profile - Profile to install. Defaults to minimal.

Configuring the Rust toolchain

This action will automatically install the appropriate toolchain with rustup by inspecting the RUSTUP_TOOLCHAIN environment variable or the rust-toolchain.toml (preferred) or rust-toolchain configuration files. If no toolchain found, will default to stable.

# rust-toolchain.toml
[toolchain]
channel = "1.68.0"

When loading a configuration file, only the channel field is used, while the other fields are ignored. We chose this approach, as those other fields are typically for develop/release workflows, but not for CI, which requires a minimal/granular setup. However, this can be overwritten with the inherit-toolchain input.

The toolchain/channel can also be explicitly configured with the channel input, which takes highest precedence.

- uses: moonrepo/setup-rust@v1
  with:
    channel: '1.65.0'

Profile, components, and targets

Furthermore, this action supports rustup profiles, components, and targets, which can be configured with the profile, components, and targets inputs respectively. When not defined, profile defaults to minimal.

- uses: moonrepo/setup-rust@v1
  with:
    profile: complete

When using components, the input requires a comma separated list of component names.

- uses: moonrepo/setup-rust@v1
  with:
    components: clippy
- run: cargo clippy --workspace

When using targets, the input requires a comma separated list of target triples.

- uses: moonrepo/setup-rust@v1
  with:
    targets: 'x86_64-pc-windows-msvc,x86_64-pc-windows-gnu'

Installing Cargo binaries

If you require cargo-make, cargo-nextest, or other global binaries, this action supports installing Cargo binaries through the bins input, which requires a comma-separated list of crate names.

- uses: moonrepo/setup-rust@v1
  with:
    bins: cargo-nextest, [email protected]
  env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Binaries are installed with cargo-binstall under the hood. We suggest setting GITHUB_TOKEN to avoid rate limiting.

Caching in CI

By default this action will cache the ~/.cargo/registry and /target/debug directories to improve CI times. To disable caching, set the cache input to false. Furthermore, the target profile can be changed with the cache-target input, which defaults to debug.

- uses: moonrepo/setup-rust@v1
  with:
    cache: false
    cache-target: release

The following optimizations and considerations are taken into account when caching:

  • ~/.cargo
    • The /bin directory is not cached as we manage binary installation in this action via the bins input.
    • The /git directory is not cached as it's not necessary for CI. When required by Cargo or a crate, a checkout will be performed on-demand.
    • The /registry directory is cleaned before saving the cache. This includes removing src, .cache, and any other unnecessary files.
  • /target
    • Only the /debug profile is cached, as this profile is typically used for formatting, linting, and testing. This can be changed with the cache-target input.
    • The /examples and /incremental sub-directories are not cached as they are not necessary for CI.
    • All dep-info (*.d) files are removed, as they're meant for build systems and signaling re-executions.

The following sources are hashed for the generated cache key: $GITHUB_JOB, Cargo.lock, Rust version, Rust commit hash, and OS.

Warmup strategy

Another strategy that we support is called a warmup cache, where a base branch/ref is used to generate and save the cache (like master), and all other branches/refs will only restore this cache (and not save).

This can be enabled with the cache-base input, which requires a branch/ref name. This input also supports regex.

- uses: moonrepo/setup-rust@v1
  with:
    cache-base: master
    # With regex
    cache-base: (master|main|develop)

Compared to

actions-rs/*

The "official" actions have served their purpose, but after 3 years of no updates, severe lack of maintenance, and being full of deprecation warnings, it was time to create something new.

Outside of being evergreen, this action also supports the following features:

  • Automatically caches.
  • Installs Cargo bins.
  • Assumes rustup, cargo, and other commands are available globally. This allows you to use them directly in a run command, without having to use actions-rs/cargo.

dtolnay/rust-toolchain

Our action is very similar to theirs, which was a great implementation reference, but our action also supports the following features:

  • Extracts the toolchain/channel from rust-toolchain.toml and rust-toolchain configuration files.
  • Automatically caches.
  • Installs Cargo bins.

Swatinem/rust-cache

Their action only caches for Rust/Cargo, it doesn't actually setup Rust/Cargo. Our action is meant to do everything, but it's not as configurable as the alternatives.

Here are the key differences between the two:

  • Theirs caches the entire ~/.cargo directory, while our action only caches ~/.cargo/registry. View the reasoning above.
    • Our action also avoids an array of ~/.cargo/bin issues that theirs currently has.
  • Theirs includes compiler specific environment variables in the cache key, while our action currently does not.
  • Theirs supports a handful of inputs for controlling the cache key, while ours does not.
  • Theirs is a bit more smart in what it caches, while ours is a bit more brute force. We simply cache specific directories as-is after cleaning.

setup-rust's People

Contributors

g07cha avatar milesj avatar mlga avatar mozart409 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

setup-rust's Issues

Change behaviour of rust-toolchain config file

Hi,

I would like to ask if it would be possible to change the behaviour of the config file in regards to following

When loading a configuration file, only the channel field is used, while the other fields are ignored. We chose this approach, as those other fields are typically for develop/release workflows, but not for CI, which requires a minimal/granular setup.

Couldn't be the default to use the settings from the file and allow it to be overridden by the specific settings in the config of the action (components, targets and profile)?

error: link.exe not found

image

Hey, this might just be me not knowing what I'm doing, but I've tried a fair bit of things and none of them seemed to help.
workflow:

      - uses: moonrepo/setup-rust@v1
        with:
          profile: complete
          targets: 'x86_64-pc-windows-msvc,x86_64-pc-windows-gnu'
      - name: Build exe/
        run: cargo build --release --all-features --manifest-path exe/Cargo.toml --target=x86_64-pc-windows-msvc

pre and post build cache hashes are different on windows-latest

before build;
Attempting to restore cache
Generating cache key
Cache does not exist using key setup-rustcargo-v1-win32-d94c355bca58aa4c0186140d125e989ab8395e82

after build:

Size changed 1.63 GB => 313.25 MB (-1.32 GB, -80.81%)
Cleaning target/release before saving
Removing examples and incremental directories
Removing dep-info files (*.d)
Saving cache with key setup-rustcargo-v1-win32-1dbfb78edf8a75329b6134312e65b3ad0c5c67d8

allow building with `--locked`

tl;dr I would like to build bins with option --locked

From this failed build I got error

Run moonrepo/setup-rust@v1
  with:
    channel: 1.70.0
    bins: cross
    targets: aarch64-apple-ios
    cache: true
    cache-target: debug
    inherit-toolchain: false
    target-dirs: target

...

  WARN The package cargo-cache v0.8.3 will be installed from source (with cargo)
 WARN The package cross v0.2.5 will be installed from source (with cargo)

...

   Compiling cfg-if v1.0.0
error: failed to compile `cargo-cache v0.8.3`, intermediate artifacts can be found at `/var/folders/w3/xh1ngrc13bg46kmhfdprbpyw0000gn/T/cargo-installgoVcWl`

Caused by:
  package `cargo-platform v0.1.8` cannot be built because it requires rustc 1.73 or newer, while the currently active rustc version is 1.70.0
  Try re-running cargo install with `--locked`

From this rust.yml code

env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: 1
  MSRV_UPLOAD: "1.70.0"
jobs:
  job_cross_targets_macos:
    name: cross ${{ matrix.target }} on macos-13
    strategy:
      matrix:
        target:
          - x86_64-apple-darwin
          - aarch64-apple-darwin
          - aarch64-apple-ios
          - x86_64-apple-ios
    runs-on: macos-13
    steps:
      - uses: actions/checkout@v4
      - uses: moonrepo/setup-rust@v1
        with:
          channel: ${{ env.MSRV_UPLOAD }}
          bins: cross
          targets: ${{ matrix.target }}
      - run: |
          set -eux
          uname -a
          rustup show
          cross --version
          cross check --lib --bins --target ${{ matrix.target }}

The built-in advice from the error message above is pass CLI argument --locked.

Try re-running cargo install with --locked

Can you allow passing --locked to the cargo install command invoked by the with statement?

Unable to install cargo-binstall

Trying to use the action on host where rustup is missing leads to this error:

Run moonrepo/setup-rust@v1
  with:
    inherit-toolchain: true
    cache-target: release
    cache: true
  env:
    RUSTFLAGS: -D warnings --cfg tokio_unstable
Setting cargo environment variables
Adding ~/.cargo/bin to PATH
Detecting toolchain
Loading rust-toolchain.toml or rust-toolchain file
Inheriting toolchain settings from inputs
Installing toolchain with rustup
/home/ubuntu/.cargo/bin/rustup toolchain install nightly-[2](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:2)02[3](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:3)-06-1[4](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:4) --profile minimal --target x86_64-unknown-linux-gnu --target aarch64-unknown-linux-gnu --component rustfmt --component clippy --no-self-update
info: syncing channel updates for 'nightly-2023-06-14-x86_64-unknown-linux-gnu'
info: latest update on 2023-06-14, rust version 1.72.0-nightly (371994e0d 2023-06-13)
info: component 'clippy' for target 'x86_64-unknown-linux-gnu' is up to date
info: component 'rust-std' for target 'aarch64-unknown-linux-gnu' is up to date
info: component 'rust-std' for target 'x86_64-unknown-linux-gnu' is up to date
info: component 'rustfmt' for target 'x86_64-unknown-linux-gnu' is up to date

  nightly-2023-06-14-x86_64-unknown-linux-gnu unchanged - rustc 1.72.0-nightly (371994e0d 2023-06-13)

/home/ubuntu/.cargo/bin/rustup default nightly-2023-06-14
info: using existing install for 'nightly-2023-06-14-x86_64-unknown-linux-gnu'
info: default toolchain set to 'nightly-2023-06-14-x86_64-unknown-linux-gnu'

  nightly-2023-06-14-x86_64-unknown-linux-gnu unchanged - rustc 1.72.0-nightly (371994e0d 2023-06-13)

info: note that the toolchain 'nightly-2023-06-14-x86_64-unknown-linux-gnu' is currently in use (overridden by '/opt/actions-runner/_work/nox/nox/rust-toolchain.toml')
Logging installed toolchain versions
/home/ubuntu/.cargo/bin/rustc +nightly-2023-06-14 --version --verbose
rustc 1.72.0-nightly (371994e0d 2023-06-13)
binary: rustc
commit-hash: 371994e0d8380600ddda78ca1be937c7fb179b49
commit-date: 2023-06-13
host: x86_64-unknown-linux-gnu
release: 1.72.0-nightly
LLVM version: 16.0.[5](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:5)
Installing additional binaries
cargo-binstall does not exist, attempting to install
Error: Cannot find module './133.index.js'
Require stack:
- /opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js
node:internal/modules/cjs/loader:933
  const err = new Error(message);
              ^

Error: Cannot find module './133.index.js'
Require stack:
- /opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.__nccwpck_require__.f.require (/opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js:[6](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:6)3801:28)
    at /opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js:63[7](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:7)36:40
    at Array.reduce (<anonymous>)
    at Function.__nccwpck_require__.e (/opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js:63735:67)
    at downloadAndInstallBinstall (/opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js:63077:5[8](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:9))
    at installBins (/opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js:63[12](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:13)2:[15](https://github.com/fluencelabs/nox/actions/runs/5738400881/job/15551917509?pr=1741#step:3:16)) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/opt/actions-runner/_work/_actions/moonrepo/setup-rust/v1/dist/index.js'
  ]
}

cache rustup binary

setup-rust action takes around 1min even though it is cached. Most likely the rustup binary and few other items are not cached.

setup-rust/src/cargo.ts

Lines 105 to 112 in deec13b

export function getCachePaths(): string[] {
return [
// ~/.cargo/registry
path.join(CARGO_HOME, 'registry'),
// /workspace/target/debug
path.join(WORKSPACE_ROOT, 'target', getCacheTarget()),
];
}

Setting cargo environment variables
Adding ~/.cargo/bin to PATH
rustup does not exist, attempting to install
Downloaded installation script to /tmp/rustup-init
[command]/tmp/rustup-init --default-toolchain none -y
info: downloading installer
info: profile set to 'default'
info: default host triple is x86_64-unknown-linux-gnu
info: skipping toolchain installation

Fails to cache on macos-13

The post step of this github action fails on macos-13 with:

Error: Path Validation Error: Path(s) specified in the action for caching do(es) not exist, hence no cache is being saved.

No idea what command line is being tried, it is printed in the other 2 successful cases but not in this case.

Workflow (clipped):

# ...
jobs:
  build-linux:
    name: Linux
    runs-on: ubuntu-latest
    steps:
    # ...
    - name: Install dependencies (rust)
      uses: moonrepo/setup-rust@v1
      with:
        targets: x86_64-unknown-linux-gnu
        components: clippy,rustfmt
    # ...

  build-macos:
    name: MacOS
    runs-on: macos-13 # macos-latest/macos-14 has an ARM cpu (Apple M1) but we want an AMD64 cpu
    steps:
    # ...
    - name: Install dependencies (rust)
      uses: moonrepo/setup-rust@v1
      with:
        targets: x86_64-apple-darwin
        components: clippy,rustfmt
    # ...

  build-cygwin:
    name: Cygwin
    runs-on: windows-latest
    steps:
    # ...
    - name: Install dependencies (rust)
      uses: moonrepo/setup-rust@v1
      with:
        targets: x86_64-pc-windows-gnu
        components: clippy,rustfmt
    # ...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.