How to call fastlane scan with the --clean parameter using CircleCI?

We have an Xcode 11 iOS project and we added some swift packages to the project as dependencies. This is causing some issue with CircleCI (builds fine locally). We don’t have a fastlane configuration file, just the .circleci/config.yml.

First issue is Xcode 11 issue that it has to use https dependency url scheme. This required overriding the forced switch to ssh+git that circleci does via:

      - run:
          name: Override CircleCI GitHub URL rewriting
          command: git config --global --unset url."ssh://git@github.com".insteadOf

and then changing all our package dependencies to use https urls (pain, but it works). However, then there are issues with the swift package cache (possibly from the previous failed attempts).

I had hoped to add --clean to the fastlane command, as in:

      - run:
          name: Build and run tests
          command: fastlane scan --clean
          environment:
            SCAN_DEVICE: iPhone 11 (13.0)
            SCAN_SCHEME:  CircleCIScheme

xcodebuild: error: Could not resolve package dependencies:
An unknown error occurred. ‘/Users/distiller/Library/Developer/Xcode/DerivedData/TestProject-bivykjtderujuveypcdernfqexxy/SourcePackages/repositories/CwlCatchException-463e8527’ exists and is not an empty directory (-4)

I get the same thing if I try it without the dashes as command: fastlane scan clean

I can get the build to work (at least most of the time) if instead of adding --clean to the fastlane scan command I instead add an xcodebuild and clean run command first:

      - run: 
          name: xcode clean (fixes stale package cache)
          command: xcodebuild -scheme CircleCIScheme -project ./TestProject.xcodeproj clean

seems to work, doing this first:

#!/bin/bash -eo pipefail
xcodebuild -scheme CircleCIScheme -project ./TestProject.xcodeproj clean
^D^DResolve Package Graph

Fetching https://github.com/redactedorg/CwlPreconditionTesting.git

Fetching https://github.com/redactedorg/CwlCatchException.git

Cloning https://github.com/redactedorg/CwlCatchException.git

Checking out https://github.com/redactedorg/CwlCatchException.git at 2.0.0-beta.1

Cloning https://github.com/redactedorg/CwlPreconditionTesting.git

Checking out https://github.com/redactedorg/CwlPreconditionTesting.git at 2.0.0-beta.3

Resolved source packages:
CwlCatchException: https://github.com/redactedorg/CwlCatchException.git @ 2.0.0-beta.1
CwlPreconditionTesting: https://github.com/redactedorg/CwlPreconditionTesting.git @ 2.0.0-beta.3

note: Using new build system

** CLEAN SUCCEEDED **

but that seems wrong. It seems like I should be able to pass clean to fastlane and have it do the clean and package stuff automatically.

Suggestions? TIA