Skip to content

ng g directive generate wrong import in a module #2762

Closed

Description

@akomm

OS?

Ubuntu 14.04 lts

Versions.

angular-cli: 1.0.0-beta.17
node: 5.6.0

Repro steps.

  • ng new test
  • cd test
  • ng g module modulename
  • cd src/app/modulename
  • ng g directive directivename

The log given by the failure.

No failure

Expected result

Generates files:

  • src/app/modulename/modulename.module.ts
  • src/app/modulename/modulename.component.ts
  • src/app/modulename/modulename.component..spec.ts
  • src/app/modulename/modulename.component.html
  • src/app/modulename/modulename.component.css
  • src/app/modulename/directivename/directivename.directive.ts
  • src/app/modulename/directivename/directivename.directive.spec.ts

Content of src/app/modulename/modulename.module.ts:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { ModulenameComponent } from './modulename.component';
import { DirectivenameDirective } from './directivename/directivename.directive';

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [ModulenameComponent, DirectivenameDirective]
})
export class ModulenameModule { }

Actual result (what's wrong - DIFF)

  • src/app/modulename/directivename.directive.ts
  • src/app/modulename/directivename.directive.spec.ts

Content of src/app/modulename/modulename.module.ts:

// ...
import { DirectivenameDirective } from './modulename/directivename.directive';
// ...

Problem

  1. ng g directive does not behave like ng g component, which generate output similar to the expected one.
  2. Ignoring the 1. problem, it still generates wrong import path, resolving relative to src/app and not src/app/module.

Activity

  1. akomm commented on Oct 18, 2016

    @akomm
    Author

    Found the difference is that the component blueprint has the option flat default set to false and the directive blueprint has it set by default to true. When I use ng g directive modulename/directivename --flat=false problem 1 is fixed, but problem 2 still exists.

  2. filipesilva commented on Oct 23, 2016

    @filipesilva
    Contributor

    @Brocco can you have a look?

  3. added
    P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
    on Oct 23, 2016
  4. akomm commented on Oct 24, 2016

    @akomm
    Author

    If you want to know whether it was fixed in beta.18: No. Tested it :)

  5. Brocco commented on Oct 25, 2016

    @Brocco
    Contributor

    This seems to be behaving as expected...

     ~/dev/mlb/bar (master): ng g module modulename
    installing module
      create src/app/modulename/modulename.module.ts
    installing component
      create src/app/modulename/modulename.component.css
      create src/app/modulename/modulename.component.html
      create src/app/modulename/modulename.component.spec.ts
      create src/app/modulename/modulename.component.ts
     ~/dev/mlb/bar (master): ng g directive directivename
    installing directive
      create src/app/directivename.directive.spec.ts
      create src/app/directivename.directive.ts

    modulename is created in it's own directory... as expected
    directivename is created in the app directory... as expected
    and directivename is imported and declared in app.module.ts... as expected (shown here)

    ...
    import { DirectivenameDirective } from './directivename.directive';
    @NgModule({
      declarations: [
        AppComponent,
        DirectivenameDirective
      ]
    ...
  6. akomm commented on Oct 25, 2016

    @akomm
    Author

    You are not creating it in a module, as I'v shown in the reproduction. As I'v said:
    generates wrong import path, resolving relative to src/app and not src/app/module
    and ng g directive generate wrong import |in a module|

    Since you'v created it in src/app, it obviously works. I assume it is intended to work inside of modules, because the ng g component works, why should directives not?

  7. Brocco commented on Oct 25, 2016

    @Brocco
    Contributor

    I followed your repro steps...

    ng new test
    cd test
    ng g module modulename
    ng g directive directivename
    

    should they be?

    ng new test
    cd test
    ng g module modulename
    cd src/app/modulename
    ng g directive directivename
    

    With those repro steps I was able to reproduce this issue (I updated your repro steps above)

    FYI, this is also an issue with pipe generations.

  8. added a commit that references this issue on Oct 25, 2016
    c7ad8bc
  9. added a commit that references this issue on Oct 25, 2016
    86021a0
  10. angular-automatic-lock-bot commented on Sep 6, 2019

    @angular-automatic-lock-bot

    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.

  11. locked and limited conversation to collaborators on Sep 6, 2019
  12. added a commit that references this issue on May 29, 2026
    c8fb7bc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions