Nightwatch pipeline started to fail if the module contains Nightwatch commands
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3490191. --> Reported by: [murz](https://www.drupal.org/user/157092) Related to !297 >>> <h3 id="summary-problem-motivation">Problem/Motivation</h3> <p>After fixing the issue <span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/gitlab_templates/issues/3475974" title="Status: Closed (fixed)">#3475974: Preserve environment variables when running testing jobs</a></span> I faced a new issue - any Drupal module that contains Nightwatch commands started to fail with an error:</p> <pre>&nbsp;&nbsp;&nbsp; Error&nbsp;&nbsp; unhandledRejection: Error while loading the API commands: the command .myNightwatchCommand() is already defined.<br>TypeError: Error while loading the API commands: the command .myNightwatchCommand() is already defined.<br>&nbsp;&nbsp;&nbsp; at CommandLoader.validateMethod (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/_loaders/_command-loader.js:54:19)<br>&nbsp;&nbsp;&nbsp; at CommandLoader.define (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/_loaders/_base-loader.js:330:10)<br>&nbsp;&nbsp;&nbsp; at ApiLoader.addCommandDefinitionAsync (/builds/project/test_helpers/web/core/node_modules/nightwatch/lib/api/index.js:367:42)<br>&nbsp;&nbsp;&nbsp; at async Promise.all (index 29)</pre><h4 id="summary-steps-reproduce">Steps to reproduce</h4> <p>A job reproducing the issue: <a href="https://git.drupalcode.org/project/test_helpers/-/jobs/3506124">https://git.drupalcode.org/project/test_helpers/-/jobs/3506124</a></p> <h3 id="summary-proposed-resolution">Proposed resolution</h3> <p>This happens because before fixing the "missing environment variables" issue we have the empty value of the environment variable <code>DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY</code>, but now it got the "right" value:</p> <pre>DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY=../</pre><p>This leads to scanning the module root directory for available Nightwatch tests by a glob pattern:</p> <pre>globSync('**/tests/**/Nightwatch/**/*.js', {</pre><p> - see <a href="https://git.drupalcode.org/project/drupal/-/blob/10.3.x/core/tests/Drupal/Nightwatch/nightwatch.conf.js?ref_type=heads#L16">https://git.drupalcode.org/project/drupal/-/blob/10.3.x/core/tests/Drupal/Nightwatch/nightwatch.conf.js?ref_type=heads#L16</a></p> <p>But, because we use symlinks to make a "copy" of the module in the <code>web/modules/custom/MODULENAME</code> directory, this leads to finding the Nightwatch commands directory with the same commands twice.</p> <p>The quick solution is to set the <code>DRUPAL_NIGHTWATCH_SEARCH_DIRECTORY</code> to the empty string, but it looks not so right.</p> <p>So, any ideas on how to fix this right?</p> <h3 id="summary-remaining-tasks">Remaining tasks</h3> <h3 id="summary-ui-changes">User interface changes</h3> <h3 id="summary-api-changes">API changes</h3> <h3 id="summary-data-model-changes">Data model changes</h3> > Related issue: [Issue #3389763](https://www.drupal.org/node/3389763)
issue