on: push

name: R-CMD-check-with-sanitizers

jobs:
  pre-condition:
    name: Check hash of src files
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - id: cache_hit
        uses: actions/cache@v4
        with:
          path: |
            src
          key: san-${{ hashFiles('src/**') }}
    outputs:
      run_check: ${{ steps.cache_hit.outputs.cache-hit }}
  R-CMD-check:
    needs: pre-condition
    if: needs.pre-condition.outputs.run_check != 'true'

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: ubuntu-latest, img: 'r-devel-san'}
          - {os: ubuntu-latest, img: 'r-devel-ubsan-clang'}

    runs-on: ${{ matrix.config.os }}
    container: rocker/${{ matrix.config.img }}:latest
    name: ${{ matrix.config.os }} (${{ matrix.config.img }})

    env:
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
      R_KEEP_PKG_SOURCE: yes

    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        with:
          path: |
            /usr/local/lib/R/site-library
          key: ${{ matrix.config.img }}

      - name: Install dependencies
        env:
           ASAN_OPTIONS: "detect_odr_violation=0"
        shell: Rscriptdevel {0}
        run: |
          install.packages("remotes")
          library("remotes")
          install_cran(c("bigmemory"))
          install_cran(c("Matrix", "codetools", "cluster", "lattice", "class"), force = TRUE)
          update(dev_package_deps(dependencies = TRUE))

      - name: Check with sanitizers
        env:
          NOT_CRAN: false
          ASAN_OPTIONS: "detect_leaks=0:detect_odr_violation=0"
        run: |
          Rdevel CMD build --no-build-vignettes --no-manual .
          Rdevel --vanilla CMD check *.tar.gz --as-cran --no-manual --ignore-vignettes
        continue-on-error: true

      - name: Show testthat output
        if: always()
        run: find *.Rcheck -name 'testthat.Rout*' -exec cat '{}' \; || true
        shell: bash

      - name: Upload check results
        uses: actions/upload-artifact@main
        with:
          name: ${{ matrix.config.img }}-results
          path: |
            *.Rcheck/*.out
            *.Rcheck/*.log
