Skip to content

Cannot run Angular library migration in Nx workspace #20282

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
1 of 4 tasks
bradley-carestia-jbh opened this issue Nov 16, 2023 · 19 comments · Fixed by #27634
Closed
1 of 4 tasks

Cannot run Angular library migration in Nx workspace #20282

bradley-carestia-jbh opened this issue Nov 16, 2023 · 19 comments · Fixed by #27634
Assignees
Labels
blocked: repro needed outdated scope: angular Issues related to Angular support in Nx scope: core core nx functionality type: bug

Comments

@bradley-carestia-jbh
Copy link

bradley-carestia-jbh commented Nov 16, 2023

Current Behavior

We have an internal Angular library with migration scripts for use with ng update. The scripts work fine in a standard Angular workspace.

When run though nx migrate however, the arguments to the script are always undefined. Just like in this StackOverflow post https://stackoverflow.com/questions/76860116/tree-is-undefined-angular-update-schematics-in-a-nx-workspace. Running with FORCE_NG_UPDATE=true results in "Error: This command is not available when running the Angular CLI outside a workspace."

To be clear; the migrations.json file is generated and appears to run without errors, it just doesn't actually do anything do to the undefined argument.

As not everyone using this library is using Nx, we can't make an Nx generator instead; and it seems awkward to maintain both an Angular and Nx version of the migration script. The consuming project is currently on Nx 16.3.1.

Expected Behavior

The library's migration script to run correctly in Nx workspaces.

GitHub Repo

No response

Steps to Reproduce

  1. Create Angular library migration with standard structure, ie:
export function version4(_options: any): Rule {
  return (host: Tree, context: SchematicContext) => {
    const version = `4.0.0`;
    context.logger.info(`Applying migration for lib to version ${version}`);
    ...
    return host;
  };
  1. Run in Nx workspace with nx migrate. Notice not even the log statement works (though a standard console.log does, its definitely calling the function)

Nx Report

Node   : 16.20.1
   OS     : darwin arm64
   yarn   : 1.22.19
   Hasher : Node
   
   nx                 : 16.3.1
   @nx/js             : 16.3.2
   @nrwl/js           : 16.3.1
   @nx/jest           : 16.3.1
   @nrwl/jest         : 16.3.2
   @nx/linter         : 16.3.1
   @nrwl/linter       : 16.3.2
   @nx/workspace      : 16.3.1
   @nx/angular        : 16.3.1
   @nx/cypress        : 16.3.1
   @nx/devkit         : 16.3.2
   @nx/eslint-plugin  : 16.3.1
   @nx/plugin         : 16.3.2
   @nrwl/tao          : 13.1.3
   @nx/web            : 16.3.1
   @nx/webpack        : 16.3.1
   typescript         : 5.0.4
   ---------------------------------------
   Community plugins:
   @custom/rollout              : 4.0.0
   @ngrx/component              : 16.0.1
   @ngrx/component-store        : 16.0.1
   @ngrx/eslint-plugin          : 16.0.1
   ---------------------------------------
   Local workspace plugins:
         @custom/opex-nx
   ---------------------------------------
   The following packages should match the installed version of nx
     - @nx/js@16.3.2
     - @nrwl/jest@16.3.2
     - @nrwl/linter@16.3.2
     - @nx/devkit@16.3.2
     - @nrwl/devkit@16.3.2
     - @nx/plugin@16.3.2
     - @nrwl/nx-plugin@16.3.2
     - @nrwl/tao@13.1.3
   
   To fix this, run `nx migrate nx@16.3.2`

Failure Logs

No response

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

@AgentEnder AgentEnder self-assigned this Nov 16, 2023
@AgentEnder AgentEnder added scope: angular Issues related to Angular support in Nx scope: core core nx functionality labels Nov 16, 2023
@Coly010 Coly010 assigned Coly010 and unassigned AgentEnder Mar 20, 2024
@Coly010
Copy link
Contributor

Coly010 commented Jul 24, 2024

Hey @bradley-carestia-jbh !

I know it's been a while and this is still sitting.

Would you be able to check if this is still happening in the latest version of Nx?

@Coly010 Coly010 added the blocked: retry with latest Retry with latest release or head. label Jul 24, 2024
@bradley-carestia-jbh
Copy link
Author

Good timing; we're about to do another upgrade cycle @Coly010 . I should be able to test it again in the near future.

Copy link

github-actions bot commented Aug 1, 2024

This issue has been automatically marked as stale because no results of retrying on the latest version of Nx was provided within 7 days.
It will be closed in 21 days if no results are provided.
If the issue is still present, please reply to keep it active.
If the issue was not present, please close this issue.
Thanks for being a part of the Nx community! 🙏

@github-actions github-actions bot added the stale label Aug 1, 2024
@bradley-carestia-jbh
Copy link
Author

@Coly010

Just ran my latest migration in version 19, and the issue is still present. My migration starts with:

export function version8(_options: any): Rule {
  return (host: Tree, context: SchematicContext) => {
    const version = `8.0.0`;
    context.logger.info(`Applying migration for library to version ${version}`);

While the output is just:

Running the following migrations:
- library: migration-v8.0.0 (Updates from v7 to v8)
---------------------------------------------------------

Running migration library: migration-v8.0.0
Ran migration-v8.0.0 from library
  Updates from v7 to v8

No changes were made

Notice the logging statement is not present.

@github-actions github-actions bot removed the stale label Aug 10, 2024
@Coly010 Coly010 removed the blocked: retry with latest Retry with latest release or head. label Aug 12, 2024
@Coly010
Copy link
Contributor

Coly010 commented Aug 21, 2024

@bradley-carestia-jbh I'm going to need a reproduction repo, minimal if possible, because I can't reproduce this behaviour.

You can see my repo here: https://github.com/Coly010/nx-ng-migration-repro.git

The root migrations.json points to the angular schematic migration and i can see it logging correctly:
image

If you clone my repro, make sure to

  1. install
  2. build

You may need to change the package.json to remove the devDependencies.'@ng-mig/acme' for the first install so that you can subsequently build the package.

@GuillaumeNury
Copy link

Hi @Coly010, I have the same problem.
Nx call the migration script differently if it is a Nx script or an @angular/cli script. The logic works most of the time, but not always.

The @angular/climigration will be run correctly only if it contains some of the strings checked here.

In your reproduction repo, your schematic is called (ie version4 function is called), but not the (host, context) => {} function.

This is because the compiled file of the schematic does not include require('@angular-devkit/schematics').

@Coly010
Copy link
Contributor

Coly010 commented Aug 23, 2024

@GuillaumeNury The(host, context) must be called in my reproduction, as the log is being output. That log is within the (host, context) block: https://github.com/Coly010/nx-ng-migration-repro/blob/main/acme/src/migrations/testing.ts#L7

We can see that log message output in the screenshot above

@GuillaumeNury
Copy link

I cannot see the log:

image

(I just added a "version4 called" log above (host, context))

@GuillaumeNury
Copy link

@Coly010 if this can help, I just

  • cloned your repro repo
  • npm i
  • nx build acme
  • nx migrate --run-migrations

Here is the nx report:

Node           : 20.11.0
OS             : darwin-arm64
Native Target  : aarch64-macos
npm            : 10.2.4

nx                 : 19.6.1
@nx/js             : 19.6.1
@nx/jest           : 19.6.1
@nx/linter         : 19.6.1
@nx/eslint         : 19.6.1
@nx/workspace      : 19.6.1
@nx/angular        : 19.6.1
@nx/devkit         : 19.6.1
@nx/eslint-plugin  : 19.6.1
@nx/plugin         : 19.6.1
@nrwl/tao          : 19.6.1
@nx/web            : 19.6.1
@nx/webpack        : 19.6.1
typescript         : 5.5.4
---------------------------------------
Community plugins:
@ng-mig/acme : 0.0.2

@GuillaumeNury
Copy link

@Coly010 here is a repro, with a second schematic: Coly010/nx-ng-migration-repro@main...GuillaumeNury:nx-ng-migration-repro:repro

image

@Coly010
Copy link
Contributor

Coly010 commented Aug 23, 2024

Interesting, with your change in place, i'm reproducing the issue

@Coly010
Copy link
Contributor

Coly010 commented Aug 23, 2024

Ok, I was able to fix it easily enough:

ensure "cli": "ng" is set in your package's migrations.json file:

{
  "schematics": {
    "testing": {
      "cli": "ng",
      "version": "0.0.2",
      "description": "Migration for v0.0.2",
      "factory": "./src/migrations/testing"
    }
}

@GuillaumeNury I've pushed an update to the PR (https://github.com/Coly010/nx-ng-migration-repro/pull/2/files) that I created from your change comparison, and here is the result from run-migrations
image

@Coly010
Copy link
Contributor

Coly010 commented Aug 23, 2024

@bradley-carestia-jbh Can you confirm if setting cli: ng resolves your issue?

@bradley-carestia-jbh
Copy link
Author

I'll give it a try!

@GuillaumeNury
Copy link

@Coly010 it does not work on my machine 🤔
What version of Nx are you using?

If you look at isAngularMigration in migrate.ts file, it does not use cli === "ng".

Since #16259, cli is not really used anymore. You can see in the test files of this PR that var angular_devkit_core1 = require("@angular-devkit/core"); was added. But when you compile an Angular CLI schematics, this line is not always there.

@Coly010
Copy link
Contributor

Coly010 commented Aug 26, 2024

@GuillaumeNury in fact, in that file, CLI is the default check:

return entry.cli ? entry.cli !== 'nx' : useAngularDevkitToRunMigration;

return entry.cli ? entry.cli !== 'nx' : useAngularDevkitToRunMigration;

If the CLI property exists on the package’s migrations.json file, then return true if it does not equal ‘nx’.

If the CLI property does not exist on the package’s migrations.json, use the other methods to try find if it’s an angular migration.

I do understand the issue that if you’ve only imported types from angular devkit that after compilation that import will be stripped.

Coly010 added a commit that referenced this issue Aug 26, 2024
…cli or schematics #20282
@GuillaumeNury
Copy link

Oh, I misread the condition!
The problem was indeed the lack of cli property in migrations.json file. This property is not an "official" one : the cli property is not in the collection-schema.json).

@Coly010 I tried your fix on isAngularMigration function and it works like a charm! Thx ❤️

@Coly010
Copy link
Contributor

Coly010 commented Aug 26, 2024

This property is not an "official" one : the cli property is not in the collection-schema.json

Yep, which is why we had planned to remove it, but given the fallback logic was flawed, I think it makes sense to keep it for another while to let people have the change to migrate

Coly010 added a commit that referenced this issue Aug 26, 2024
…cli or schematics #20282 (#27634)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
We check the CLI property exists and if it's not `nx` we will use the ng
compat layer to run the migration.
If the CLI property does not exist, we check both if the migration is in
the `schematics` object on the `migrations.json` and if the contents of
the migration implementation contains an import from `@angular-devkit`.

The problem with the fallback is that if only types are imported from
`@angular-devkit` the import is stripped from the migration
implementation completely.


## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
We had planned already to remove the fallback of reading the file
contents.
We had also planned to remove using the `cli` property to determine if
the migration needed the `ng compat layer`.

However, as the `cli` property is still useful for now for package's
that needed some manner to circumvent the flawed fallback logic, let's
continue to use it until v21.
Log a warning however if the `cli !== 'nx'` and it is placed in the
`generators` section of the `migrations.json` to provide ample time for
plugin developers to move them to the `schematics` property.

Fallback has been updated to whether or not the migration lives in
`schematics` and not flawed read file logic.


## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #20282
FrozenPandaz pushed a commit that referenced this issue Aug 26, 2024
…cli or schematics #20282 (#27634)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
We check the CLI property exists and if it's not `nx` we will use the ng
compat layer to run the migration.
If the CLI property does not exist, we check both if the migration is in
the `schematics` object on the `migrations.json` and if the contents of
the migration implementation contains an import from `@angular-devkit`.

The problem with the fallback is that if only types are imported from
`@angular-devkit` the import is stripped from the migration
implementation completely.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
We had planned already to remove the fallback of reading the file
contents.
We had also planned to remove using the `cli` property to determine if
the migration needed the `ng compat layer`.

However, as the `cli` property is still useful for now for package's
that needed some manner to circumvent the flawed fallback logic, let's
continue to use it until v21.
Log a warning however if the `cli !== 'nx'` and it is placed in the
`generators` section of the `migrations.json` to provide ample time for
plugin developers to move them to the `schematics` property.

Fallback has been updated to whether or not the migration lives in
`schematics` and not flawed read file logic.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #20282

(cherry picked from commit 62c6512)
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 Sep 26, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: repro needed outdated scope: angular Issues related to Angular support in Nx scope: core core nx functionality type: bug
Projects
None yet
4 participants