name: Test with compiler sanitizers (Linux)

on:
  pull_request:
    branches:
      - main
      - maintenance/**

defaults:
  run:
    shell: bash

env:
  PYTHON_VERSION: 3.11

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
  cancel-in-progress: true

permissions:
  contents: read # to fetch code (actions/checkout)

jobs:
  clang_sanitizers:
    # To enable this workflow on a fork, comment out:
    if: github.repository == 'numpy/numpy'
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
      with:
        submodules: recursive
        fetch-tags: true
    - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
      with:
        python-version: ${{ env.PYTHON_VERSION }}
    - name: Install dependencies
      run: |
        sudo apt update
        sudo apt install -y llvm libstdc++-12-dev
        pip install -r requirements/build_requirements.txt
        pip install -r requirements/ci_requirements.txt
    - name: Build
      shell: 'script -q -e -c "bash --noprofile --norc -eo pipefail {0}"'
      env:
        TERM: xterm-256color
        PKG_CONFIG_PATH: ${{ github.workspace }}/.openblas
      run:
        CC=clang CXX=clang++ spin build --with-scipy-openblas=32 -- -Db_sanitize=address,undefined
    - name: Test
      shell: 'script -q -e -c "bash --noprofile --norc -eo pipefail {0}"'
      env:
        TERM: xterm-256color
      run: |
        pip install pytest pytest-xdist hypothesis typing_extensions
        ASAN_OPTIONS=detect_leaks=0:symbolize=1:strict_init_order=true:allocator_may_return_null=1:halt_on_error=1 \
        UBSAN_OPTIONS=halt_on_error=0 \
        LD_PRELOAD=$(clang --print-file-name=libclang_rt.asan-x86_64.so) \
        python -m spin test -- -v -s
