Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/schematics): running external schematics with yarn pnp #26109

Merged
merged 2 commits into from
Oct 31, 2023

Conversation

michaelfaith
Copy link
Contributor

@michaelfaith michaelfaith commented Oct 23, 2023

This change addresses an issue encountered when running external schematics from a yarn pnp workspace. The function used to resolve a collection json using node used recursion in a way that it effectively walked itself into an exception. Then if the exception is the type it expected, it would keep going. This was flawed in that yarn with pnp throws a different type of error when it failed to load the mis-constructed collection path (e.g. /node_modules/@schematics/angular/collection.json/package.json). ENOTDIR instead of MODULE_NOT_FOUND.

This process of intentionally / knowingly walking into an exception seems problematic in
general. So, I addressed it by first checking if the schematics entry in the package
is a relative path. If it is, then don't construct the collection path from that.
If entry is not relative, then assume it's pointing at another package and we need
to recurse to get to the actual collection path.

I've tested this in both yarn pnp and non-pnp environments.

PR Checklist

Please check to confirm your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #25648

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Fixes: #25648

…n pnp

This change addresses an issue encountered when running external schematics from
a yarn pnp workspace.  The function used to resolve a collection json using node
used recursion in a way that it effectively walked itself into an exception. Then
if the exception is the type it expected, it would keep going.  This was flawed
in that yarn with pnp throws a different type of error when it failed to load
the mis-constructed collection path (e.g. `/node_modules/@schematics/angular/collection.json/package.json`).  `ENOTDIR` instead of `MODULE_NOT_FOUND`.

This process of intentionally / knowingly walking into an exception seems problematic in
general.  So, I addressed it by removing the recursion that was used mainly because
there's a similar need to construct the collection path from a relative path in the
package.json as there is to construct the collection path from a relative path that's
passed in.  Rather than leaning on the recursion to do this, I added the logic
at the time we pull the schematics path from the package, and move on.

Since the recursion was removed, the infinite recursion safety check at the start wasn't
needed anymore.

I've tested this in both yarn pnp and non-pnp environments.
@alan-agius4 alan-agius4 requested a review from clydin October 25, 2023 10:50
@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: review The PR is still awaiting reviews from at least one requested reviewer labels Oct 25, 2023
@clydin
Copy link
Member

clydin commented Oct 26, 2023

Thank you for the contribution.
The recursion is used to facilitate the use of package references within the schematics field of which the referenced package name could then contain a schematics field in its package.json that then needs to be resolved. This scenario unfortunately is lacking test coverage currently.
The relative path case appears well covered within this change but the package reference case with schematics fields look s like it would fail here. This may lead to breakage of some schematics packages.

@michaelfaith
Copy link
Contributor Author

michaelfaith commented Oct 26, 2023

@clydin thanks for the feedback. Do you happen to have an example of a package that has their collection declared in that way? Just so I understand the use case correctly. Or just a snippet of what that package.json entry might look like? And I'm guessing it could be that several package references are made before one finally resolves to a collection?

…n pnp

This change addresses an issue encountered when running external schematics from
a yarn pnp workspace.  The function used to resolve a collection json using node
used recursion in a way that it effectively walked itself into an exception. Then,
if the exception is the type it expected, it would keep going.  This was flawed
in that yarn with pnp throws a different type of error when it failed to load
the mis-constructed collection path
(e.g. `/node_modules/@schematics/angular/collection.json/package.json`).
`ENOTDIR` instead of `MODULE_NOT_FOUND`.

This process of intentionally / knowingly walking into an exception seems problematic in
general.  So, I addressed it by first checking if the `schematics` entry in the package
is a relative path.  If it is, then don't construct the collection path from that.
If entry is not relative, then assume it's pointing at another package and we need
to recurse to get to the actual collection path.

I've tested this in both yarn pnp and non-pnp environments.
@michaelfaith
Copy link
Contributor Author

I went ahead and added the recursion back in, but after the check for relative path. Let me know if you feel this addresses the package use-case.

Copy link
Member

@clydin clydin left a comment

Choose a reason for hiding this comment

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

LGTM.

Thank you for taking the time to improve this area of the code.

@clydin clydin 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 Oct 30, 2023
@alan-agius4 alan-agius4 merged commit f63520b into angular:main Oct 31, 2023
@michaelfaith michaelfaith deleted the fix/schematics-yarn-pnp branch October 31, 2023 12:38
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: running "external" schematics in yarn pnp workspace
3 participants