question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Workspace Schematics: importing a TS file outside the "tools" directory causes a TS error

See original GitHub issue

Expected Behavior

I should be able to import a TS file from an app or lib into my custom workspace schematic and use it.

Current Behavior

When I try importing a file from an app or lib into my custom schematic, I get a 'rootDir' is expected to contain all source files TS error.

Failure Information (for bugs)

Steps to Reproduce

The specific steps to reproduce are as follows:

  1. Create a new workspace via npx create-nx-workspace@latest schematics-playground
  2. Select Angular as the type of workspace
  3. During the process it asks for the name of the first app, set it to default
  4. Create a test schematic via ng g workspace-schematic test
  5. Inside the index.ts file for that schematic, import the app.module.ts for the default app that was generated (or really any file that requires traversal outside of the tools directory)
  6. Run the schematic via ./node_modules/.bin/nx workspace-schematic test

Context

nx report

  @nrwl/angular : 9.0.2
  @nrwl/cli : 9.0.2
  @nrwl/cypress : 9.0.2
  @nrwl/eslint-plugin-nx : Not Found
  @nrwl/express : Not Found
  @nrwl/jest : 9.0.2
  @nrwl/linter : Not Found
  @nrwl/nest : Not Found
  @nrwl/next : Not Found
  @nrwl/node : Not Found
  @nrwl/react : Not Found
  @nrwl/schematics : Not Found
  @nrwl/tao : 9.0.2
  @nrwl/web : Not Found
  @nrwl/workspace : 9.0.2
  typescript : 3.7.5

Reproduction A minimal reproduction of the error can be found here. This was generated using the exact steps outlined in “Steps to Reproduce”. Just install dependencies and run the test schematic via nx workspace-schematic test.

Failure Logs

In the repro I am trying to import the AppModule from apps/default/src/app/app.module.ts into my schematic at tools/schematics/test/index.ts and it results in this error:

apps/default/src/app/app.module.ts:4:30 - error TS6059: File '/Users/Garve/projects/schematics-playground/apps/default/src/app/app.component.ts' is not under 'rootDir' '/Users/Garve/projects/schematics-playground/tools'. 'rootDir' is expected to contain all source files.

4 import { AppComponent } from './app.component';
                               ~~~~~~~~~~~~~~~~~

tools/schematics/test/index.ts:4:27 - error TS6059: File '/Users/Garve/projects/schematics-playground/apps/default/src/app/app.module.ts' is not under 'rootDir' '/Users/Garve/projects/schematics-playground/tools'. 'rootDir' is expected to contain all source files.

4 import { AppModule } from '../../../apps/default/src/app/app.module';
                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Found 2 errors.

Other

For reference, here is the resulting structure of the dist folder when I run the schematic and get the error: image

Almost positive the error is coming from this function, because I tried running the tsc -p tools/tsconfig.tools.json command myself and got the same error.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:22 (5 by maintainers)

github_iconTop GitHub Comments

4reactions
seadonkcommented, May 4, 2021

I found that templated ts files in the generator/schematic were being compiled when they shouldn’t be.

They had been excluded in tsconfig.base.json as "exclude": ["node_modules", "tmp", "tools/schematics/*/files/*]", which no longer worked because schematics was renamed to generators in NX 11.

I updated tsconfig.base.json to exclude tools/**/files/* instead to fix the issue. It would be nice if these exclusions were added with the creation of a generator, and also added to the migration scripts.

3reactions
hadrien-tomacommented, Oct 1, 2021

Same on our side, we have a bash script to do the copy and need to rerun it each time we need to propagate a change from generator A to generator B (generator B using generator A)

Read more comments on GitHub >

github_iconTop Results From Across the Web

TS errors while importing modules in files of shared directory
Property 'setState' is missing in type 'Test'. This happens only to imports from files that are located outside of directory where webpack- ...
Read more >
Ahead-of-time (AOT) compilation - Angular
The file name reported in the error message, my.component.ts.MyComponent.html , is a synthetic file generated by the template compiler that holds contents of ......
Read more >
Making an Addable Angular Package Using Schematics
Let's try to create an Angular library that's consumable easily by making an "ng add" schematic. This library will provide a simple exported ......
Read more >
TypeScript: Cannot use import statement outside a module
To solve the error "Cannot use import statement outside a module" in TypeScript, set the module option to commonjs in your tsconfig.json file...
Read more >
Angular CLI: "Module not found: Error: Can't resolve..."
This tells the TypeScript compiler that imports from os should be looked in the file src/empty.ts . This will fix the issue for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found