Skip to content

Fully populate the cache when running vendor/bin/rector process --dry-run #7932

@dorrogeray

Description

@dorrogeray

Feature Request

When running rector in vendor/bin/rector process --dry-run, the cache should be fully populated, just like when running rector without the --dry-run flag. The use case is that in the CI, you would ideally want to run rector with --dry-run, since that will return non-zero exit code when there are any changes made by applying rector rules. But you would also like to be able to cache the rector cache, so following runs can be executed faster / consume less resources. Currently however, it seems that using --dry-run writes only very little to the cache (almost nothing) when compared to running without. Therefore, the caching in the CI does not work as expected..

Here is a workaround I came up with:

  1. Restore cache
  2. Run vendor/bin/rector process which fully populates the rector cache (if it is not already there from the cache restore)
  3. Save cache
  4. Clear any changes with git reset --hard
  5. Run vendor/bin/rector process --dry-run which will use the cache from step 2, so it will be fast, but it will also exit with non-zero code if any changes are detected, which is what will make the pipeline fail (which is what we want)

Diff

  rector:
    runs-on: ubuntu-latest-16-cores-64-gb-ram
    strategy:
      fail-fast: false
      matrix:
        php-version: [ '7.4' ]

    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Setup PHP
        uses: shivammathur/setup-php@v2
        with:
          ini-values: zend.assertions=1
          php-version: ${{ matrix.php-version }}
          extensions: mbstring, sockets, imap, gmp, memcached

      - name: Cache composer dependencies
        uses: actions/cache@v2
        with:
          path: vendor
          key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
          restore-keys: ${{ runner.os }}-composer-

      - name: Install dependencies
        run: |
          composer config github-oauth.github.com ${{ secrets.COMPOSER_TOKEN }}
          composer install --no-progress --prefer-dist --optimize-autoloader --ignore-platform-reqs

      - name: Copy phpstan.neon.example to phpstan.neon
        run: |
          cp phpstan.neon.example phpstan.neon

      - name: Restore rector cache
        id: cache-rector-restore
        uses: actions/cache/restore@v3
        with:
          path: ./temp/rector
          key: ${{ runner.os }}-rector-${{ hashFiles('**/composer.lock') }}-${{ github.ref }}
          restore-keys: |
            ${{ runner.os }}-rector-${{ hashFiles('**/composer.lock') }}-
            ${{ runner.os }}-rector-

      # Only real run will generate cache, which the --dry-run can then re-use
      - name: Rector generate cache
        run: vendor/bin/rector process

      - name: Clear changes generated while generating rector cache
        run: git reset --hard

      # Explicitly save rector cache
      - name: Cache rector save
        id: cache-rector-save
        uses: actions/cache/save@v3
        with:
          path: ./temp/rector
          # Adding github.run_id to force cache update https://github.com/actions/cache/blob/main/tips-and-workarounds.md#update-a-cache
          key: ${{ runner.os }}-rector-${{ hashFiles('**/composer.lock') }}-${{ github.ref }}-${{ github.run_id }}

      # Only --dry-run will fail when changes are detected
      - name: Rector
        run: vendor/bin/rector process --dry-run

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions