<h3 dir="auto">Is there an existing issue for this?</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the existing issues</li>
</ul>
<h3 dir="auto">This issue exists in the latest npm version</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I am using the latest npm</li>
</ul>
<h3 dir="auto">Current Behavior</h3>
<p dir="auto">We have a workspace configured 2 levels deep as follows:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content=".
+-- package.json
   +-- a
   |   `-- package.json
   `-- b
       `-- package.json
       +-- b1
       |   `-- package.json
       `-- b2
           `-- package.json"><pre class="notranslate"><code class="notranslate">.
+-- package.json
   +-- a
   |   `-- package.json
   `-- b
       `-- package.json
       +-- b1
       |   `-- package.json
       `-- b2
           `-- package.json
</code></pre></div>
<p dir="auto">The root package.json has a script <code class="notranslate">npm run build --workspaces</code> to build all workspaces.<br>
The package.json under <code class="notranslate">b</code> also has a script <code class="notranslate">npm run build --workspaces</code> to build b1 and b2.<br>
When we run the root script, npm goes into a infinite loop of running the scripts.</p>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">npm should not hang. it should run the scripts as expected: a, b1, b2</p>
<h3 dir="auto">Steps To Reproduce</h3>
<p dir="auto">Create a configuration with a workspace hierarchy that has multiple levels.<br>
For parent workspaces, define a script runs the children scripts with the --workspace option.</p>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>npm: 8.6.0</li>
<li>Node.js: v14.18.1</li>
<li>OS Name: MacOS</li>
<li>System Model Name: Macbook Pro</li>
</ul>
<h3 dir="auto">Is there an existing issue for this?</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I have searched the existing issues</li>
</ul>
<h3 dir="auto">This issue exists in the latest npm version</h3>
<ul class="contains-task-list">
<li class="task-list-item"><input type="checkbox" id="" disabled="" class="task-list-item-checkbox" checked=""> I am using the latest npm</li>
</ul>
<h3 dir="auto">Current Behavior</h3>
<p dir="auto">Given this project hierarchy:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="root
│   package.json
│
└───packages
    └───a
        │   package.json
        │
        └───b
                package.json"><pre class="notranslate"><code class="notranslate">root
│   package.json
│
└───packages
    └───a
        │   package.json
        │
        └───b
                package.json
</code></pre></div>
<p dir="auto">With root/package.json having:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="  &quot;workspaces&quot;: [
    &quot;packages/a&quot;,
    &quot;packages/a/b&quot;
  ]"><pre class="notranslate"><code class="notranslate">  "workspaces": [
    "packages/a",
    "packages/a/b"
  ]
</code></pre></div>
<p dir="auto">If I run <code class="notranslate">npm run myscript</code> with <code class="notranslate">packages/a</code> as my current directory, <code class="notranslate">npm</code> will try to run <code class="notranslate">myscript</code> in <code class="notranslate">package/a</code> and <code class="notranslate">packages/a/b</code>.</p>
<h3 dir="auto">Expected Behavior</h3>
<p dir="auto">Given the same project hierarchy I would expect:</p>
<ol dir="auto">
<li><g-emoji class="g-emoji" alias="x" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/274c.png">❌</g-emoji>  <code class="notranslate">npm run myscript</code> with the CWD being <code class="notranslate">packages/a</code> to run <strong>only</strong> <code class="notranslate">myscript</code> of <code class="notranslate">packages/a/package.json</code></li>
<li><g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji>  <code class="notranslate">npm run myscript --workspace=packages/a</code> to run both on <code class="notranslate">a</code> and <code class="notranslate">b</code> (<a href="https://docs.npmjs.com/cli/v8/commands/npm-run-script#workspace" rel="nofollow">npm doc</a>):</li>
</ol>
<blockquote>
<ul dir="auto">
<li>Path to a workspace directory</li>
<li>Path to a parent workspace directory (will result in selecting all workspaces within that folder)</li>
</ul>
</blockquote>
<ol start="3" dir="auto">
<li><g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji>   <code class="notranslate">npm run myscript --workspace=a</code> to run only on <code class="notranslate">a</code>. (<a href="https://docs.npmjs.com/cli/v8/commands/npm-run-script#workspace" rel="nofollow">npm doc</a>):</li>
</ol>
<blockquote>
<p dir="auto">Workspace names</p>
</blockquote>
<p dir="auto">Note, 2&amp;3 are working as expected on the latest version</p>
<h3 dir="auto">Steps To Reproduce</h3>
<ol dir="auto">
<li>Scaffold NPM project.</li>
</ol>
<div class="highlight highlight-source-shell notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="mkdir test-run-script
cd test-run-script
npm init -y
npm init -w packages/a -y
npm init -w packages/a/b -y"><pre class="notranslate">mkdir test-run-script
<span class="pl-c1">cd</span> test-run-script
npm init -y
npm init -w packages/a -y
npm init -w packages/a/b -y</pre></div>
<ol start="2" dir="auto">
<li>Add a dummy script in <code class="notranslate">packages/a/package.json</code> &amp; <code class="notranslate">packages/b/package.json</code>, e.g. `"myscript": "echo hello"</li>
<li>Ensure your cwd is <code class="notranslate">packages/a</code> and run <code class="notranslate">npm run myscript</code>.</li>
<li>Assert that hello is printed twice.</li>
</ol>
<h3 dir="auto">Environment</h3>
<ul dir="auto">
<li>npm: <code class="notranslate">8.5.0 - 8.5.3</code> (latest</li>
<li>Node.js: v16.14.0</li>
<li>OS Name: W10</li>
<li>System Model Name: N.A</li>
<li>npm config:</li>
</ul>
<div class="highlight highlight-source-ini notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="; &quot;builtin&quot; config from C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\npmrc

prefix = &quot;C:\\Users\\USER_NAME\\AppData\\Roaming\\npm&quot;

; &quot;user&quot; config from C:\Users\USER_NAME\.npmrc

//registry.npmjs.org/:_authToken = (protected)

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\Users\USER_NAME\Repos\runScriptWorkspace
; HOME = C:\Users\USER_NAME
; Run `npm config ls -l` to show all defaults."><pre class="notranslate"><span class="pl-c"><span class="pl-c">;</span> "builtin" config from C:\Users\USER_NAME\AppData\Roaming\npm\node_modules\npm\npmrc</span>

<span class="pl-k">prefix</span> = <span class="pl-s"><span class="pl-pds">"</span>C:\\Users\\USER_NAME\\AppData\\Roaming\\npm<span class="pl-pds">"</span></span>

<span class="pl-c"><span class="pl-c">;</span> "user" config from C:\Users\USER_NAME\.npmrc</span>

//registry.npmjs.org/:<span class="pl-k">_authToken</span> = (protected)

<span class="pl-c"><span class="pl-c">;</span> node bin location = C:\Program Files\nodejs\node.exe</span>
<span class="pl-c"><span class="pl-c">;</span> cwd = C:\Users\USER_NAME\Repos\runScriptWorkspace</span>
<span class="pl-c"><span class="pl-c">;</span> HOME = C:\Users\USER_NAME</span>
<span class="pl-c"><span class="pl-c">;</span> Run `npm config ls -l` to show all defaults.</span></pre></div>