Schematics : Copy then delete can cause "This should never happen" error.

Open
#16,527 0 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
angular, typescript
Domain
tooling

Research direction

Start by running the minimal reproduction from the issue's schematic spec, especially when the tree is created by another schematic. Then inspect the Tree copy/delete behavior and the move rule in @angular-devkit/schematics/src/rules. Done means the copy-then-delete case no longer throws “This should never happen” and the move rule no longer reports ENOENT.

Written by the indexing model from the issue text.

Description

area: @angular-devkit/schematics freq1: low severity3: broken type: bug/fix

🐞 Bug report

Description

Moving files as part of a schematic can cause an error to be thrown:

  Message:
    Error: This should never happen. Path: "/projects/bar".

It seems to happen if those files were created by a previous rule and the move is done by copy and deletion.

(As an aside here, it seems that delete operations are hoisted somehow...whenever they occur in a schematic, the logger output shows them to always happen first, this is not what I'd expect)

I'm aware that there is a move rule in @angular-devkit/schematics/src/rules, but this does not work. Attempting to use this rule to move files from one folder to another results in this error: ENOENT: no such file or directory, rename '<source>' -> '<destination>'

🔬 Minimal Reproduction

export function move(schema: Schema): Rule {
  return (host: Tree, _context: SchematicContext): Tree => {
    const dir = host.getDir(schema.source);
    dir.visit(file => {
      const newPath = file.replace(schema.source, schema.destination);

      const buffer = host.read(file);
      if (buffer === null) {
        return;
      }

      host.create(newPath, buffer);
    });

    host.delete(schema.source);

    return host;
  };
}

See also: https://github.com/jdpearce/move-schematic

NB - this schematic will probably work if run against a pre-existing workspace, but when the tree is set up by another schematic, as in the spec file, that's when the error is thrown.

🌍 Your Environment

Angular CLI: 8.3.21
Node: 13.5.0
OS: darwin x64
Angular: undefined
...

Package Version

@angular-devkit/architect 0.803.21 (cli-only)
@angular-devkit/core 8.3.21
@angular-devkit/schematics 8.3.21
@schematics/angular 8.3.21
@schematics/update 0.803.21 (cli-only)
rxjs 6.5.4
typescript 3.5.3

Dominant language
TypeScript
Stars
27k
Forks
11.8k
Avg merge
16h 35m
Merged PRs (30d)
176

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from angular/angular-cli

All issues in angular/angular-cli

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.