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

Nx Migration fails for a custom migration of a package #15014

Closed
sonikasharma1403 opened this issue Feb 15, 2023 · 10 comments · Fixed by #15133
Closed

Nx Migration fails for a custom migration of a package #15014

sonikasharma1403 opened this issue Feb 15, 2023 · 10 comments · Fixed by #15133
Assignees
Labels
blocked: more info needed outdated scope: angular Issues related to Angular support in Nx scope: core core nx functionality type: bug

Comments

@sonikasharma1403
Copy link
Contributor

sonikasharma1403 commented Feb 15, 2023

Current Behavior

We have written custom schematics for our npm package, which uses updateWorkspace provided by @angular/devkit. This executes fine when we do ng update <packageName> in an application not dependent on NX.

Create a new NX application and do nx migrate <packageName>. Notice that the migrations.json is updated. But as soon as you try to run the migration. It fails with unable to run migration trying to access angular.json (which should not be part of NX).

wondering, if we have any docs or recommendation for such a scenario

Expected Behavior

Migrations should work as expected

GitHub Repo

No response

Steps to Reproduce

Mentioned above

Nx Report

 Report complete - copy this into the issue template

   Node : 16.14.2
   OS   : darwin x64
   yarn : 1.22.18
   
   nx                      : 15.7.1
   @nrwl/jest              : 15.7.1
   @nrwl/linter            : 15.7.1
   @nrwl/workspace         : 15.7.1
   @nrwl/angular           : 15.7.1
   @nrwl/cli               : 15.7.1
   @nrwl/cypress           : 15.7.1
   @nrwl/devkit            : 15.7.1
   @nrwl/eslint-plugin-nx  : 15.7.1
   @nrwl/js                : 15.7.1
   @nrwl/storybook         : 15.7.1
   @nrwl/tao               : 15.7.1
   @nrwl/webpack           : 15.7.1
   typescript              : 4.8.4
   ---------------------------------------
   Community plugins:
   @my/core             : 23.0.0
   @ngrx/effects        : 15.0.0
   @ngrx/router-store   : 15.0.0
   @ngrx/store          : 15.0.0
   ngx-build-plus       : 11.0.0
   @ngrx/store-devtools : 15.0.0
   @storybook/angular   : 6.5.16
   ng-mocks             : 14.6.0
   nx-stylelint         : 13.4.0

Failure Logs

No response

Additional Information

No response

@AgentEnder
Copy link
Member

Hey @sonikasharma1403!

This sounds like it could be an issue in our compatibility layer, but its hard to tell without some of the information above.

Can you run nx report in the repo that is failing and provide the versions?

Additionally, a reproduction here would be super helpful. If I were you and wanted to create the repro, I'd take the migrations.json file that was generated and copy it into a new Nx workspace. Copy the built migration files over into that workspace as well under some folder (call it migration or something), and update the path in the migrations.json file to point to it. At this point, strip out as much from the migration itself as you can while still getting the same error. This will help us find where the issue is occurring.

@AgentEnder AgentEnder added scope: angular Issues related to Angular support in Nx scope: core core nx functionality labels Feb 15, 2023
@sonikasharma1403
Copy link
Contributor Author

sonikasharma1403 commented Feb 17, 2023

migrations/test/index.ts

import { Rule, Tree } from '@angular-devkit/schematics';
import { updateWorkspace } from '@schematics/angular/utility/workspace';
import { ProjectType } from '@schematics/angular/utility/workspace-models';
export default function (): Rule {
  return async (host: Tree) => {
    return updateWorkspace(workspace => {
      workspace.projects.forEach(project => {
        if (project.extensions.projectType === ProjectType.Application) {
          project.architect.build.options.assets = [ // add something here]
        }
      });
    });
  };
}

migrations.json
...
 "migration-v23-test": {
      "version": "23.0.0",
      "description": "Modify assets",
      "factory": "./migrations/test"
    }
    

The above migrations work fine for a non-NX workspace by updating angular.json. But fail to update the project.json in an NX workspace

@sonikasharma1403
Copy link
Contributor Author

@AgentEnder - do let me know if you need more details to reproduce the issue

@sonikasharma1403
Copy link
Contributor Author

@AgentEnder - any update?

@leosvelperez
Copy link
Member

hey @sonikasharma1403!

Could you please post the error stack trace you're getting? Make sure you run the migration with --verbose.

@leosvelperez
Copy link
Member

@sonikasharma1403 there were a couple of issues with our Angular DevKit interoperability layer that you might have encountered when reporting this. Those issues were solved in 15.7.2. Could you try again with the latest version?

Also, the migration schematic code you shared here doesn't seem correct. I ran it and I get a valid error: Cannot read properties of undefined (reading 'build') in this line project.architect.build.options.assets = ['some-asset.css].

That error is legit, architect doesn't exist in project. You'd rather have:

import { Rule, Tree } from '@angular-devkit/schematics';
import { updateWorkspace } from '@schematics/angular/utility/workspace';
import { ProjectType } from '@schematics/angular/utility/workspace-models';

export function foo(): Rule {
  return async (host: Tree) => {
    return updateWorkspace((workspace) => {
      workspace.projects.forEach((project) => {
        if (
          project.extensions.projectType === ProjectType.Application &&
          project.targets.has('build')
        ) {
          project.targets.get('build').options.assets = ['some-asset.css'];
        }
      });
    });
  };
}

Using latest with the code above runs the migration successfully, but I doesn't make changes to the project.json config files. That's a separate issue I'll be working to address.

@sonikasharma1403
Copy link
Contributor Author

@leosvelperez - Thanks for the quick fix..

@sonikasharma1403
Copy link
Contributor Author

sonikasharma1403 commented Feb 22, 2023

@leosvelperez - I tried the code you have shared above with the Nx@15.8.0-beta.2, but still it does not make any changes to the project.json config files

@leosvelperez
Copy link
Member

@sonikasharma1403 running nx migrate next or nx migrate 15.8.0-beta.2 won't work. While that version is what you migrate to, the temp installation of nx used to run the migrations is still latest. The 15.8.0-beta.2 is not latest, it's next. So, the temp installation of nx used to run the migrations doesn't have the changes.

If you can't wait for the stable 15.8.0 release, you'd need to run NX_MIGRATE_USE_NEXT=true yarn nx migrate next instead. Please note you'd be migrating to a beta version which might have some issues.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: more info needed outdated scope: angular Issues related to Angular support in Nx scope: core core nx functionality type: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants