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

emitDecoratorMetadata workaround #2

Closed
yassinebridi opened this issue Apr 14, 2021 · 13 comments
Closed

emitDecoratorMetadata workaround #2

yassinebridi opened this issue Apr 14, 2021 · 13 comments

Comments

@yassinebridi
Copy link

First of all thank you for this wonderful package, the speed difference is insane.
It almost complied for me, until this error occured

UnhandledPromiseRejectionWarning: TypeError: Cannot read property '1' of undefined
    at extractTypeIfArray (/home/yaslix/projects/vp/node_modules/@nestjs/graphql/dist/utils/reflection.utilts.js:32:29)
    at Object.reflectTypeFromMetadata (/home/yaslix/projects/vp/node_modules/@nestjs/graphql/dist/utils/reflection.utilts.js:11:26)

I'm not sure if this is due to esbuild not supporting emitDecoratorMetadata.
This problem occurs exactly with this kind of code:

@Query(() => String)
 getOneSettings(@Args() name: string) {
   return name;
 }

If i remove the @Args() name: string it works properly.
@Args is coming from @nestjs/graphql

@axe-me
Copy link
Owner

axe-me commented Apr 14, 2021

Hi @yassinebridi , to use this with nestjs you have to set the server type in the plugin config:

server: "nest"

under the hood for nest, this plugin is using swc to transpile ts file in order to support emitDecoratorMetadata

please try that and let me know if it works.

Cheers.

@yassinebridi
Copy link
Author

@axe-me That's what i did with the ts transplier set to esbuild.
While the swc option get exited with a SIGKILL

@axe-me
Copy link
Owner

axe-me commented Apr 14, 2021

@yassinebridi can you please provide the error log? Esbuild won't work for nest.

@yassinebridi
Copy link
Author

The error log isn't really helpful, i get this:

error Command failed with signal "SIGILL".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
error Command failed.
Exit code: 1
Command: /home/yaslix/.local/share/nvm/versions/node/v14.16.1/bin/node
Arguments: /usr/lib/node_modules/yarn/lib/cli.js start:dev:vite
Directory: /home/yaslix/projects/vp/packages/api
Output:

info Visit https://yarnpkg.com/en/docs/cli/workspace for documentation about this command.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

This is my vite.config.js file:

import { VitePluginNode } from 'vite-plugin-node';
/**
 * @type {import('vite').UserConfig}
 */
const config = {
  plugins: [
    ...VitePluginNode({
      server: 'nest',
      appPath: './src/main.ts',
      port: 3000,
      tsCompiler: 'swc',
    }),
  ],
};

export default config;

And this is my main.ts file:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

export const createViteNodeApp = NestFactory.create(AppModule);

And this is my packages.json file:

{
  "dependencies": {
    "@anatine/esbuild-decorators": "^0.2.7",
    "@nestjs/common": "^7.6.15",
    "@nestjs/config": "^0.6.3",
    "@nestjs/core": "^7.6.15",
    "@nestjs/graphql": "^7.10.3",
    "@nestjs/jwt": "^7.2.0",
    "@nestjs/passport": "^7.1.5",
    "@nestjs/platform-express": "^7.6.15",
    "@prisma/client": "^2.20.1",
    "@vp/common": "*",
    "apollo-server-express": "^2.22.2",
    "bcrypt": "^5.0.1",
    "chalk": "^4.1.0",
    "chokidar": "^3.5.1",
    "cookie-parser": "^1.4.5",
    "esbuild": "^0.11.10",
    "esbuild-node-externals": "^1.1.0",
    "graphql": "^15.5.0",
    "graphql-redis-subscriptions": "^2.3.1",
    "graphql-type-json": "^0.3.2",
    "ioredis": "^4.26.0",
    "lodash": "^4.17.21",
    "passport": "^0.4.1",
    "passport-facebook": "^3.0.0",
    "passport-google-oauth20": "^2.0.0",
    "passport-jwt": "^4.0.0",
    "pg": "^8.5.1",
    "prisma-nestjs-graphql": "^11.0.3",
    "reflect-metadata": "^0.1.13",
    "remove": "^0.1.5",
    "rimraf": "^3.0.2",
    "rxjs": "^6.6.7"
  },
  "devDependencies": {
    "@anatine/esbuild-decorators": "^0.2.7",
    "@nestjs/cli": "7.5.4",
    "@nestjs/schematics": "^7.3.1",
    "@types/express": "^4.17.11",
    "@types/graphql-type-json": "^0.3.2",
    "@types/ioredis": "^4.22.3",
    "@types/node": "^14.14.37",
    "@types/passport-jwt": "^3.0.5",
    "@typescript-eslint/eslint-plugin": "^4.21.0",
    "@typescript-eslint/parser": "^4.21.0",
    "chalk": "^4.1.0",
    "chokidar": "^3.5.1",
    "esbuild": "^0.11.10",
    "esbuild-node-externals": "^1.1.0",
    "eslint": "^7.24.0",
    "eslint-config-prettier": "^8.1.0",
    "eslint-plugin-prettier": "^3.3.1",
    "prettier": "^2.2.1",
    "prisma": "^2.20.1",
    "ts-loader": "^8.1.0",
    "ts-node": "^9.1.1",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.2.4",
    "vite": "^2.1.5",
    "vite-plugin-node": "^0.0.7"
  }
}

@axe-me
Copy link
Owner

axe-me commented Apr 15, 2021

@yassinebridi I just tried the plugin on a freshly created nest project. Everything works fine for me. Can you please create a minimum repro for this if possible?

@yassinebridi
Copy link
Author

@axe-me While using yarn doesn't throw any descriptive errors, using npm gives this:

Illegal hardware instruction (core dumped)

I'm not sure if something needs to be compiled for each CPU architecture.

This is my cat /proc/cpuinfo

processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 58
model name      : Intel(R) Core(TM) i3-3110M CPU @ 2.40GHz
stepping        : 9
microcode       : 0x21
cpu MHz         : 1909.582
cache size      : 3072 KB
physical id     : 0
siblings        : 4
core id         : 0
cpu cores       : 2
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer xsave avx f16c lahf_lm cpuid_fault epb pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase smep erms xsaveopt dtherm arat pln pts md_clear flush_l1d
vmx flags       : vnmi preemption_timer invvpid ept_x_only flexpriority tsc_offset vtpr mtf vapic ept vpid unrestricted_guest
bugs            : cpu_meltdown spectre_v1 spectre_v2 spec_store_bypass l1tf mds swapgs itlb_multihit
bogomips        : 4790.34
clflush size    : 64
cache_alignment : 64
address sizes   : 36 bits physical, 48 bits virtual
power management:

processor       : 1

Can you check if this package is working on another CPU architecture.

@axe-me
Copy link
Owner

axe-me commented Apr 16, 2021

Without see a reproduce I cant really tell what's the issue is. However, since you are using graphQL, I guess it could be related to the circular dependency problem in Vite. see : vitejs/vite#2491 and vitejs/vite#2258

until that gets resolved, I cant do much in the plugin.

@yassinebridi
Copy link
Author

No my last comment was about a completely new NestJS project, just go scaffolded with the Nest CLI, no GraphQL.
That's why i didn't make a reproducible repo.

From what i can deduct from searching for that error, it seems that a package used in vite-plugin-node, or vite-plugin-node itself, doesn't work with my CPU architecture, that's why i suggested you test on a new CPU arch, or have someone else test it.

@axe-me
Copy link
Owner

axe-me commented Apr 16, 2021

Interesting...
the main dependency of this project are vite and swc, can you try if swc it alone runs on your computer:
npx swc [some ts file]

@yassinebridi
Copy link
Author

yassinebridi commented Apr 16, 2021

Yep you are right, it's swc's issue.
I will make an issue there instead.

@Tnifey
Copy link

Tnifey commented Apr 16, 2021

@yassinebridi Is that code run with nest start without vite and without error? Is swc compiles your code without error?

What kind of args, are you referring to in this code?

@Query(() => String)
getOneSettings(@Args() name: string) {
   return name;
}

Is it working with

getOneSettings(@Args("name") name: string) {
   return name;
}

? If not, what error it throws?

Isn't that @Args decorator should get name of argument or typescript metadata with following object type from typescript to perform catching this type (?) e.g.:

@ArgsType()
class GetOneSettingsArgs {
   @Field()
   name: string;
}

// .... in resolver

getOneSettings(@Args() withName: GetOneSettingArgs) {
   return withName.name; // as string
}

Sorry for that amount of questions, but I want to make sure that is really fault of vite-plugin-node and not swc or nestjs :P
CAN you make minimum viable code to reproduce this error on your machine? And if you can, can you post it on github for everyone to see that first hand?

@axe-me speed of your plugin makes mine heart grow :P thank you

@Tnifey
Copy link

Tnifey commented Apr 16, 2021

Yeah, I should have check if it's closed already before posting questions.

@yassinebridi
Copy link
Author

I opened an issue in swc's repo, i really hope there is a quick fix for this issue, because i can't wait to use this package too, the speed difference in insanely huge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants