nrwl / add-nx

22 stars 8 forks source link

NX cannot locate projects/packages without a scaffolded workspace.json file #36

Open arizonatribe opened 2 years ago

arizonatribe commented 2 years ago

Summary

NX is not able to locate project/packages in a monorepo to which it was added (via this add-nx-to-monorepo generator).

Details

The add-nx-to-monorepo recently removed the scaffolding of the workspace.json, however the latest nx CLI doesn't (yet) seem to be able to recognize nested packages/projects without that file.

The workspace.json did make it so that project names can be mapped to folder locations in a monorepo, however this is likely consolidated now into the nx.json.

My expectation was the nx CLI was modified so that it would be able to recognize projects without that explicit mapping (probably traversing folders and searching for nested package.json and/or project.json files?). However the latest version of nx doesn't seem to be able to locate projects without it.

I did update to the latest of nx (via yarn global upgrade) and confirmed afterwards it's the latest via npm view nx.

Steps to Reproduce

To reproduce easily, here is a public, open-source repository I've been migrating from Lerna to NX (feel free to clone and use):

I'll outline the steps I took and hopefully will make it easier for you to assist or give me any homework.

  1. Removed the package-lock.json and node_modules/ - rm -rf node_modules package-lock.json
  2. Switched to yarn and create the lock file- yarn install
  3. Add NX to the repo - npx add-nx-to-monorepo
  4. Added the new JS/TS generator - yarn add --dev @nrwl/js
  5. Create a new (buildable) TS package - yarn nx g @nrwl/js:lib scalars --buildable
  6. Run (any) NX command on that package/project - nx test scalars or nx build scalars
Could not find project "scalars"

Questions

I'm not sure if there is an issue with this generator or with NX or rather if there's a mistake I'm making which you can help point out. Maybe I even need to open/move this issue to another repo. Regardless, I'm happy to assist or create a PR if you can point me in the right direction.

These are my (speculation) questions:


NX version: 13.4.1 Yarn version: 1.22.0 Node version: v14.17.0 OS version: Fedora v35

arizonatribe commented 2 years ago

I was tinkering around a bit this morning and discovered something that might help pin down the issue. Basically it seems that nx isn't looking at a packages/ folder the same way it's looking at a libs/ or apps/ folder.

In those Steps to Reproduce I described, you'll end up with a packages/scalars/ folder and a project.json properly configured. However the output for nx build scalars or nx test scalars indicates that nx isn't finding that project.json. Although adding in a workspace.json to the root of the repo can fix it, that isn't the point.

The experiment was I created a libs/ folder and copied the packages/scalars/ into it as-is. And NX was able to find it and execute the nx test scalars just fine.

So it seems that the underlying cause might be the packages/ directory isn't one of the few that NX checks when there is no workspace.json. Does that help?

arizonatribe commented 2 years ago

Follow-up to the follow-up :smile:

Looking through the source code the devkit package seems the schema for the nx.json file defines this:

  /**
   * Where new apps/libs should be placed
   */
  workspaceLayout: {
    libsDir: string;
    appsDir: string;
  }

When I set my nx.json like this it works:

  "workspaceLayout": {
    "libsDir": "packages"
  }

However the default workspace preset for an NX generated NPM lib should have that same setting. It seems that it might not be read-in early enough (or everywhere it's needed) in @nrwl/tao. I think that would be the spot for the fix.