Skip to content

Issue with collection cache name when using FileSystemEngineHost #12156

Closed
@alexeagle

Description

@alexeagle

From @sasxa on November 19, 2017 14:40

Bug Report or Feature Request (mark with an x)

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

Area

- [ ] devkit
- [x] schematics

Versions

"@angular-devkit/schematics": "0.0.36",

Repro steps

FileSystemEngineHost cannot resolve collection.json in subfolder. For example, with @nrwl/schematics it's in @nrwl/schematics/src/collection.json. I extended it and used this;

import { sync as globSync } from 'glob';

export class MyEngineHost extends FileSystemEngineHost {
  constructor(protected _root: string) { super(_root); }

  protected _resolveCollectionPath(name: string): string {
    // Allow `${_root}/${name}.json` as a collection.
    if (existsSync(join(this._root, name + '.json'))) {
      return join(this._root, name + '.json');
    }

    // Allow `${_root}/${name}/collection.json.
    if (existsSync(join(this._root, name, 'collection.json'))) {
      return join(this._root, name, 'collection.json');
    }

    // Allow `${_root}/ ** /${name}/collection.json.
    const collectionJsonPath = globSync(`${ name }/**/collection.json`, {
      cwd: this._root
    })[ 0 ];
    if (collectionJsonPath) {
      return join(this._root, collectionJsonPath);
    }

    throw new CollectionCannotBeResolvedException(name);
  }
}

Next, I created host, engine and collection:

const host = new MyEngineHost('my_root');
host.registerOptionsTransform(/* ... */)
const engine = new SchematicEngine(host);
const collection = engine.createCollection('@nrwl/schematics');

So far everything works:

host.listSchematics(collection); /* OK */

but when I try to create schematics:

const schematic = collection.createSchematic('app');

I get an error Error: Unknown collection "nx".

The log given by the failure

It turns out that createSchematic() is using name from @nrwl/schematics/src/collection.json which is set to nx, but is caching it using @nrwl/schematics;

collection-cache

Desired functionality

Since name in collection.json is optional (it's not set in @schematics/angular) I believe collection name should be used and not name from collection.json.

Mention any other details that might be useful

Chaning FileSystemEngineHost _transformCollectionDescription() to match NodeModulesEngineHost _transformCollectionDescription() should solve the issue

    // return desc as FileSystemCollectionDesc;
    return {
      ...desc,
      name,
    } as FileSystemCollectionDesc;

Copied from original issue: angular/devkit#285

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgentarea: @angular-devkit/schematicsfreq1: lowOnly reported by a handful of users who observe it rarelyneeds: investigationRequires some digging to determine if action is neededseverity3: brokentype: bug/fix

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions