Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weird Behavior in NestJS Generators #29076

Closed
1 of 4 tasks
bzp2010 opened this issue Nov 26, 2024 · 6 comments · Fixed by #29084
Closed
1 of 4 tasks

Weird Behavior in NestJS Generators #29076

bzp2010 opened this issue Nov 26, 2024 · 6 comments · Fixed by #29084

Comments

@bzp2010
Copy link

bzp2010 commented Nov 26, 2024

Current Behavior

The NestJS generator is used in a way that doesn't match the documentation and is very different from previous releases.

According to my #28910 (Additional Information), and another reporter's checks (#28910 (comment)), this does exist.

@pawel-twardziak also proves (#28910 (comment)) that these generators are not quite normal, at least, it doesn't match the documentation. It also doesn't work as well as the previous implementation in Nx 19. As mentioned there, I'd better create a new issue to keep track.

In detail: #28910

I would kindly ask you to indicate the correct way to use the nestjs generator, as it is very different from the way I used it during nx 16-19.
I know we've switched to the default as-provided, but it also looks like the nestjs generator doesn't support directory but uses path instead.

I've read https://nx.dev/deprecated/as-provided-vs-derived#generate-paths-and-names-asprovided, but it doesn't look like that's what actually works, which I don't understand.

For example, when I try to generate code for a project in the root directory, I use the --path option and the files are generated in the wrong path.

➜  my-workspace git:(main) ✗ nx g @nx/nest:guard --name auth --path apps/my-workspace/src/app

 NX  Generating @nx/nest:guard

CREATE apps/my-workspace/src/apps/my-workspace/src/app/app.guard.spec.ts
CREATE apps/my-workspace/src/apps/my-workspace/src/app/app.guard.ts

It is duplicated in the apps/my-workspace/src section and I have to move it manually which I think might be wrong, it should be able to work correctly, in the past I used --directory to deal with the problem directly, but now it seems that it no longer exists.

I saw that section included in --path and maybe it was the cause of the error, so I went into that folder and generated it again.

➜  my-workspace git:(main) ✗ cd apps/my-workspace/src 
➜  src git:(main) ✗ nx g @nx/nest:guard --name auth --path app                      

 NX  Generating @nx/nest:guard

CREATE apps/my-workspace/src/app/app.guard.spec.ts
CREATE apps/my-workspace/src/app/app.guard.ts

Now, the generated path is correct, but again, I noticed that the generated file names app.guard.ts and classes AppGuard in them do not follow the input value auth, but appear to be automatically inferred from the path app. It's not just guard, but other nestjs code generators have similar problems, such as controllers and filters.

If I want to make sure the filename is correct, I have to make sure that the last part of the path in --path is the filename prefix I want. Just like:

nx g @nx/nest:guard --name auth --path app/auth
nx g @nx/nest:guard --name auth --path auth

But unfortunately the two methods above fall back into the cycle of problems about paths mentioned above.

➜  src git:(main) ✗ nx g @nx/nest:guard --name auth --path app/auth

 NX  Generating @nx/nest:guard

CREATE apps/my-workspace/src/app/app/auth/auth.guard.spec.ts
CREATE apps/my-workspace/src/app/app/auth/auth.guard.ts

➜  src git:(main) ✗ nx g @nx/nest:guard --name auth --path auth    

 NX  Generating @nx/nest:guard

CREATE apps/my-workspace/src/auth/auth.guard.spec.ts
CREATE apps/my-workspace/src/auth/auth.guard.ts

It looks like I have to enter the app directory to generate the file at auth/auth.guard.ts.

This breaks the intuition of the previous generator's use of --directory, and --path forces me to infer where I should be generating files and return the up-level directory in that location.

Also, I don't want to place the generated files inside a directory, well, at least not every time, e.g. I want to generate auth.guard.ts instead of auth/auth.guard.ts, which, if memory serves me correctly, was the behavior when I used --directory before.

Expected Behavior

It is desirable that these generators continue to support the --directory option that was supported in Nx19 and earlier, which is the most intuitive to use. The current --path option is extremely unintuitive.

nx --version

Nx Version:
- Local: v19.3.0
- Global: v20.1.0

Back then, I used commands like:

nx g @nx/nest:guard --name auth --directory apps/[hidden]/src/app/config

 NX  Generating @nx/nest:guard

? Where should the guard be generated? … 
▸ As provided: apps/[hidden]/src/app/config/auth.ts
  Derived:     apps/[hidden]/src/apps/[hidden]/src/app/config/auth.ts

Of course, I'm using the As-provided pattern, so it generates:

CREATE apps/[hidden]/src/app/config/auth.guard.spec.ts
CREATE apps/[hidden]/src/app/config/auth.guard.ts

No misplaced paths, no duplicate paths, no weird nested folders, everything is fine.
This is exactly what I expected, nothing that forces me to have to guess and infer paths, and the generated files are not nested by their folder prefixes. The files are generated in exactly the folder I specify, which is fairly intuitive.

GitHub Repo

No response

Steps to Reproduce

  1. Using the nestjs generator in Nx20, try to generate a Guard (including, but not limited to, a Guard, which behaves like a Controller or Service).
  2. As I mentioned above, the Nx 19 is very intuitive and easy to use, of course, it works fine.

Nx Report

Node           : 20.13.1
OS             : linux-x64
Native Target  : x86_64-linux
npm            : 10.5.2

nx (global)        : 20.1.0
nx                 : 20.1.0
@nx/js             : 20.1.0
@nx/jest           : 20.1.0
@nx/eslint         : 20.1.0
@nx/workspace      : 20.1.0
@nx/eslint-plugin  : 20.1.0
@nx/nest           : 20.1.0
@nx/node           : 20.1.0
@nx/web            : 20.1.0
@nx/webpack        : 20.1.0
typescript         : 5.5.4
---------------------------------------
Registered Plugins:
@nx/webpack/plugin
@nx/eslint/plugin
@nx/jest/plugin

Failure Logs

Package Manager Version

pnpm 9.14.1

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

This issue is derived from another issue #28910. I've transferred as much of the relevant background and errors as I can here, and if not here, there may be more information there. If information is still missing, I will add it.

@pawel-twardziak
Copy link
Contributor

thanks @bzp2010 for following up and reporting that issue. Will work on it, something should get clarified within ~1 week.

1 similar comment
@pawel-twardziak
Copy link
Contributor

thanks @bzp2010 for following up and reporting that issue. Will work on it, something should get clarified within ~1 week.

@pawel-twardziak
Copy link
Contributor

there is a bug on Github :D it doubles comments 😆

@ndcunningham
Copy link
Contributor

Hi, You are correct.
When we transitioned to use --path instead of --project it created this issue.

It will be fixed stay tuned for the PR, thanks for reporting!

ndcunningham added a commit that referenced this issue Nov 26, 2024
ndcunningham added a commit that referenced this issue Nov 26, 2024
ndcunningham added a commit that referenced this issue Nov 27, 2024
ndcunningham added a commit that referenced this issue Nov 27, 2024
ndcunningham added a commit that referenced this issue Nov 27, 2024
ndcunningham added a commit that referenced this issue Nov 27, 2024
ndcunningham added a commit that referenced this issue Nov 27, 2024
…licate when provided (#29084)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
When we run the nest generators (interceptor, resource, guard etc...) it
tends to duplicate the path and leads to unexpected folder creations.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
It should generate in the path provided.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #29076
FrozenPandaz pushed a commit that referenced this issue Nov 28, 2024
…licate when provided (#29084)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->
When we run the nest generators (interceptor, resource, guard etc...) it
tends to duplicate the path and leads to unexpected folder creations.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->
It should generate in the path provided.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #29076

(cherry picked from commit 25c5643)
@bzp2010
Copy link
Author

bzp2010 commented Nov 29, 2024

I have started using Nx 20.1.4 and now these generators are working fine. I've noticed that name is no longer used and instead path is used to infer the file name and class name prefix, which I used on Nx 19.

Anyway, it works fine now.

Copy link

github-actions bot commented Jan 1, 2025

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 github-actions bot locked as resolved and limited conversation to collaborators Jan 1, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants