Nx: Webstorm's Typescript checker errors with 10.0.* nest libs (possibly others as well)

Created on 3 Aug 2020  路  3Comments  路  Source: nrwl/nx

Current Behavior

When using Webstorm with nx 10.0.* the typescript service will error out with some imports. The error message is as follows:

TS6059: File '/home/max/dev/ts-error-repo/libs/api-core/lib/src/index.ts' is not under 'rootDir' '/home/max/dev/ts-error-repo/libs/api-core/modules/src'. 'rootDir' is expected to contain all source files.

here a screenshot of the behaviour:
webstorm-ts-error

This was only observed with nest libs, as the tsconfig contains the rootDir property in the lib tsconfigs, which seems to confuse webstorm's typescript checking service.

Expected Behavior

Webstorm should work normally with nx and typescript.

Steps to Reproduce

I created a test repro repo (wow what a word :joy:) here

Basically when creating nest libs (or any other lib that generates a tsconfig with the rootDir property the ts service seems to be confused.

Environment

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

node bug

Most helpful comment

This happens because of the tsconfig solution that was introduced with Nx 10. As soon as there's a reference in the project's tsconfig.json, rootDir acts a little bit different. There's a couple workarounds you can do.

If your node project is not publishable/buildable (ie. no build architect), you can just remove the rootDir: "./src" from the tsconfig.lib.json (modules/tsconfig.lib.json in your repro).

If the project is buildable, then you'll have to place add references to other tsconfigs in the tsconfig.lib.json. So for your repro modules/tsconfig.lib.json should have a property like this:

  "references": [
    {
      "path": "../lib"
    },
    {
      "path": "../../shared-types"
    }
  ]

The second solution is kinda wonky, as then you'll have to manage your own dependencies.

With Nx, we want this to be automatic so I'll be working on a way to make this transparent.

All 3 comments

This happens because of the tsconfig solution that was introduced with Nx 10. As soon as there's a reference in the project's tsconfig.json, rootDir acts a little bit different. There's a couple workarounds you can do.

If your node project is not publishable/buildable (ie. no build architect), you can just remove the rootDir: "./src" from the tsconfig.lib.json (modules/tsconfig.lib.json in your repro).

If the project is buildable, then you'll have to place add references to other tsconfigs in the tsconfig.lib.json. So for your repro modules/tsconfig.lib.json should have a property like this:

  "references": [
    {
      "path": "../lib"
    },
    {
      "path": "../../shared-types"
    }
  ]

The second solution is kinda wonky, as then you'll have to manage your own dependencies.

With Nx, we want this to be automatic so I'll be working on a way to make this transparent.

So if I understand correctly, if we remove the rootDir property from all nest libs we're fine? The nest libs are just shared code for nest apps anyways and not buildable on their own anyways :thinking:

We already figured out that removing rootDir seems to be a workaround but were not sure if this breaks something in the background, so I thought I'd create an issue. Thanks for your reply, you guys do awesome work :raised_hands:

Yup, rootDir affects the output of buildable node libs. And only so because we use the TypeScript compiler directly. So if you're not building those libs separately, then you're good to go.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elliotmendiola picture elliotmendiola  路  3Comments

Koslun picture Koslun  路  3Comments

zachnewburgh picture zachnewburgh  路  3Comments

jasedwards picture jasedwards  路  3Comments

MichaelWarneke picture MichaelWarneke  路  3Comments