Skip to content

ng update did not change my import statements #14589

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

Closed
markgoho opened this issue May 30, 2019 · 37 comments
Closed

ng update did not change my import statements #14589

markgoho opened this issue May 30, 2019 · 37 comments

Comments

@markgoho
Copy link
Contributor

markgoho commented May 30, 2019

🐞 Bug report

Command (mark with an x)

- [ ] new
- [ ] build
- [ ] serve
- [ ] test
- [ ] e2e
- [ ] generate
- [ ] add
- [X] update
- [ ] lint
- [ ] xi18n
- [ ] run
- [ ] config
- [ ] help
- [ ] version
- [ ] doc

Is this a regression?

No, this behavior did not exist prior to 8.x

Description

ran ng update @angular/cli @angular/core
loadChildren import statements were not updated to new syntax

🔬 Minimal Reproduction

I haven't dug into the migration code, but I have my routes in a app-routing.module.ts file:

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
  {
    path: '',
    pathMatch: 'full',
    loadChildren: './home/home.module#HomeModule',
    data: { depth: 0 },
  },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {}

🔥 Exception or Error

No error, just old import pattern instead of new.

🌍 Your Environment

I was on Angular 7.2.15 and this was part of the update process to 8.0.0.

Anything else relevant?
I'm wondering if the migration code is targeting a certain file or something else that would have excluded this file from being updated to the new import pattern.

Here's the last bit from the ng update command:

    ** Executing migrations for package '@angular/core' **
            ------ Static Query Migration ------
            With Angular version 8, developers need to
            explicitly specify the timing of ViewChild and
            ContentChild queries. Read more about this here:
            https://v8.angular.io/guide/static-query-migration
            ------------------------------------------------
UPDATE src/app/search/search.component.ts (2049 bytes)
@alan-agius4
Copy link
Collaborator

Can you also confirm that you were not using an RC or beta version of Angular CLi?

@alan-agius4 alan-agius4 added the needs: more info Reporter must clarify the issue label May 30, 2019
@markgoho
Copy link
Contributor Author

markgoho commented May 30, 2019

@alan-agius4 not using an RC or beta.

When I try to manually update the import statements, I'm getting a Typescript error:
Dynamic import is only supported when '--module' flag is 'commonjs' or 'esNext'.ts(1323)

  {
    path: '',
    pathMatch: 'full',
    // loadChildren: './home/home.module#HomeModule',
    loadChildren: () => import('./home/home.module').then(m => m.HomeModule),
    data: { depth: 0 },
  },

Here is my tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "types": []
  },
  "exclude": [
    "src/test.ts",
    "**/*.spec.ts"
  ]
}

@markgoho
Copy link
Contributor Author

microsoft/TypeScript#16675 seems related here

@markgoho
Copy link
Contributor Author

markgoho commented May 30, 2019

I see in a newly created v8 project the root tsconfig.json shows the module property set to esnext

UPDATE: I tried setting this manually in my root tsconfig.json before upgrading to v8 but it still did not update my module imports. However, if now after the upgrade, I manually change the import paths, I'm not getting the above error anymore.

@filipesilva
Copy link
Contributor

Those should have been migrated. We also automatically migrate the module in tsconfig to esnext when the migration runs. So that makes me think the migration didn't run at all, for some reason.

@filipesilva
Copy link
Contributor

Any chance you can provide a repro of what you see? We test for this update so I'm not sure of what's going wrong.

@filipesilva filipesilva added needs: repro steps We cannot reproduce the issue with the information given and removed needs: more info Reporter must clarify the issue labels May 30, 2019
@markgoho
Copy link
Contributor Author

Sure, do you want me to do like a screen capture or something? Is there a debugging flag?

Here is my repo (pre v8 upgrade): https://github.com/markgoho/glWp

@alan-agius4
Copy link
Collaborator

@markgoho the repo link seems to be incorrect as I am getting a 404

@markgoho
Copy link
Contributor Author

Whoops! I had it set to private. Should be good to go now.

@slubowsky
Copy link

I had same issue. Migrated them using https://www.npmjs.com/package/@phenomnomnominal/angular-lazy-routes-fix instead...

@alan-agius4 alan-agius4 added comp: schematics/update freq3: high severity3: broken and removed needs: repro steps We cannot reproduce the issue with the information given labels May 30, 2019
@ngbot ngbot bot added this to the needsTriage milestone May 30, 2019
@ngbot ngbot bot modified the milestones: needsTriage, Backlog May 30, 2019
@filipesilva filipesilva modified the milestones: Backlog, 8.0.1 May 31, 2019
@filipesilva
Copy link
Contributor

We're looking at how update behaves in your project and it's odd. It doesn't seem to be running the right migrations. Looks like it's related to npm hoisting, but unsure.

@benbr-personal
Copy link

I'm also getting this bug, Angular Material correctly updated my paths etc but my imports did not upgrade and I had to manually do it myself as well as change my module param in the ts.config

@aaronte
Copy link

aaronte commented Jun 3, 2019

Same problem with loadChildren not automatically updating. Migrations for ViewChildren() happened though.

@MaciejWWojcik
Copy link

same problem

  • not working auto migrate - ViewChildren()
  • not working auto migrate - router routes

are there any info about the time this will be fixed?

@jtsom
Copy link
Contributor

jtsom commented Jun 6, 2019

Just a note that ViewChild() also did not migrate automatically.

@filipesilva
Copy link
Contributor

A workaround for now is to run the following commands after you are already on Angular and CLI version 8:

ng update @angular/cli --from 7 --to 8 --migrate-only
ng update @angular/core --from 7 --to 8 --migrate-only

@clydin
Copy link
Member

clydin commented Jun 7, 2019

The local CLI version needs to be 8.0.2 or greater for the migrate only part to work properly for these cases.

@EzyHow
Copy link

EzyHow commented Jun 9, 2019

Following worked for me:

ng update @angular/cli --from 7 --to 8 --migrate-only
ng update @angular/core --from 7 --to 8 --migrate-only
ng update @angular/material --from 7 --to 8 --migrate-only

Before above, I tried running:

ng update @angular/cli
ng update @angular/core
ng update @angular/material

But these only updated package.json. Not updated @ViewChild(), loadChildren and material imports.

@alexfung888
Copy link

alexfung888 commented Jun 20, 2019

I found that the following steps should be followed to upgrade from 7 to 8:

yarn Upgrade typescript@~3.4.5
# cannot work if still @3.2
git commit
# cannot upgrade if working tree is not clean
ng update @angular/cli @angular/core
# according to documentation, only the above line is needed, but alas no
git commit
# need to clean working tree again
ng update @angular/cli --from 7 --to 8 --migrate-only
# This time, will work.
# updates lazy loading modules, tsconfig.json, targets es2015 and creates the browserlist file
# you may want to edit the package.json now to include the browserlist information
git commit
yarn upgrade @angular/flex-layout@=8.0.0-beta.26
# since flex-layout betas often have breaking changes, I always = it to a particular release
# and you need 800b26 before you can update material
git commit
# yet another clean tree for material
ng update @angular/material
git commit
# finally completed.

@Splaktar
Copy link
Contributor

@alexfung888 I opened angular/angular-update-guide#35 to track the issue of having to run a lot of git commits. Have you seen any other areas where the docs need to be updated for this?

@alexfung888
Copy link

alexfung888 commented Jun 21, 2019

@alexfung888 I opened StephenFluin/angular-update-guide#35 to track the issue of having to run a lot of git commits. Have you seen any other areas where the docs need to be updated for this?

Actually I can tolerate making lots of git commits, as long as the update guide didn't sound as if none is necessary. And a clean working tree has the advantage of easily restore when the update went south.

More problematic is that on both repositories I tried, the update did not work and I need to do a separate --migrate-only with cli before the files (lazy loading modules, tsconfig.json, setting target to es2015 and the browserlist file) are updated.

And the dependencies were not spelt out. Like I listed in my comment above, you need to manually upgrade typescript and flex-layout for the update to work.

@alan-agius4 alan-agius4 modified the milestones: 8.0.x, 8.1.x Jul 2, 2019
@juristr
Copy link
Contributor

juristr commented Jul 3, 2019

@alan-agius4 @filipesilva I briefly mentioned this in the GDE slack and it very much looks that my issue might be related to this. I created a reproduction repo here: https://github.com/juristr/ngv7upgradetest

If you try to upgrade this repo with ng update @angular/cli @angular/core, the lazy route here

https://github.com/juristr/ngv7upgradetest/blob/177e9dacdb97367657b61e227fdb691fa51be19c/src/app/app-routing-routing.module.ts#L7

..doesn't get upgraded. In fact if you take a look at the output log, the CLI schematics don't seem to be executed at all.

Instead, by trial-and-error I found that for some reason it was due to the @angular/pwa package I was having in my repo. If you remove this line and do the upgrade again, it strangely works.

I'm very curious what the reason for this might be, so let me know in case you find a fix, or need help reproducing something.
Just for the record, I encountered a similar scenario where my workspace was upgraded to v8.1.0-rc.0 instead of the latest stable v8. I reported it here on the NX repo (nrwl/nx#1540) since it happened with my NX based monorepo. However it might be a CLI schematic issue as well. The culprit there was having ng-packagr

@alexfung888
Copy link

Instead, by trial-and-error I found that for some reason it was due to the @angular/pwa package I was having in my repo. If you remove this line and do the upgrade again, it strangely works.

Ah yes, even with my procedure above, pwa is not updated and I need to manually align it with cli.

@juristr
Copy link
Contributor

juristr commented Jul 3, 2019

@alexfung888 well, it's even less the problem that the @angular/pwa is not updated, but more that if it is present, other migration schematics, like the one of the CLI are note being executed at all. If you remove the @angular/pwa package for a moment and you do the upgrade, then it works.

@juristr
Copy link
Contributor

juristr commented Jul 3, 2019

Just as an additional note. In another project I had the same issue, removed the @angular/pwa there, but the upgrade still didn't happen. Looks like there must be something else still. Also the @angular/core migrations don't seem to have fully completed. After a first successful run, I re-executed them via ng update @angular/core --from 7 --to 8 --migrate-only and got some more @ViewChild(...) transformations 🤔

@clydin
Copy link
Member

clydin commented Jul 3, 2019

Unfortunately, there was a defect in the update functionality that was fixed in 8.0.2 that caused the wrong version of the @schematics/angular package to be used when executing migrations if an older version of the package happened to be hoisted to the root by the package manager. Manually running the migrations in this situation as shown in the comment above this one would indeed be the correct course of action.
This issue was also not limited to the @schematics/angular package but it is the most commonly used and therefore the most likely to exhibit the issue.

@mart2k
Copy link

mart2k commented Jul 9, 2019

Same here: Updating from 7.14 --> 8.10

Update routine did NOT update @ViewChild(), loadChildren and tsconfig.

@juristr
Copy link
Contributor

juristr commented Jul 9, 2019

@mart2k What happens if you re-run the migrations manually afterwards again?

ng update @angular/cli --from 7 --to 8 --migrate-only
ng update @angular/core --from 7 --to 8 --migrate-only

@mart2k
Copy link

mart2k commented Jul 9, 2019

@juristr that worked for me. Thanks to all.
Maybe this should be mentioned in the Angular Update Guide.

@jtsom
Copy link
Contributor

jtsom commented Jul 9, 2019

I think the issue is you shouldn't have to manually run those migrate-only scripts.

@juristr
Copy link
Contributor

juristr commented Jul 10, 2019

Exactly. Usually this shouldn't be necessary but read this comment: #14589 (comment)

@edinsonjp
Copy link

Updating to Angular 8.1.2 right now and had the same behaviour.
Re-running the commands as @juristr said worked just fine.

@alan-agius4
Copy link
Collaborator

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

@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 Apr 16, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests