-
Notifications
You must be signed in to change notification settings - Fork 161
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(migrations,ng-add): turn on encapsulation for devkit/schematics deps #13712
Conversation
Okay, I've pushed the change to use the bare specifier to escape the encapsulation on that specific helper so we can still use the dynamic import and branch should be working/testable now. There are path maps for the build and a task in the licensed pipe to update the package name in place already. I suspect debugging locally might be impacted, yet to try. |
Tested with a very old version of @damyanpetev you can create a branch to master. |
Related to #13668
As suggested by the Angular Team - using
"encapsulation": true
for the migration/schematics collections. This allows imports (well,require
-s really in the node context), to pass through the Angular CLI-s context - i.e. resolve that package's deps. While useful if the tree messes up and our imports won't fail, it should in theory also more closely guarantee the version used, assuming the ng CLI is updated first.The related code that handles the
encapsulation
option mostly lives here: https://github.com/angular/angular-cli/blob/4da732ddf686198ae598698dc4c74515d46624f0/packages/angular/cli/src/command-builder/utilities/schematic-engine-host.tsThis is based on Node's
module.createRequire
to create a context-bound require from the Angular CLI and override in consumed schematics using the VM context API to run said script with the overrides viavm.runInNewContext
.Note that this restricts the global API surface schematics/migrations can access as from what I can tell the new context is final in the sense that it doesn't backfill global API if not provided and so the list is fairly short:
https://github.com/angular/angular-cli/blob/4da732ddf686198ae598698dc4c74515d46624f0/packages/angular/cli/src/command-builder/utilities/schematic-engine-host.ts#L218-L234
The basics (
__dirname
,__filename
,console
,process
) are available, but we should be really careful not to step outside of those in the direct source (supporting packages should still be okay).Edit: Apparently will also need to be strict about circular refs, since that wrap is recursive and doesn't handle circular refs like node would, leading to 'Maximum call stack size exceeded' error.
The other part we were warned about are dynamic imports, which are not supported. The one we use atm was a workaround, will investigate if possible to remove.
Update:
Okay, so for the most part it seems we're not really going too far outside of the supported API. However, the dynamic import is still needed for
@angular/compiler
since that's ESM and schematics are strictly CommonJS still and there's no alternative, regardless if we use the pre-compiled js or not. The warning turned out true though, see thenew vm.Script
notes underimportModuleDynamically
. So that's not supported yet at all (behind experimental flag) and I get that error if a migration uses that.I do have a very cheeky workaround for that - if we require the specific file that only does the dynamic import as non-relative, it's a way to escape the wrap functionality and have that file run in the normal non-encapsulated context:
https://github.com/damyanpetev/bellum-gens/commits/encapsulation-test
Note: This will require us to use that in the source and some work for the licensed pipelines to edit it.
Additional information (check all that apply):
Checklist:
feature/README.MD
updates for the feature docsREADME.MD
CHANGELOG.MD
updates for newly added functionalityng update
migrations for the breaking changes (migrations guidelines)