name: Address Sanitizer

env:
  DEBUG: 'napi:*'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - main
  pull_request:

jobs:
  build_and_test:
    name: nightly - ubuntu - node@16
    runs-on: ubuntu-20.04

    steps:
      - uses: actions/checkout@v3

      - name: Setup node
        uses: actions/setup-node@v3
        with:
          node-version: 18
          cache: 'yarn'

      - name: Install
        uses: dtolnay/rust-toolchain@stable
        with:
          toolchain: nightly
          components: rust-src

      - name: Cache cargo
        uses: actions/cache@v3
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
          key: nightly-ubuntu-node@18-cargo-cache

      - name: 'Install dependencies'
        run: yarn install --immutable --mode=skip-build

      - name: Unit tests with address sanitizer
        run: |
          yarn workspace @examples/napi build -- -Z build-std
          yarn workspace @examples/compat-mode build -- -Z build-std
          LD_PRELOAD=/usr/lib/gcc/x86_64-linux-gnu/9/libasan.so yarn test
        env:
          RUST_TARGET: x86_64-unknown-linux-gnu
          RUST_BACKTRACE: 1
          RUSTFLAGS: -Z sanitizer=address
          ASAN_OPTIONS: detect_leaks=0

      - name: Clear the cargo caches
        run: |
          cargo install cargo-cache --no-default-features --features ci-autoclean
          cargo-cache