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

  [RuntimeException]
  Failed to execute git clone --no-checkout '/Users/simon/.composer/cache/vcs/https---github.com-previousnext-drush-cmi-tools.git/' 'web/modules/contrib/drush_cmi_tools' --dissociate --  
  reference '/Users/simon/.composer/cache/vcs/https---github.com-previousnext-drush-cmi-tools.git/' && cd 'web/modules/contrib/drush_cmi_tools' && git remote set-url origin 'https://git  
  hub.com/previousnext/drush_cmi_tools.git.git' && git remote add composer 'https://github.com/previousnext/drush_cmi_tools.git.git'                                                       

  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.