I should be able to import a TS file from an app or lib into my custom workspace schematic and use it.
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.
The specific steps to reproduce are as follows:
npx create-nx-workspace@latest schematics-playgroundAngular as the type of workspacedefaulttest schematic via ng g workspace-schematic testindex.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)./node_modules/.bin/nx workspace-schematic testnx 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.
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.
For reference, here is the resulting structure of the dist folder when I run the schematic and get the error:

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.
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
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.
Hey, thx for reporting this. I'll give it a look tomorrow
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.
I'm also experiencing this behavor importing @nwrl/workspace:library module files into @nrwl/nest:library modules in version 10.0.7
Still an issue? Because I'm still getting this error.
I encountered this today, did anyone find a workaround?
@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.