Skip to content

Conversation

AleksanderBodurri
Copy link
Member

@AleksanderBodurri AleksanderBodurri commented Jul 20, 2025

Previously, Angular devtools would mistakenly traverse the same DOM elements multiple times while doing traversal for the component tree explorer. This error case would occur when more than 1 Angular application root component was present on the same page and in distinct DOM branches.

An example of where it would enter the irregular behaviour (previous state)

<app-root>
...
</app-root>
<other-app>
...
</other-app>
Screenshot 2025-07-20 at 2 03 12 AM

Now, we properly ignore duplicate DOM paths when looking for application and non-application root component to begin the Angular DevTools component discovery logic.

Screenshot 2025-07-20 at 2 04 45 AM

@AleksanderBodurri AleksanderBodurri changed the title fix: DOM traversal bug fix(devtools): DOM traversal bug Jul 20, 2025
@ngbot ngbot bot added this to the Backlog milestone Jul 20, 2025
@@ -92,30 +92,6 @@ describe('render tree extraction', () => {
expect(rtree[0].children[0].children.length).toBe(0);
});

it('should go all the way to the root element to look up for nodes', () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to traverse up to the DOM is no longer the responsibility of RTree strategy so this test is no longer necessary.

The new behaviour is now tested for in devtools/cypress/integration/node-search.e2e.js.

@AleksanderBodurri AleksanderBodurri force-pushed the fix-traversal branch 2 times, most recently from cec7d4d to 2f3278e Compare July 21, 2025 13:50
@AleksanderBodurri AleksanderBodurri force-pushed the fix-traversal branch 2 times, most recently from 27f7703 to 82192b9 Compare July 27, 2025 20:08
Copy link
Contributor

@dgp1130 dgp1130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a good step in the right direction, so I think it's fine to move forward with this PR. I suspect there might be a future change we should consider in the framework so ApplicationRef actually has correct knowledge of all the root views in an application so we don't need to do this work in DevTools at all.

@thePunderWoman thePunderWoman added target: minor This PR is targeted for the next minor release action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Aug 6, 2025
@AleksanderBodurri AleksanderBodurri force-pushed the fix-traversal branch 2 times, most recently from 4d3cf76 to 740f871 Compare August 11, 2025 04:00
Copy link
Contributor

@dgp1130 dgp1130 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple minor suggestions to think about, but nothing too significant. Thanks for looking into this!

return [];
}

return strategy.build(element, i++);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider: I think flatMap provides an index argument you can use instead of tracking i++ directly.

You might want to flatMap element to [element, strategy] first to handle the !strategy case first:

elements.flatMap((element) => {
  const strategy = selectStrategy(element);
  if (!strategy) return [];

  return [element, strategy];
}).map(([element, strategy], index) => strategy.build(element, index));

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning an array from the map causes weird things to occur with the typing of element and strategy. They both end up being treated as RTreeStrategy | LTreeStrategy | Element. I think the current approach is sufficient to avoid this

@AleksanderBodurri AleksanderBodurri force-pushed the fix-traversal branch 3 times, most recently from dbc3524 to 0c7fe11 Compare August 16, 2025 05:51
Previously, Angular devtools would mistakenly traverse the same DOM elements multiple times while doing traversal for the component tree explorer. This error case would occur when more than 1 Angular application root component was present on the same page and in distinct DOM branches.

Some example cases that did work previously:

```html
<app-root>
...
</app-root>
```

```html
<app-root>
...
<app-root-2></app-root-2>
...
</app-root>
```

An example of where it would enter the irregular behaviour

```html
<app-root>
...
</app-root>
<app-root-2>
...
</app-root-2>
```

Now, we properly ignore duplicate DOM paths when looking for application and non-application root component to begin the Angular DevTools component discovery logic.
@AleksanderBodurri AleksanderBodurri added action: review The PR is still awaiting reviews from at least one requested reviewer and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Aug 18, 2025
@AleksanderBodurri AleksanderBodurri added action: merge The PR is ready for merge by the caretaker and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Aug 18, 2025
@thePunderWoman thePunderWoman added target: rc This PR is targeted for the next release-candidate and removed target: minor This PR is targeted for the next minor release labels Aug 18, 2025
thePunderWoman pushed a commit that referenced this pull request Aug 18, 2025
Previously, Angular devtools would mistakenly traverse the same DOM elements multiple times while doing traversal for the component tree explorer. This error case would occur when more than 1 Angular application root component was present on the same page and in distinct DOM branches.

Some example cases that did work previously:

```html
<app-root>
...
</app-root>
```

```html
<app-root>
...
<app-root-2></app-root-2>
...
</app-root>
```

An example of where it would enter the irregular behaviour

```html
<app-root>
...
</app-root>
<app-root-2>
...
</app-root-2>
```

Now, we properly ignore duplicate DOM paths when looking for application and non-application root component to begin the Angular DevTools component discovery logic.

PR Close #62719
@thePunderWoman
Copy link
Contributor

This PR was merged into the repository by commit 5115050.

The changes were merged into the following branches: main, 20.2.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action: merge The PR is ready for merge by the caretaker area: devtools target: rc This PR is targeted for the next release-candidate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants