Nightwatch pipeline started to fail if the module contains Nightwatch commands
Problem/Motivation
After fixing the issue #3475974: Preserve environment variables when running testing jobs I faced a new issue - any Drupal module that contains Nightwatch commands started to fail with an error:
Error unhandledRejection: Error while loading the API commands: the command .myNightwatchCommand() is already defined.
TypeError: Error while loading the API commands: the command .myNightwatchCommand() is already defined.
at CommandLoader.validateMethod (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/_loaders/_command-loader.js:54:19)
at CommandLoader.define (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:330:10)
at ApiLoader.addCommandDefinitionAsync (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/index.js:367:42)
at async Promise.all (index 29)
Steps to reproduce
A job reproducing the issue: https://git.drupalcode.org/project/test_helpers/-/jobs/3506124
Proposed resolution
This happens because before fixing the "missing environment variables" issue we have the empty value of the environment variable DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY, but now it got the "right" value:
DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY=../
This leads to scanning the module root directory for available Nightwatch tests by a glob pattern:
globSync('**/tests/**/Nightwatch/**/*.js', {
But, because we use symlinks to make a "copy" of the module in the web/modules/custom/MODULENAME directory, this leads to finding the Nightwatch commands directory with the same commands twice.
The quick solution is to set the DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY to the empty string, but it looks not so right.
So, any ideas on how to fix this right?