Rspec errors with external_pipeline

I’m working on upgrading my existing Middleman 3 site to Middleman 4. In particular, I’m using Gulp with the new external_pipeline feature instead of sprockets. I also have handful of Rspec examples, and while these are now passing, I’m getting loads of tilt warnings and at the end, some type of js exception.

When I run my whole test suite, this is the output I get:

~/Sites/cmoproject.org c965883 [mm4-gulp-upgrade]
% clear; bin/rspec spec
== Preferring use of LibSass
== Executing: `./node_modules/gulp/bin/gulp.js default`
WARN: tilt autoloading 'less' in a non thread-safe way; explicit require 'less' suggested.
WARN: tilt autoloading 'csv' in a non thread-safe way; explicit require 'csv' suggested.
WARN: tilt autoloading 'builder' in a non thread-safe way; explicit require 'builder' suggested.
WARN: tilt autoloading 'markaby' in a non thread-safe way; explicit require 'markaby' suggested.
WARN: tilt autoloading 'liquid' in a non thread-safe way; explicit require 'liquid' suggested.
WARN: tilt autoloading 'radius' in a non thread-safe way; explicit require 'radius' suggested.
WARN: tilt autoloading 'redcarpet' in a non thread-safe way; explicit require 'redcarpet' suggested.
WARN: tilt autoloading 'rdiscount' in a non thread-safe way; explicit require 'rdiscount' suggested.
WARN: tilt autoloading 'rdiscount' in a non thread-safe way; explicit require 'rdiscount' suggested.
WARN: tilt autoloading 'bluecloth' in a non thread-safe way; explicit require 'bluecloth' suggested.
WARN: tilt autoloading 'kramdown' in a non thread-safe way; explicit require 'kramdown' suggested.
WARN: tilt autoloading 'redcloth' in a non thread-safe way; explicit require 'redcloth' suggested.
WARN: tilt autoloading 'rdoc' in a non thread-safe way; explicit require 'rdoc' suggested.
WARN: tilt autoloading 'rdoc/markup' in a non thread-safe way; explicit require 'rdoc/markup' suggested.
WARN: tilt autoloading 'rdoc/markup/to_html' in a non thread-safe way; explicit require 'rdoc/markup/to_html' suggested.
WARN: tilt autoloading 'wikicloth' in a non thread-safe way; explicit require 'wikicloth' suggested.
WARN: tilt autoloading 'creole' in a non thread-safe way; explicit require 'creole' suggested.
WARN: tilt autoloading 'creole' in a non thread-safe way; explicit require 'creole' suggested.
WARN: tilt autoloading 'wikicloth' in a non thread-safe way; explicit require 'wikicloth' suggested.
WARN: tilt autoloading 'wikicloth' in a non thread-safe way; explicit require 'wikicloth' suggested.
WARN: tilt autoloading 'yajl' in a non thread-safe way; explicit require 'yajl' suggested.
WARN: tilt autoloading 'asciidoctor' in a non thread-safe way; explicit require 'asciidoctor' suggested.
WARN: tilt autoloading 'asciidoctor' in a non thread-safe way; explicit require 'asciidoctor' suggested.
WARN: tilt autoloading 'asciidoctor' in a non thread-safe way; explicit require 'asciidoctor' suggested.
== External: [17:25:09] Using gulpfile ~/Sites/cmoproject.org/gulpfile.js
== External: [17:25:09] Starting 'styles'...
== External: [17:25:09] Starting 'scripts'...
== External: [17:25:09] Starting 'home-scripts'...
== LiveReload accepting connections from ws://192.168.1.112:35729
........

Finished in 0.16507 seconds (files took 2.05 seconds to load)
8 examples, 0 failures

~/Sites/cmoproject.org c965883 [mm4-gulp-upgrade]
% events.js:154                                                                              ruby 2.3.0p0
      throw er; // Unhandled 'error' event
      ^

Error: write EPIPE
    at exports._errnoException (util.js:890:11)
~/Sites/cmoproject.org c965883 [mm4-gulp-upgrade]
%                                                                                                                                                                                                      

If I comment out the external_pipeline section in config.rb, and then run my tests, I don’t get these errors. So it appears the problem is caused by Gulp being run at the same time that Rspec/Capybara are running the tests. Is there a way to prevent this from happening?

All the code is on this feature branch: https://github.com/joshukraine/cmoproject.org/tree/mm4-gulp-upgrade

Thanks in advance for any assistance.

Found a solution. Using Middleman’s environments feature, it’s possible to limit when the external pipeline runs. My implementation can be seen in this commit: https://github.com/joshukraine/cmoproject.org/commit/e7e11633d289ce1a2495d0078f0e42c3c70e94e9