nx icon indicating copy to clipboard operation
nx copied to clipboard

File is not under 'rootDir' 'packages'. 'rootDir' is expected to contain all source files in npm preset

Open wizardnet972 opened this issue 3 years ago • 9 comments

Current Behavior

Got an error when I run yarn nx run foo:build

Compiling TypeScript files for project "foo"...
packages/core/src/index.ts:1:15 - error TS6059: File '/Users/wizardnet972/repo/nx-npm-preset/packages/core/src/lib/core.ts' is not under 'rootDir' 'packages/foo'. 'rootDir' is expected to contain all source files.

1 export * from './lib/core';
                ~~~~~~~~~~~~
packages/foo/src/lib/foo.ts:1:22 - error TS6059: File '/Users/wizardnet972/repo/nx-npm-preset/packages/core/src/index.ts' is not under 'rootDir' 'packages/foo'. 'rootDir' is expected to contain all source files.

1 import { core } from '@nx-npm-preset/core';
                       ~~~~~~~~~~~~~~~~~~~~~

Expected Behavior

nx should build foo and core

Steps to Reproduce

Clone: git clone https://github.com/wizardnet972/nx-npm-preset.git Run:

yarn install
yarn nx run foo:build

See the error in Current Behavior

-- OR -- Run those commands:

npx create-nx-workspace nx-npm-preset
✔ What to create in the new workspace · npm
✔ Use Nx Cloud? (It's free and doesn't require registration.) · No

yarn add @nrwl/js @nrwl/node -W

yarn nx g @nrwl/js:lib --name core --buildable
yarn nx g @nrwl/js:lib --name foo --buildable

Replace the content of /packages/foo/src/lib/foo.ts with: (https://github.com/wizardnet972/nx-npm-preset/blob/main/packages/foo/src/lib/foo.ts)

import { core } from '@nx-npm-preset/core';

export function foo(): string {
  core();
  return 'foo';
}

Run:

yarn nx run foo:build

See the error in Current Behavior

Failure Logs

See the error in Current Behavior

Environment


 >  NX   Report complete - copy this into the issue template

   Node : 16.15.1
   OS   : darwin arm64
   yarn : 1.22.19

   nx : 14.3.5
   @nrwl/angular : Not Found
   @nrwl/cypress : Not Found
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.3.5
   @nrwl/eslint-plugin-nx : 14.3.5
   @nrwl/express : Not Found
   @nrwl/jest : 14.3.5
   @nrwl/js : 14.3.5
   @nrwl/linter : 14.3.5
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : 14.3.5
   @nrwl/nx-cloud : Not Found
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : Not Found
   @nrwl/web : Not Found
   @nrwl/workspace : 14.3.5
   typescript : 4.7.3
   ---------------------------------------
   Community plugins:

✨  Done in 0.83s.

wizardnet972 avatar Jun 16 '22 13:06 wizardnet972

Facing same issue.

codewaseem avatar Jun 20 '22 05:06 codewaseem

Facing same issue too.

askides avatar Jun 22 '22 00:06 askides

@codewaseem , @itsrennyman you can vote in the @wizardnet972 post to promote the issue

jon9090 avatar Jun 22 '22 08:06 jon9090

Solution for me here: https://github.com/nrwl/nx/issues/10785#issuecomment-1159017136

sir-captainmorgan21 avatar Jun 24 '22 13:06 sir-captainmorgan21

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it's compile.

Thanks.

For me, this issue can be closed

wizardnet972 avatar Jun 24 '22 14:06 wizardnet972

In my case, I do some refactoring between packages and VSCode automatically add moved file totsconfig.lib.json.

When I remove it - issue gone

Снимок экрана 2022-09-06 в 09 57 31

whalemare avatar Sep 06 '22 02:09 whalemare

Hi! In my case it was related to using dependOn for build task with different cwd. It looks like this executor doesn't like it. I've added full reply here; https://github.com/nrwl/nx/issues/5952#issuecomment-1241975766

ThePlenkov avatar Sep 09 '22 13:09 ThePlenkov

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it's compile.

Thanks.

For me, this issue can be closed

I've tried doing this, but didn't work for me.

For me it's still complaining about a "library" (it's just a set of shared models), it saying that the models (even the non-used ones) need to be part of the root, for any project that is buildable and imports a model

cskiwi avatar Sep 10 '22 09:09 cskiwi

Solution for me here: https://github.com/nrwl/nx/issues/10785#issuecomment-1159017136

For v14.7.5, it works for me even if I only added an empty object for pluginsConfig e.g.

// nx.json
{
  // ...
  "pluginsConfig": {}
}

Very weird. Not sure why.

Also, it would be nice to have a proper solution, rather than having to manually add this in every time we create a new repo

eddeee888 avatar Sep 18 '22 14:09 eddeee888

for me it worked by manually adding the package inside the dependency list (in package.json)

mxvsh avatar Oct 07 '22 02:10 mxvsh

This instructional video claims it is suppose to be implemented at all levels and angles but it fails

https://www.youtube.com/watch?v=weZ7NAzB7PM&t=675s&ab_channel=Nx-Smart%2CFast%2CExtensible

here is my build and all my libs built like this

I cannot get cross lib dependency build coherency

any help is appreciated. thanks in advance

{
    "name": "shared-root-ui-styles",
    "$schema": "../../../../node_modules/nx/schemas/project-schema.json",
    "projectType": "library",
    "sourceRoot": "libs/shared/root/ui-styles/src",
    "prefix": "shared-root-ui-styles",
    "targets": {
        "compile": {
            "executor": "@nrwl/angular:package",
            "outputs": ["{workspaceRoot}/dist/libs/shared/root/ui-styles"],
            "options": {
                "project": "libs/shared/root/ui-styles/ng-package.json"
            },
            "configurations": {
                "production": {
                    "tsConfig": "libs/shared/root/ui-styles/tsconfig.lib.prod.json"
                },
                "development": {
                    "tsConfig": "libs/shared/root/ui-styles/tsconfig.lib.json"
                }
            },
            "defaultConfiguration": "production"
        },
        "build": {
            "executor": "nx:run-commands",
            "dependsOn": ["compile"],
            "options": {
                "tsConfig": "libs/shared/root/ui-styles/tsconfig.lib.json",
                "commands": [
                    {
                        "command": "yarn copy-styles"
                    }
                ]
            }
        },

same thing with ng-packgr-lite

be nice if someone upgraded the nx-examples repo there isn't a single build target for all the libs... mnaybe it never worked there at the time... and still? just sayin

can anyone elaborate?

nhhockeyplayer avatar Nov 20 '22 04:11 nhhockeyplayer

Be nice if we can get some help on this one

Tried with

@nrwl/angular:package
@nrwl/angular:ng-packagr-lite

always produces same result

meanstack02@admin meanstacknh % npx nx build shared-root-ui-styles

> nx run shared-root-ui-styles:compile:production

It looks like all of shared-root-ui-styles's dependencies have not been built yet:
- shared-root-ui-assets

You might be missing a "targetDefaults" configuration in your root nx.json (https://nx.dev/reference/project-configuration#target-defaults),
or "dependsOn" configured in shared-root-ui-styles's project.json (https://nx.dev/reference/project-configuration#dependson)

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target build for project shared-root-ui-styles and 1 task(s) it depends on (2s)
 
    ✖    1/1 failed
    ✔    0/1 succeeded [0 read from cache]

the docs attempt to convey but no real examples depicting this anywhere nor in nx-examples

can anyone help?

I cant think of much else to put inside my Nx.json than this:

    "targetDefaults": {
        "build": {
            "dependsOn": ["compile", "^compile", "build", "^build"],
            "inputs": ["production", "^production"]
        },

Im guessing npmScope is the github account username for me thats 'nhhockeyplayer'

"npmScope": "nhhockeyplayer",

or is it the project name, which for repository should be the same as github scope, it was previously the name of my repository meanstacknh... "npmScope": "meanstacknh", but same result yeilds same?

we rely heavily on shared libs and Nx is the world class authority.

Hoping for best

thanks

p.s. I thought I would just post my Nx.json in case anyone wants to take interest

I have run nx migrate latest repeatedly on latest 15 rev of Nx

{
    "$schema": "./node_modules/nx/schemas/nx-schema.json",
    "npmScope": "nhhockeyplayer",
    "affected": {
        "defaultBase": "main"
    },
    "pluginsConfig": {
        "@nrwl/js": {
            "analyzeSourceFiles": true
        }
    },
    "cli": {
        "packageManager": "yarn",
        "defaultCollection": "@nrwl/angular"
    },
    "tasksRunnerOptions": {
        "default": {
            "runner": "@nrwl/nx-cloud",
            "options": {
                "canTrackAnalytics": false,
                "showUsageWarnings": true,
                "scan": true,
                "cacheableOperations": ["build", "lint", "test", "e2e"],
                "parallel": 1,
                "accessToken": "***********="
            }
        }
    },
    "targetDependencies": {
        "build": [
            {
                "target": "build",
                "projects": "dependencies"
            }
        ]
    },
    "generators": {
        "@nrwl/angular:library": {
            "linter": "eslint",
            "unitTestRunner": "jest",
            "simpleModuleName": true,
            "standaloneConfig": true
        },
        "@nrwl/angular:scam": {
            "style": "scss",
            "changeDetection": "OnPush",
            "flat": true,
            "inlineStyle": true,
            "inlineTemplate": true,
            "skipTests": true,
            "inlineScam": false
        },
        "@nrwl/angular:application": {
            "style": "scss",
            "linter": "eslint",
            "unitTestRunner": "jest",
            "e2eTestRunner": "cypress"
        },
        "@nrwl/angular:component": {
            "style": "scss"
        }
    },
    "defaultProject": "admin",
    "namedInputs": {
        "default": ["{projectRoot}/**/*", "sharedGlobals"],
        "sharedGlobals": [
            "{workspaceRoot}/tsconfig.base.json",
            "{workspaceRoot}/nx.json",
            "{workspaceRoot}/workspace.json"
        ],
        "production": [
            "default",
            "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
            "!{projectRoot}/tsconfig.spec.json",
            "!{projectRoot}/jest.config.[jt]s",
            "!{projectRoot}/.eslintrc.json",
            "!{projectRoot}/.storybook/**/*",
            "!{projectRoot}/**/*.stories.@(js|jsx|ts|tsx|mdx)"
        ]
    },
    "targetDefaults": {
        "build": {
            "dependsOn": ["compile", "^compile", "build", "^build"],
            "inputs": ["production", "^production"]
        },
        "test": {
            "inputs": [
                "default",
                "^production",
                "{workspaceRoot}/jest.preset.js"
            ]
        },
        "lint": {
            "inputs": ["default", "{workspaceRoot}/.eslintrc.json"]
        },
        "build-storybook": {
            "inputs": [
                "default",
                "^production",
                "{workspaceRoot}/.storybook/**/*"
            ]
        },
        "e2e": {
            "inputs": ["default", "^production"]
        }
    }
}

nhhockeyplayer avatar Nov 20 '22 04:11 nhhockeyplayer

this is what we have to resort to

not the kind of software I want to write

    "targets": {
        "deps": {
            "executor": "nx:run-commands",
            "options": {
                "tsConfig": "libs/shared/root/ui-styles/tsconfig.lib.json",
                "commands": [
                    {
                        "command": "nx build shared-root-environment"
                    },
                    {
                        "command": "nx build shared-util-isomorphic"
                    },
                    {
                        "command": "nx build shared-util-models"
                    },
                    {
                        "command": "nx build shared-root-ui-assets"
                    },
                    {
                        "command": "nx build shared-root-ui-styles"
                    }
                ]
            }
        },
        "build": {
            "executor": "@nrwl/angular:ng-packagr-lite",
            "dependsOn": ["deps"],
            "outputs": ["{workspaceRoot}/dist/libs/shared/root/ui-widget"],
            "options": {
                "project": "libs/shared/root/ui-widget/ng-package.json"
            },
            "configurations": {
                "production": {
                    "tsConfig": "libs/shared/root/ui-widget/tsconfig.lib.prod.json"
                },
                "development": {
                    "tsConfig": "libs/shared/root/ui-widget/tsconfig.lib.json"
                }
            },
            "defaultConfiguration": "production"
        },

nhhockeyplayer avatar Nov 20 '22 05:11 nhhockeyplayer

For me it was the missing package.json in the buildable libraries. Added a package.json with name and version and everything worked fine.

egorderg avatar Nov 22 '22 11:11 egorderg

For me it was the missing package.json in the buildable libraries. Added a package.json with name and version and everything worked fine.

Exactly what worked for me just now!

Also you have to make sure the name in package.json doesn't contain more than one "/"

omerfaran avatar Nov 22 '22 15:11 omerfaran

For me the fix was to include "declaration": true,

inside library tsconfig.lib.json

{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "outDir": "../../../dist/out-tsc/shared/utils",
        "declaration": true,
        "types": ["node"]
    },
    "files": [
        "../../../node_modules/@nrwl/react/typings/cssmodule.d.ts",
        "../../../node_modules/@nrwl/react/typings/image.d.ts"
    ],
    "exclude": [
        ".jest-cache",
        "jest.config.ts",
        "**/*.spec.ts",
        "**/*.test.ts",
        "**/*.spec.tsx",
        "**/*.test.tsx",
        "**/*.spec.js",
        "**/*.test.js",
        "**/*.spec.jsx",
        "**/*.test.jsx"
    ],
    "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}

MayGo avatar Feb 03 '23 09:02 MayGo

Hi, I regenerated my libs with the --buildable command and the problem disappeared. I am also pretty sure they were already generated like that, but anyway, it worked. Hope it helps someone

Huncuska avatar Feb 05 '23 18:02 Huncuska

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application.

https://nx.dev/packages/js/executors/tsc#rootdir

baotpham avatar Feb 20 '23 08:02 baotpham

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application.

https://nx.dev/packages/js/executors/tsc#rootdir

This did it for me. I was having this issue with clean newly-created libraries in a monorepo https://github.com/jcguarinpenaranda/test-monorepo-submodules

jcguarinpenaranda avatar Apr 01 '23 17:04 jcguarinpenaranda

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it's compile.

Thanks.

For me, this issue can be closed

In my case, just running nx reset did the trick

devopsmash avatar Apr 10 '23 11:04 devopsmash

In my situation this was the problem:

Using Nx v15.8.9.

I had a library called shared-my-lib,

- apps
- libs
  - shared
     - my-lib

with a tsconfig path alias like @acme/shared/my-lib. This library is a publishable library, so I had it's package.json name property changed to @acme/my-lib.

So the mismatch between

workspace alias: @acme/shared/my-lib and package.json name: @acme/my-lib

made building other libraries depending on it fail with the File is not under 'rootDir' '<...>'. 'rootDir' is expected to contain all source files error.

sebastiandg7 avatar May 02 '23 23:05 sebastiandg7

For me, I had to add

 "rootDir": "."

under options in project.json. Otherwise, it will use the root of the application. https://nx.dev/packages/js/executors/tsc#rootdir

This did it for me. I was having this issue with clean newly-created libraries in a monorepo https://github.com/jcguarinpenaranda/test-monorepo-submodules

This was the fix for me also. A little disconcerting as the fix seems very unintuitive.

danielsharvey avatar May 08 '23 14:05 danielsharvey

@wizardnet972 and anyone else who's facing this issue:

Solution:

  1. nx migrate latest
  2. In nx.json, in targetDefaults you can add
"targetDefaults": {
...
    "build": {
      "dependsOn": ["^build"]
    },
...
}

No need for anything else. Here's the original repo posted here, with solution. Let me know if this helps, or if you think we could close the issue.

mandarini avatar May 15 '23 12:05 mandarini

In my situation this was the problem:

Using Nx v15.8.9.

I had a library called shared-my-lib,

- apps
- libs
  - shared
     - my-lib

with a tsconfig path alias like @acme/shared/my-lib. This library is a publishable library, so I had it's package.json name property changed to @acme/my-lib.

So the mismatch between

workspace alias: @acme/shared/my-lib and package.json name: @acme/my-lib

made building other libraries depending on it fail with the File is not under 'rootDir' '<...>'. 'rootDir' is expected to contain all source files error.

I had the same problem. When you generate a library, it creates a path alias on tsconfig.base.json as @project/folder/lib-name, so I had to remove the /folder/ part from the path alias and update the name property on the library's package.json to @project/lib-name. You saved me, I was stuck on this for days.

Also, if someone is facing the same problem, remember to define "composite": true and "declaration": true on the library's tsconfig.json

julianlbs avatar May 17 '23 21:05 julianlbs

I am facing this problem on workspace extensions when creating a generator. For libs the rootDir worked, but for the generator it doesnot accepts. Anybody had the same proble with generator?

schirrel avatar May 22 '23 16:05 schirrel

@sir-captainmorgan21 Yes the solution is working for me.

I clone the repo then I add analyzeSourceFiles and targetDefaults and run nx reset and now it's compile.

Thanks.

For me, this issue can be closed

This one was working for me. I added the following to nx.json:

"pluginsConfig": {
    "@nx/js": {
      "analyzeSourceFiles": false
    }
 }

Has anyone an idea why this is solving the issue?

nicoschuck avatar Jun 01 '23 21:06 nicoschuck

Just for information, I had to add this to the nx.json file.

nx.json

{
  "targetDefaults": {
    // ...
    "build": {
      "dependsOn": ["^build"]
    },
    // ...
  },
  // ...
  "pluginsConfig": {
    // ...
    "@nx/js": {
      "analyzeSourceFiles": true
    }
    // ...
  }
}

olivbau avatar Jun 06 '23 16:06 olivbau

I also needed what @olivbau and @nicoschuck have done. And I only need it when deploying an app that uses a library (bit of a complex deploy via sst). Building is fine. I'm on nx 16.3.2.

The other mentioned solutions with rootDir did not work. And yes, I have depends "^build", that's default these days in nx.json.

dnz-bdeboer avatar Jun 07 '23 00:06 dnz-bdeboer

Setting the rootDir with . works, but I find another way to resolve it. Add the below config in nx.json

{
  "pluginsConfig": {
    "@nrwl/js": {
      "analyzeSourceFiles": true
    }
  }
}

nnsay avatar Jun 09 '23 10:06 nnsay

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[bot] avatar Jul 10 '23 00:07 github-actions[bot]