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

Swagger Plugin breaks builds when multiple source directories exist #501

Closed
DenzoNL opened this issue Jan 21, 2020 · 2 comments
Closed

Swagger Plugin breaks builds when multiple source directories exist #501

DenzoNL opened this issue Jan 21, 2020 · 2 comments

Comments

@DenzoNL
Copy link

DenzoNL commented Jan 21, 2020

I'm submitting a...


[ ] Regression 
[x ] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

When building the application with the Swagger plugin enabled in nest-cli.json, the paths to types in the node_modules folder are semi-hardcoded to go up one folder from the root of the compiled files.

In a fresh NestJS project this works fine, because the dist/ directory is where the compiled files live. However, when you have additional folders with source files besides src/ that are compiled, dist will look something like this:

dist/
    src/
    migration/

If this is the case, then the build breaks when returning types that are imported from external packages in the node_modules folder. Consider the following example:

// src/photo/photo.controller.ts
import { Controller Delete, Param } from '@nestjs/common';
import { PhotoService } from './photo.service';
import { DeleteResult } from 'typeorm';

@Controller('photo')
export class PhotoController {
    constructor(protected readonly service: PhotoService) {}

    @Delete('/:id')
    async deletePhoto(@Param('id') id: number): Promise<DeleteResult> {
        return this.service.delete(id);
    }
}

This results in the following call in the compiled file:

// dist/src/photo/photo.controller.js
__decorate([
    common_1.Delete('/:id'),
    openapi.ApiResponse({ status: 200, type: require("../../node_modules/typeorm/query-builder/result/DeleteResult").DeleteResult }),
    __param(0, common_1.Param('id')),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [Number]),
    __metadata("design:returntype", Promise)
], PhotoController.prototype, "deletePhoto", null);

As you can see, the require call only goes up two directories to the dist/ folder,
where node_modules/ is not present, obviously.

This results in the following error when running NestJS:

Error: Cannot find module '../../node_modules/typeorm/query-builder/result/DeleteResult'
Require stack:
- /home/denzo/Code/nest-swagger-typeorm-repro/dist/src/photo/photo.controller.js
- /home/denzo/Code/nest-swagger-typeorm-repro/dist/src/photo/photo.module.js
- /home/denzo/Code/nest-swagger-typeorm-repro/dist/src/app.module.js
- /home/denzo/Code/nest-swagger-typeorm-repro/dist/src/main.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:973:15)
    at Function.Module._load (internal/modules/cjs/loader.js:855:27)
    at Module.require (internal/modules/cjs/loader.js:1033:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/denzo/Code/nest-swagger-typeorm-repro/dist/src/photo/photo.controller.js:38:46)
    at Module._compile (internal/modules/cjs/loader.js:1144:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1164:10)
    at Module.load (internal/modules/cjs/loader.js:993:32)
    at Function.Module._load (internal/modules/cjs/loader.js:892:14)
    at Module.require (internal/modules/cjs/loader.js:1033:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/denzo/Code/nest-swagger-typeorm-repro/dist/src/photo/photo.controller.js',
    '/home/denzo/Code/nest-swagger-typeorm-repro/dist/src/photo/photo.module.js',
    '/home/denzo/Code/nest-swagger-typeorm-repro/dist/src/app.module.js',
    '/home/denzo/Code/nest-swagger-typeorm-repro/dist/src/main.js'
  ]
}

If the plugin is disabled, or any additional directories besides src/ are ignored in the tsconfig, the code will build and run correctly.

Expected behavior

The preferred solution would be for the imports from node_modules/ to be handled correctly in any case, and not reliant on the compiled source files to be in the root of dist.

If this is not possible, some additional documentation to prevent this from happening would be preferred.

Minimal reproduction of the problem with instructions

I have set up a minimal reproduction repository here:

https://github.com/DenzoNL/nestjs-swagger-plugin-repro

What is the motivation / use case for changing the behavior?

Whether or not it is best practice to have multiple directories being compiled into the dist/ folder, it's a very common case, and this error is very vague and mysterious for any beginners encountering this. However, I don't think a minor variation in directory structure should be a problem when using the Swagger plugin.

Environment


Nest version: 6.7.2 (reproduction repo), 6.10.14 (my production environment where I encountered it).

NestJS Swagger version: 4.1.14
 
For Tooling issues:
- Node version: 13.6.0  
- Platform:  Linux 
@kamilmysliwiec
Copy link
Member

This should be fixed in 4.1.15 release.

@lock
Copy link

lock bot commented Apr 25, 2020

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

@lock lock bot locked as resolved and limited conversation to collaborators Apr 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants