refactor: ensure paths are safely passed around in ng-update #19354
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Schematics using the TypeScript compiler API have a significant issue
with paths. The problem is that the TypeScript compiler API intends to
rely fully on absolute paths, while ng-update and the devkit tree intend
to rely on project-scoped/relative paths.
This is problematic as it is straightforward to mix paths up, leading to
unexpected errors. For example, computing a relative path between a devkit
path and an absolute disk path results in a non-existent path.
To fix this ambiguity, we improve type safety of paths by using branded
primitives such as the
@angular-devkit/core
Path
or an even morestrict type called
WorkspacePath
. A normal string is considered anunresolved path and cannot be assigned to a
WorkspacePath
. Similarly aWorkspacePath
cannot be assigned to astring
. This ensures thatresolved paths are not accidentally mixed with unresolved paths, and
that we can safely pass around unresolved and resolved paths without
loss of their semantics.
This fixes a case (and potentially more) where a devkit path and an absolute disk
path result in an incorrectly resolved resource. In CLI apps it's possible to reference
resources through absolute project-scoped paths. e.g.
/src/app/app.component.html
. This has been fixed, and tests have been added (this might be the fix for #19354)Note: Long-term it would be ideal to implement a TypeScript compiler host that only deals with resolved workspace paths, but this is not possible yet as implementing TypeScript's
readDirectory
host method with a virtual tree is complex (and TS doesn't expose its helpers here).