Skip to content

Bug: Can't return Rule from Schematic - "Invalid rule result: Function()" #11759

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
mattezell opened this issue Aug 2, 2018 · 5 comments
Closed
Labels
area: @angular-devkit/schematics freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Milestone

Comments

@mattezell
Copy link

Bug Report or Feature Request (mark with an x)

- [X] bug report -> please search issues before submitting
- [ ] feature request

Command (mark with an x)

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

Versions

Node 8.11.3
NPM 5.6.0
@angular/common/core/compiler 6.0.3
@angular-devkit/core 0.7.2
@angular-devkit/schematics 0.7.2

Repro steps

Excerpt from collection.json

{
  "$schema": "./node_modules/@angular-devkit/schematics/collection-schema.json",
  "schematics": {
    "test": {
      "description": "A test to see if a Rule can be returned...",
      "factory": "./test/index"
    }
  }
}

test\index.ts

import {
    Rule,
    SchematicsException,
    Tree,
    apply,
    branchAndMerge,
    chain,
    filter,
    mergeWith,
    move,
    noop,
    template,
    url,
    SchematicContext,
    Source,
} from '@angular-devkit/schematics';
 
export default function(options: any) {
    return chain([returnRule(options)]);
}
 
export function returnRule(options: any) {
    return (tree: Tree, context: SchematicContext) => {
        return chain([
            (tree: Tree, context: SchematicContext) => {
                context.logger.info('here we go...')
            }
        ])
    }
}

Install/add and attempt to use...

npm install @my/schematics
ng add @my/schematics
ng g @my/schematics:test

The log given by the failure

Invalid rule result: Function().

Desired functionality

https://github.com/angular/angular-cli/releases/tag/v6.1.0
According to the 6.1.0 release notes for @angular-devkit/schematics, a Rule should now be returnable from a schematic - yet I get this "Invalid Rule" error.. I originally encountered it when trying to copy/modify the component schematic from the Angular repo, which uses this new 'return a rule' approach - and since have reproduced the issue with the above basic test schematic...

Mention any other details that might be useful

Am I missing something - specifically comprehensive documentation and guidance examples? We adopted Schematics for an enterprise project due to Angular CLI's 'dog fooding' Schematics for the Angular projects - but we've found it very, very difficult to use due to the currently incomplete docs and sparse guidance to be found for non-trivial collections around the web... Is Schematics 'production ready', or would we be best served reverting to our old Gulp ways?

Thanks!

@filipesilva
Copy link
Contributor

Related to #11663, #11754.

@hemanthNaga
Copy link

Remove node_modules folder & install @angular-cli again

@dgp1130 dgp1130 added freq1: low Only reported by a handful of users who observe it rarely severity3: broken triage #1 type: bug/fix labels May 26, 2020
@ngbot ngbot bot modified the milestones: needsTriage, Backlog May 26, 2020
@dgp1130
Copy link
Collaborator

dgp1130 commented May 26, 2020

@mattezell, I'm able to run your example with any errors with the latest CLI. Had to rename/remove some parameters to make tsc's overly aggressive checks happy, but it's otherwise the same source.

import {
  Tree,
  chain,
  SchematicContext,
} from '@angular-devkit/schematics';

export default function(options: any) {
  return chain([returnRule(options)]);
}

export function returnRule(_: any) {
  return () => {
      return chain([
          (_: Tree, context: SchematicContext) => {
              context.logger.info('here we go...')
          }
      ])
  }
}

Version information:

douglasparker@basilisk (~/Source/fixit-test)
[20-05-26 14:22:26]$ ng version

     _                      _                 ____ _     ___
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 9.1.7
Node: 12.16.1
OS: linux x64

Angular: 9.1.9
... animations, common, compiler, compiler-cli, core, forms
... platform-browser, platform-browser-dynamic, router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.901.7
@angular-devkit/build-angular     0.901.7
@angular-devkit/build-optimizer   0.901.7
@angular-devkit/build-webpack     0.901.7
@angular-devkit/core              9.1.7
@angular-devkit/schematics        9.1.7
@angular/cli                      9.1.7
@ngtools/webpack                  9.1.7
@schematics/angular               9.1.7
@schematics/update                0.901.7
rxjs                              6.5.5
typescript                        3.8.3
webpack                           4.42.0

I'm assuming this issue has been fixed at some point after the bug was filed. Please re-open if you are still seeing the error.

@dgp1130 dgp1130 closed this as completed May 26, 2020
@mattezell
Copy link
Author

@dgp1130 Yeah, perhaps I should have closed this some time back. I opened it on Ng 6 - we've since upgraded our app to Ng 7, Ng 8 and Ng 9 :)

@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 Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: @angular-devkit/schematics freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Projects
None yet
Development

No branches or pull requests

4 participants