# Generated from xtask::workflows::run_tests
# Rebuild with `cargo xtask workflows`.
name: run_tests
env:
  CARGO_TERM_COLOR: always
  RUST_BACKTRACE: '1'
  CARGO_INCREMENTAL: '0'
on:
  pull_request:
    branches:
    - '**'
  push:
    branches:
    - main
    - v[0-9]+.[0-9]+.x
jobs:
  orchestrate:
    if: (github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
        fetch-depth: ${{ github.ref == 'refs/heads/main' && 2 || 350 }}
    - id: filter
      name: filter
      run: |
        if [ -z "$GITHUB_BASE_REF" ]; then
          echo "Not in a PR context (i.e., push to main/stable/preview)"
          COMPARE_REV="$(git rev-parse HEAD~1)"
        else
          echo "In a PR context comparing to pull_request.base.ref"
          git fetch origin "$GITHUB_BASE_REF" --depth=350
          COMPARE_REV="$(git merge-base "origin/${GITHUB_BASE_REF}" HEAD)"
        fi
        CHANGED_FILES="$(git diff --name-only "$COMPARE_REV" ${{ github.sha }})"

        check_pattern() {
          local output_name="$1"
          local pattern="$2"
          local grep_arg="$3"

          echo "$CHANGED_FILES" | grep "$grep_arg" "$pattern" && \
            echo "${output_name}=true" >> "$GITHUB_OUTPUT" || \
            echo "${output_name}=false" >> "$GITHUB_OUTPUT"
        }

        check_pattern "run_action_checks" '^\.github/(workflows/|actions/|actionlint.yml)|tooling/xtask|script/' -qP
        check_pattern "run_docs" '^(docs/|crates/.*\.rs)' -qP
        check_pattern "run_licenses" '^(Cargo.lock|script/.*licenses)' -qP
        check_pattern "run_nix" '^(nix/|flake\.|Cargo\.|rust-toolchain.toml|\.cargo/config.toml)' -qP
        check_pattern "run_tests" '^(docs/|script/update_top_ranking_issues/|\.github/(ISSUE_TEMPLATE|workflows/(?!run_tests)))' -qvP
      shell: bash -euxo pipefail {0}
    outputs:
      run_action_checks: ${{ steps.filter.outputs.run_action_checks }}
      run_docs: ${{ steps.filter.outputs.run_docs }}
      run_licenses: ${{ steps.filter.outputs.run_licenses }}
      run_nix: ${{ steps.filter.outputs.run_nix }}
      run_tests: ${{ steps.filter.outputs.run_tests }}
  check_style:
    if: (github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions')
    runs-on: namespace-profile-4x8-ubuntu-2204
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: steps::setup_pnpm
      uses: pnpm/action-setup@fe02b34f77f8bc703788d5817da081398fad5dd2
      with:
        version: '9'
    - name: steps::prettier
      run: ./script/prettier
      shell: bash -euxo pipefail {0}
    - name: steps::cargo_fmt
      run: cargo fmt --all -- --check
      shell: bash -euxo pipefail {0}
    - name: ./script/check-todos
      run: ./script/check-todos
      shell: bash -euxo pipefail {0}
    - name: ./script/check-keymaps
      run: ./script/check-keymaps
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_style::check_for_typos
      uses: crate-ci/typos@2d0ce569feab1f8752f1dde43cc2f2aa53236e06
      with:
        config: ./typos.toml
    timeout-minutes: 60
  run_tests_windows:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: self-32vcpu-windows-2022
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::setup_cargo_config
      run: |
        New-Item -ItemType Directory -Path "./../.cargo" -Force
        Copy-Item -Path "./.cargo/ci-config.toml" -Destination "./../.cargo/config.toml"
      shell: pwsh
    - name: steps::setup_node
      uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
      with:
        node-version: '20'
    - name: steps::clippy
      run: ./script/clippy.ps1
      shell: pwsh
    - name: steps::clear_target_dir_if_large
      run: ./script/clear-target-dir-if-larger-than.ps1 250
      shell: pwsh
    - name: steps::cargo_nextest
      run: cargo nextest run --workspace --no-fail-fast
      shell: pwsh
    - name: steps::cleanup_cargo_config
      if: always()
      run: |
        Remove-Item -Recurse -Path "./../.cargo" -Force -ErrorAction SilentlyContinue
      shell: pwsh
    timeout-minutes: 60
  run_tests_linux:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: namespace-profile-16x32-ubuntu-2204
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
      shell: bash -euxo pipefail {0}
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: steps::setup_linux
      run: ./script/linux
      shell: bash -euxo pipefail {0}
    - name: steps::install_mold
      run: ./script/install-mold
      shell: bash -euxo pipefail {0}
    - name: steps::download_wasi_sdk
      run: ./script/download-wasi-sdk
      shell: bash -euxo pipefail {0}
    - name: steps::setup_node
      uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
      with:
        node-version: '20'
    - name: steps::clippy
      run: ./script/clippy
      shell: bash -euxo pipefail {0}
    - name: steps::cargo_install_nextest
      uses: taiki-e/install-action@nextest
    - name: steps::clear_target_dir_if_large
      run: ./script/clear-target-dir-if-larger-than 250
      shell: bash -euxo pipefail {0}
    - name: steps::cargo_nextest
      run: cargo nextest run --workspace --no-fail-fast
      shell: bash -euxo pipefail {0}
    - name: steps::cleanup_cargo_config
      if: always()
      run: |
        rm -rf ./../.cargo
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
  run_tests_mac:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: self-mini-macos
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
      shell: bash -euxo pipefail {0}
    - name: steps::setup_node
      uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020
      with:
        node-version: '20'
    - name: steps::clippy
      run: ./script/clippy
      shell: bash -euxo pipefail {0}
    - name: steps::clear_target_dir_if_large
      run: ./script/clear-target-dir-if-larger-than 300
      shell: bash -euxo pipefail {0}
    - name: steps::cargo_nextest
      run: cargo nextest run --workspace --no-fail-fast
      shell: bash -euxo pipefail {0}
    - name: steps::cleanup_cargo_config
      if: always()
      run: |
        rm -rf ./../.cargo
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
  doctests:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: namespace-profile-16x32-ubuntu-2204
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: steps::setup_linux
      run: ./script/linux
      shell: bash -euxo pipefail {0}
    - name: steps::install_mold
      run: ./script/install-mold
      shell: bash -euxo pipefail {0}
    - name: steps::download_wasi_sdk
      run: ./script/download-wasi-sdk
      shell: bash -euxo pipefail {0}
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
      shell: bash -euxo pipefail {0}
    - id: run_doctests
      name: run_tests::doctests::run_doctests
      run: |
        cargo test --workspace --doc --no-fail-fast
      shell: bash -euxo pipefail {0}
    - name: steps::cleanup_cargo_config
      if: always()
      run: |
        rm -rf ./../.cargo
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
  check_workspace_binaries:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: namespace-profile-8x16-ubuntu-2204
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
      shell: bash -euxo pipefail {0}
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: steps::setup_linux
      run: ./script/linux
      shell: bash -euxo pipefail {0}
    - name: steps::install_mold
      run: ./script/install-mold
      shell: bash -euxo pipefail {0}
    - name: steps::download_wasi_sdk
      run: ./script/download-wasi-sdk
      shell: bash -euxo pipefail {0}
    - name: cargo build -p collab
      run: cargo build -p collab
      shell: bash -euxo pipefail {0}
    - name: cargo build --workspace --bins --examples
      run: cargo build --workspace --bins --examples
      shell: bash -euxo pipefail {0}
    - name: steps::cleanup_cargo_config
      if: always()
      run: |
        rm -rf ./../.cargo
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
  check_dependencies:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: run_tests::check_dependencies::install_cargo_machete
      uses: clechasseur/rs-cargo@8435b10f6e71c2e3d4d3b7573003a8ce4bfc6386
      with:
        command: install
        args: cargo-machete@0.7.0
    - name: run_tests::check_dependencies::run_cargo_machete
      uses: clechasseur/rs-cargo@8435b10f6e71c2e3d4d3b7573003a8ce4bfc6386
      with:
        command: machete
    - name: run_tests::check_dependencies::check_cargo_lock
      run: cargo update --locked --workspace
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_dependencies::check_vulnerable_dependencies
      if: github.event_name == 'pull_request'
      uses: actions/dependency-review-action@67d4f4bd7a9b17a0db54d2a7519187c65e339de8
      with:
        license-check: false
    timeout-minutes: 60
  check_docs:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_docs == 'true'
    runs-on: namespace-profile-8x16-ubuntu-2204
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::setup_cargo_config
      run: |
        mkdir -p ./../.cargo
        cp ./.cargo/ci-config.toml ./../.cargo/config.toml
      shell: bash -euxo pipefail {0}
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: run_tests::check_docs::lychee_link_check
      uses: lycheeverse/lychee-action@82202e5e9c2f4ef1a55a3d02563e1cb6041e5332
      with:
        args: --no-progress --exclude '^http' './docs/src/**/*'
        fail: true
        jobSummary: false
    - name: steps::setup_linux
      run: ./script/linux
      shell: bash -euxo pipefail {0}
    - name: steps::install_mold
      run: ./script/install-mold
      shell: bash -euxo pipefail {0}
    - name: steps::download_wasi_sdk
      run: ./script/download-wasi-sdk
      shell: bash -euxo pipefail {0}
    - name: ./script/generate-action-metadata
      run: ./script/generate-action-metadata
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_docs::install_mdbook
      uses: peaceiris/actions-mdbook@ee69d230fe19748b7abf22df32acaa93833fad08
      with:
        mdbook-version: 0.4.37
    - name: run_tests::check_docs::build_docs
      run: |
        mkdir -p target/deploy
        mdbook build ./docs --dest-dir=../target/deploy/docs/
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_docs::lychee_link_check
      uses: lycheeverse/lychee-action@82202e5e9c2f4ef1a55a3d02563e1cb6041e5332
      with:
        args: --no-progress --exclude '^http' 'target/deploy/docs'
        fail: true
        jobSummary: false
    timeout-minutes: 60
  check_licenses:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_licenses == 'true'
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: steps::cache_rust_dependencies_namespace
      uses: namespacelabs/nscloud-cache-action@v1
      with:
        cache: rust
    - name: ./script/check-licenses
      run: ./script/check-licenses
      shell: bash -euxo pipefail {0}
    - name: ./script/generate-licenses
      run: ./script/generate-licenses
      shell: bash -euxo pipefail {0}
  check_scripts:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_action_checks == 'true'
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: run_tests::check_scripts::run_shellcheck
      run: ./script/shellcheck-scripts error
      shell: bash -euxo pipefail {0}
    - id: get_actionlint
      name: run_tests::check_scripts::download_actionlint
      run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash)
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_scripts::run_actionlint
      run: |
        ${{ steps.get_actionlint.outputs.executable }} -color
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_scripts::check_xtask_workflows
      run: |
        cargo xtask workflows
        if ! git diff --exit-code .github; then
          echo "Error: .github directory has uncommitted changes after running 'cargo xtask workflows'"
          echo "Please run 'cargo xtask workflows' locally and commit the changes"
          exit 1
        fi
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
  build_nix_linux_x86_64:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_nix == 'true'
    runs-on: namespace-profile-32x64-ubuntu-2004
    env:
      ZED_CLIENT_CHECKSUM_SEED: ${{ secrets.ZED_CLIENT_CHECKSUM_SEED }}
      ZED_MINIDUMP_ENDPOINT: ${{ secrets.ZED_SENTRY_MINIDUMP_ENDPOINT }}
      ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON: ${{ secrets.ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON }}
      GIT_LFS_SKIP_SMUDGE: '1'
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: nix_build::build_nix::install_nix
      uses: cachix/install-nix-action@02a151ada4993995686f9ed4f1be7cfbb229e56f
      with:
        github_access_token: ${{ secrets.GITHUB_TOKEN }}
    - name: nix_build::build_nix::cachix_action
      uses: cachix/cachix-action@0fc020193b5a1fa3ac4575aa3a7d3aa6a35435ad
      with:
        name: zed
        authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
        cachixArgs: -v
        pushFilter: -zed-editor-[0-9.]*-nightly
    - name: nix_build::build_nix::build
      run: nix build .#debug -L --accept-flake-config
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
    continue-on-error: true
  build_nix_mac_aarch64:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_nix == 'true'
    runs-on: self-mini-macos
    env:
      ZED_CLIENT_CHECKSUM_SEED: ${{ secrets.ZED_CLIENT_CHECKSUM_SEED }}
      ZED_MINIDUMP_ENDPOINT: ${{ secrets.ZED_SENTRY_MINIDUMP_ENDPOINT }}
      ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON: ${{ secrets.ZED_CLOUD_PROVIDER_ADDITIONAL_MODELS_JSON }}
      GIT_LFS_SKIP_SMUDGE: '1'
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        clean: false
    - name: nix_build::build_nix::set_path
      run: |
        echo "/nix/var/nix/profiles/default/bin" >> "$GITHUB_PATH"
        echo "/Users/administrator/.nix-profile/bin" >> "$GITHUB_PATH"
      shell: bash -euxo pipefail {0}
    - name: nix_build::build_nix::cachix_action
      uses: cachix/cachix-action@0fc020193b5a1fa3ac4575aa3a7d3aa6a35435ad
      with:
        name: zed
        authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
        cachixArgs: -v
        pushFilter: -zed-editor-[0-9.]*-nightly
    - name: nix_build::build_nix::build
      run: nix build .#debug -L --accept-flake-config
      shell: bash -euxo pipefail {0}
    - name: nix_build::build_nix::limit_store
      run: |-
        if [ "$(du -sm /nix/store | cut -f1)" -gt 50000 ]; then
            nix-collect-garbage -d || true
        fi
      shell: bash -euxo pipefail {0}
    timeout-minutes: 60
    continue-on-error: true
  check_postgres_and_protobuf_migrations:
    needs:
    - orchestrate
    if: needs.orchestrate.outputs.run_tests == 'true'
    runs-on: namespace-profile-16x32-ubuntu-2204
    env:
      GIT_AUTHOR_NAME: Protobuf Action
      GIT_AUTHOR_EMAIL: ci@zed.dev
      GIT_COMMITTER_NAME: Protobuf Action
      GIT_COMMITTER_EMAIL: ci@zed.dev
    steps:
    - name: steps::checkout_repo
      uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
      with:
        fetch-depth: 0
    - name: run_tests::check_postgres_and_protobuf_migrations::remove_untracked_files
      run: git clean -df
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_postgres_and_protobuf_migrations::ensure_fresh_merge
      run: |
        if [ -z "$GITHUB_BASE_REF" ];
        then
          echo "BUF_BASE_BRANCH=$(git merge-base origin/main HEAD)" >> "$GITHUB_ENV"
        else
          git checkout -B temp
          git merge -q "origin/$GITHUB_BASE_REF" -m "merge main into temp"
          echo "BUF_BASE_BRANCH=$GITHUB_BASE_REF" >> "$GITHUB_ENV"
        fi
      shell: bash -euxo pipefail {0}
    - name: run_tests::check_postgres_and_protobuf_migrations::bufbuild_setup_action
      uses: bufbuild/buf-setup-action@v1
      with:
        version: v1.29.0
        github_token: ${{ secrets.GITHUB_TOKEN }}
    - name: run_tests::check_postgres_and_protobuf_migrations::bufbuild_breaking_action
      uses: bufbuild/buf-breaking-action@v1
      with:
        input: crates/proto/proto/
        against: https://github.com/${GITHUB_REPOSITORY}.git#branch=${BUF_BASE_BRANCH},subdir=crates/proto/proto/
    timeout-minutes: 60
  tests_pass:
    needs:
    - orchestrate
    - check_style
    - run_tests_windows
    - run_tests_linux
    - run_tests_mac
    - doctests
    - check_workspace_binaries
    - check_dependencies
    - check_docs
    - check_licenses
    - check_scripts
    - build_nix_linux_x86_64
    - build_nix_mac_aarch64
    if: (github.repository_owner == 'zed-industries' || github.repository_owner == 'zed-extensions') && always()
    runs-on: namespace-profile-2x4-ubuntu-2404
    steps:
    - name: run_tests::tests_pass
      run: |
        set +x
        EXIT_CODE=0

        check_result() {
          echo "* $1: $2"
          if [[ "$2" != "skipped" && "$2" != "success" ]]; then EXIT_CODE=1; fi
        }

        check_result "orchestrate" "${{ needs.orchestrate.result }}"
        check_result "check_style" "${{ needs.check_style.result }}"
        check_result "run_tests_windows" "${{ needs.run_tests_windows.result }}"
        check_result "run_tests_linux" "${{ needs.run_tests_linux.result }}"
        check_result "run_tests_mac" "${{ needs.run_tests_mac.result }}"
        check_result "doctests" "${{ needs.doctests.result }}"
        check_result "check_workspace_binaries" "${{ needs.check_workspace_binaries.result }}"
        check_result "check_dependencies" "${{ needs.check_dependencies.result }}"
        check_result "check_docs" "${{ needs.check_docs.result }}"
        check_result "check_licenses" "${{ needs.check_licenses.result }}"
        check_result "check_scripts" "${{ needs.check_scripts.result }}"
        check_result "build_nix_linux_x86_64" "${{ needs.build_nix_linux_x86_64.result }}"
        check_result "build_nix_mac_aarch64" "${{ needs.build_nix_mac_aarch64.result }}"

        exit $EXIT_CODE
      shell: bash -euxo pipefail {0}
concurrency:
  group: ${{ github.workflow }}-${{ github.ref_name }}-${{ github.ref_name == 'main' && github.sha || 'anysha' }}
  cancel-in-progress: true
