nx-dotnet / nx-dotnet

A Nx plugin adding support for .NET 5+ (or .net core) projects, featuring full project graph and generator support.
https://www.nx-dotnet.com/
MIT License
263 stars 63 forks source link

[BUG] Generate lib doesn't complete. #474

Closed paul-hammant closed 2 years ago

paul-hammant commented 2 years ago
  1. Clone repo with existing .NET library solution/proj. One that used dotnet test as it's build toolchain.

  2. Run npx nx g @nx-dotnet/core:lib my-lib within (per the README)

√ Which language should the project use? · C#
√ Which template should be used for creating the tests project · nunit

>  NX   Cannot find nx.json

Now, there's no mention of nx.json in the readme as an important step in the generation of a lib.

Hacker me makes it with nano/vim with contents of { } and re-runs.

  1. Run npx nx g @nx-dotnet/core:lib my-lib
>  NX  Generating @nx-dotnet/core:library

√ Which language should the project use? · C#
√ Which template should be used for creating the tests project · nunit

Tool Manifest created for managing local .NET tools

Executing Command: dotnet "new" "tool-manifest"

The template "Dotnet local tool manifest file" was created successfully.

? What template should the project be initialized with? (https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-new#template-options)  classlib

Executing Command: dotnet "new" "classlib" "--language" "C#" "--name" ".MyLib" "--output" "libs/my-lib"

The template "Class Library" was created successfully.

Processing post-creation actions...

Running 'dotnet restore' on C:\Users\phammant\path\to\repo\existing_project_dir\libs\my-lib\.MyLib.csproj...

  Determining projects to restore...

  Restored C:\Users\phammant\path\to\repo\existing_project_dir\libs\my-lib\.MyLib.csproj (in 62 ms).

Restore succeeded.

Executing Command: dotnet "new" "nunit" "--language" "C#" "--name" ".MyLib.Test" "--output" "libs/my-lib-test"

The template "NUnit 3 Test Project" was created successfully.

Processing post-creation actions...

Running 'dotnet restore' on C:\Users\phammant\path\to\repo\existing_project_dir\libs\my-lib-test\.MyLib.Test.csproj...

  Determining projects to restore...

  Restored C:\Users\phammant\path\to\repo\existing_project_dire\libs\my-lib-test\.MyLib.Test.csproj (in 323 ms).

Restore succeeded.

>  NX   Unable to find a build-able project within project's source directory!

   Pass --verbose to see the stacktrace.

I would've though a generate target wouldn't need to "find" a build-able project within a directory it was allegedly generating.

AgentEnder commented 2 years ago

@paul-hammant Can you provide a repo to reproduce this problem? The output of nx report would also be helpful.

paul-hammant commented 2 years ago

https://github.com/paul-hammant/TestProjectForNxDotNetIssue Is the repo I have just made for this issue.

paul-hammant commented 2 years ago

Now, some new problems with that (Mac)

npm install --save-dev @nx-dotnet/core
npx nx g @nx-dotnet/core:lib my-lib

Complains about missing JSON file, offers three solutions, No. 3 picked:

npx nx init 

Causes:

npm ERR! peer dep missing: @nrwl/devkit@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! peer dep missing: @nrwl/workspace@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! peer dep missing: @nrwl/tao@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! extraneous: eslint-visitor-keys@2.1.0 /path/to/TestProjectForNxDotNetIssue/node_modules/eslint-utils/node_modules/eslint-visitor-keys
npm ERR! extraneous: glob-parent@5.1.2 /path/to/TestProjectForNxDotNetIssue/node_modules/fast-glob/node_modules/glob-parent
npm ERR! extraneous: type-fest@0.21.3 /path/to/TestProjectForNxDotNetIssue/node_modules/ansi-escapes/node_modules/type-fest
npm ERR! extraneous: escape-string-regexp@1.0.5 /path/to/TestProjectForNxDotNetIssue/node_modules/figures/node_modules/escape-string-regexp
nx init

Adds nx.json file and installs nx if not installed already

Options:
  --help     Show help                                                                                                                                          [boolean]
  --version  Show version number                                                                                                                                [boolean]

Error: Command failed: npm ls
npm ERR! peer dep missing: @nrwl/devkit@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! peer dep missing: @nrwl/workspace@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! peer dep missing: @nrwl/tao@>13.4.0-beta.1, required by @nx-dotnet/core@1.12.0
npm ERR! extraneous: eslint-visitor-keys@2.1.0 /path/to/TestProjectForNxDotNetIssue/node_modules/eslint-utils/node_modules/eslint-visitor-keys
npm ERR! extraneous: glob-parent@5.1.2 /path/to/TestProjectForNxDotNetIssue/node_modules/fast-glob/node_modules/glob-parent
npm ERR! extraneous: type-fest@0.21.3 /path/to/TestProjectForNxDotNetIssue/node_modules/ansi-escapes/node_modules/type-fest
npm ERR! extraneous: escape-string-regexp@1.0.5 /path/to/TestProjectForNxDotNetIssue/node_modules/figures/node_modules/escape-string-regexp

    at checkExecSyncError (child_process.js:790:11)
    at execSync (child_process.js:863:15)
    at Object.initHandler (/Users/paul/.npm/_npx/35411/lib/node_modules/nx/src/command-line/init.js:10:58)
    at /Users/paul/.npm/_npx/35411/lib/node_modules/nx/src/command-line/nx-commands.js:187:140
    at Generator.next (<anonymous>)
    at fulfilled (/Users/paul/.npm/_npx/35411/lib/node_modules/nx/node_modules/tslib/tslib.js:115:62) {
  status: 1,
  signal: null,
  output: [
    null,
    <Buffer 2f 56 6f 6c 75 6d 65 73 2f 4f 48 44 2f 73 63 6d 2f 6f 73 73 2f 54 65 73 74 50 72 6f 6a 65 63 74 46 6f 72 4e 78 44 6f 74 4e 65 74 49 73 73 75 65 0a e2 ... 7317 more bytes>,
    <Buffer 6e 70 6d 20 45 52 52 21 20 70 65 65 72 20 64 65 70 20 6d 69 73 73 69 6e 67 3a 20 40 6e 72 77 6c 2f 64 65 76 6b 69 74 40 3e 31 33 2e 34 2e 30 2d 62 65 ... 802 more bytes>
  ],
  pid: 35418,
  stdout: <Buffer 2f 56 6f 6c 75 6d 65 73 2f 4f 48 44 2f 73 63 6d 2f 6f 73 73 2f 54 65 73 74 50 72 6f 6a 65 63 74 46 6f 72 4e 78 44 6f 74 4e 65 74 49 73 73 75 65 0a e2 ... 7317 more bytes>,
  stderr: <Buffer 6e 70 6d 20 45 52 52 21 20 70 65 65 72 20 64 65 70 20 6d 69 73 73 69 6e 67 3a 20 40 6e 72 77 6c 2f 64 65 76 6b 69 74 40 3e 31 33 2e 34 2e 30 2d 62 65 ... 802 more bytes>
}

I carry on regardless. Hacker me makes the nx.json file by hand again.

$ npx nx g @nx-dotnet/core:lib my-lib

 >  NX   Could not find Nx modules in this workspace.
AgentEnder commented 2 years ago

As noted in the readme, one of the prerequisites for nx-dotnet is that you are operating inside of an Nx workspace already. To get started with this, you can create a workspace with npx create-nx-workspace.

Can you try inside an Nx workspace and see if you s till encounter issues?

paul-hammant commented 2 years ago

Thanks for the reply.

Looking at the suggestions offered by npx create-nx-workspace, I don't know which one to choose for a C# library with no UI and no web that will may to nuget not npm:

apps [an empty workspace with no plugins with a layout that works best for building apps] core [an empty workspace with no plugins set up to publish npm packages (similar to yarn workspaces)] ts [an empty workspace with the JS/TS plugin preinstalled] react [a workspace with a single React application] angular [a workspace with a single Angular application] next.js [a workspace with a single Next.js application] nest [a workspace with a single Nest application] express [a workspace with a single Express application] web components [a workspace with a single app built using web components] react-native [a workspace with a single React Native application] react-express [a workspace with a full stack application (React + Express)] angular-nest [a workspace with a full stack application (Angular + Nest)]

Advice?

paul-hammant commented 2 years ago

I've chosen the first open and seen the command through to completion.

I the tried the nx-dotnet setup command again.

npx nx g @nx-dotnet/core:lib my-lib

 >  NX   Could not find Nx modules in this workspace.

I tried npm install, then the same command again getting the same result.

AgentEnder commented 2 years ago

Did you cd into your new workspace before running the command?

paul-hammant commented 2 years ago

Oh. I see what you're saying. This is very complicated for a migration chore.

Maybe I should start over.

  1. Create a fresh nothing-in-it nx-dotnet workspace
  2. Use git-mv to move sets of existing source C# (etc) files into the workspace.
  3. Confirm remainder of old directory structure.
paul-hammant commented 2 years ago

OK, all done. Seven or so commits - https://github.com/paul-hammant/TestProjectForNxDotNetIssue/commits/master

My understanding gap obviously. You have to create a NEW nx-dotnet project(s), then move in your legacy source code to fit that directory structure. Nx is opinionated so you have to stick with that directory structure as you carry on.

In short, migration of existing is SECONDARY to nx-dotnet setup for a buildable/testable .NET thing.

Key setup was:

npm install --save-dev @nx-dotnet/core
npx nx init 
cd workspace
npm install 
npx nx g @nx-dotnet/core:lib my-lib
github-actions[bot] commented 1 year ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.