Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jdgarvey opened this issue Feb 23, 2020 · 23 comments
Assignees

Comments

@jdgarvey
Copy link

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.

@jdgarvey jdgarvey changed the title Workspace Schematics: importing a file outside the tools directory breaks Workspace Schematics: importing a TS file outside the "tools" directory causes a TS error Feb 23, 2020
@Cammisuli
Copy link
Member

When we run workspace schematics we use the tsconfig in the tools directory. This file has a property called rootDir: '.'. Because of this, the TypeScript compiler expects all files to be under the same directory as the tools folder.

You can try changing some of the settings in the tsconfig.tools.json file. You could possibly use the files or include properties. You can see all the options here:
https://www.typescriptlang.org/v2/en/tsconfig#files

@jdgarvey
Copy link
Author

Ok, I've tried removing the rootDir but that results in the underlying Nx script not being able to copy over some of the resulting files. I'll keep playing with it.

@juristr
Copy link
Member

juristr commented Feb 25, 2020

Hey, thx for reporting this. I'll give it a look tomorrow

@juristr juristr self-assigned this Feb 25, 2020
@radarsu
Copy link

radarsu commented Apr 27, 2020

I'm having similar problem. I'd like to use some utility functions in my schematic and they cannot be imported because workspace-schematic script breaks.

@github-actions github-actions bot added the stale label May 29, 2020
@vsavkin vsavkin removed the stale label May 29, 2020
@nrwl nrwl deleted a comment from github-actions bot May 29, 2020
@zpydee
Copy link

zpydee commented Aug 6, 2020

I'm also experiencing this behavor importing @nwrl/workspace:library module files into @nrwl/nest:library modules in version 10.0.7

@asherccohen
Copy link

Still an issue? Because I'm still getting this error.

@Tiedye
Copy link

Tiedye commented Nov 18, 2020

I encountered this today, did anyone find a workaround?

@callmeaponte
Copy link

callmeaponte commented Dec 16, 2020

@Tiedye It's a bit hacky, but I was able to work around this (until it gets fixed) by using a global require statement.

For example, if you need to import a foo.json file located at the root of the project:

Before

import * as fooData from '../../../foo.json';

After

let fooData;

function init() {
    fooData = require('../../../../../foo.json');
}

init();

... note that the extra ../../ in the global require is needed - because unless you changed the outDir in tsconfig.tools.json - this script gets executed from /dist/out-tsc/tools/generators/your-generator-name.

@github-actions
Copy link

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs.
If we missed this issue please reply to keep it active.
Thanks for being a part of the Nx community! 🙏

@github-actions github-actions bot added the stale label Mar 29, 2021
@juristr juristr removed the stale label Apr 12, 2021
@ronnyek
Copy link

ronnyek commented May 3, 2021

Sure seems like this still may be a problem... anyone with ideas about a fix. Real easy to create a generator / schematic that attempts to import a nx library into the new application, and ultimately fail when running because of this.

@seadonk
Copy link

seadonk commented 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.

@hadrien-toma
Copy link

I also would like to be able to use a generator from another generator of the same monorepo 👍

@Jad31
Copy link

Jad31 commented Aug 8, 2021

I also would like to be able to use a generator from another generator of the same monorepo

I would like to do the same as @hadrien-toma

@theres
Copy link

theres commented Aug 25, 2021

Is there any reason why workspace generators are not build in the same way as the apps? It could solve this issue as well as #5548 and allow to have different tsconfig for generators if needed.

@muuvmuuv
Copy link
Contributor

This is kind of annoying. Why was this decision made? We have many utility functions in /libs and /tools. ATM we need to copy and paste them into libs and vice versa...

@hadrien-toma
Copy link

hadrien-toma commented 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)

@ruslan-byondxr
Copy link

I have the same problem. Can't use "libs/" inside "tools/"

@chenka
Copy link

chenka commented Dec 28, 2021

I have the same problem too, I should be able to import libs I've created

@larsilus
Copy link

Me too, i cannot access a lib from a workspace-schematic

@raphzandrade
Copy link

Still no reason to why we can't use libs inside our generators?

@AgentEnder
Copy link
Member

The common use case for this (using a workspace library from a workspace generator) should be solved by #9116. One thing to note is that it doesn't solve it for current workspace generators, but migrating those into a local plugin library (perhaps named workspace or similar) and calling them with nx g {npmScope}/workspace:{generatorName} would work as one would expect.

After this use case is solidified and battle-tested, we may consider migrating existing workspace generators into a similar setup.

@AgentEnder AgentEnder assigned AgentEnder and unassigned juristr Mar 1, 2022
@AgentEnder
Copy link
Member

I'm going to close this out, since its addressed in the local plugin functionality.

@github-actions
Copy link

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests