Recently I started having an issue with composer update or composer install on macOS. I ignored it for a bit and used Linux instead, but had to fix it eventually. When running composer update -n, the error was:

  - Installing drupal/drush_cmi_tools (dev-8.x-2.x 69b417d): Cloning 69b417ddbf from cache

  Failed to execute git clone --no-checkout '/Users/simon/.composer/cache/vcs/' 'web/modules/contrib/drush_cmi_tools' --dissociate --  
  reference '/Users/simon/.composer/cache/vcs/' && cd 'web/modules/contrib/drush_cmi_tools' && git remote set-url origin 'https://git' && git remote add composer ''                                                       

  fatal: destination path 'web/modules/contrib/drush_cmi_tools' already exists and is not an empty directory.

But that directory wouldn't be there. Even weirder, manually copying and pasting the commands showed they worked fine.

For another project, it failed when cloning a different repository, complaining that the git clone failed and the path didn't exist.

This different behaviour between common repositories and unique ones was just information enough to make me try commenting the CDPATH variable in my .zshrc (which is copied from my Linux machine).

This fixed the issue!

So best guess is that when composer calls a shell on macOS that it still takes the CDPATH into account, and that leads it to try incorrect directories, where on Linux it doesn't do that.

Using CDPATH is not that common, so its likely nobody else will notice.