angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

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

Open mattezell opened this issue 8 years ago • 5 comments

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!

mattezell avatar Aug 02 '18 17:08 mattezell

Related to https://github.com/angular/angular-cli/issues/11663, https://github.com/angular/angular-cli/issues/11754.

filipesilva avatar Aug 04 '18 11:08 filipesilva

Remove node_modules folder & install @angular-cli again

hemanthNaga avatar Feb 17 '19 06:02 hemanthNaga

@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 avatar May 26 '20 21:05 dgp1130

@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 :)

mattezell avatar May 26 '20 21:05 mattezell

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.