Cammisuli / monodon

39 stars 17 forks source link

Accessing rust lib from NestJS throws error #55

Open cskiwi opened 6 months ago

cskiwi commented 6 months ago

So when I tried to load in the function from the rust lib it throws:

╰─nx build app

   ×  1/2 dependent project tasks failed (see below)
   √  1/2 dependent project tasks succeeded [0 read from cache]

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

> nx run backend-test:build

Compiling TypeScript files for project "backend-test"...
libs/backend/test/src/lib/backend-test.service.ts:2:21 - error TS2307: Cannot find module '@app/rust-testing' or its corresponding type declarations.

2 import { sum } from '@app/rust-testing';
                      ~~~~~~~~~~~~~~~~~~~

————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— 

 NX   Ran target build for project app and 2 task(s) they depend on (5s)

   ×  1/2 failed
   √  1/2 succeeded [0 read from cache]

Any suggestion what might be wrong?

project can be found in: https://github.com/cskiwi/nestjs-rust You can see the error when you try and build the backend-test library (or the full app)

nx build backend-test

Cammisuli commented 5 months ago

Sorry about that, I'm having a hard time trying to install the deps in your repro.. I'll come around again when i update monodon to support the latest versions of Nx.

Nevermind, i got it installed. Let me see whats happening..

Cammisuli commented 5 months ago

Ok, I figured it out for you. It's entirely because of the @nx:js:tsc executor because it creates a temporary tsconfig file that modifies the path based on the outputs of dependent packages. So in your case, you can just add an output in libs/rust/testing/project.json like so:

"build": {
      "executor": "@monodon/rust:napi",
+     "outputs": ["{workspaceRoot}/libs/rust/testing"],
      "options": {
        "dist": "libs/rust/testing",
        "jsFile": "index.js"
      },
      "configurations": {
        "production": {
          "dist": "dist/rust_testing",
          "release": true
        }
      }

If you're just building for one platform you can get away with just having the dist here go to the same location as other dists. You'll end up with a index.js, index.d.ts and a *.node file, and thats all you need to get rust code loading. If you build and run on the same platform.

cskiwi commented 5 months ago

Hi,

Thanks for the response. indeed now the package build. However building the full app is still failing. see github action: https://github.com/cskiwi/nestjs-rust/actions/runs/9461993977/job/26063979120

And on a second note, running the serve creates a infinite loop because it detects that the index.d.ts is changed so it rebuilds. but building updates the index.d.ts and so the cycle continues


 NX   Running target build for project api and 2 tasks it depends on:

(node:25948) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
(Use `node --trace-warnings ...` to show where the warning was created)

> nx run rust_testing:build

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.12s

> nx run backend-test:build  [existing outputs match the cache, left as is]

> nx run api:build

> webpack-cli build --node-env=production

chunk (runtime: main) main.js (main) 4.56 KiB [entry] [rendered]

ERROR in ../../libs/rust/testing/index.d.ts
Module build failed (from ../../node_modules/ts-loader/index.js):
Error: Debug Failure. Output generation failed
    at Object.transpileModule <redacted>\node_modules\typescript\lib\typescript.js:137139:20)
    at getTranspilationEmit <redacted>\node_modules\ts-loader\dist\index.js:418:74)
    at successLoader <redacted>\node_modules\ts-loader\dist\index.js:38:11)
    at Object.loader <redacted>\node_modules\ts-loader\dist\index.js:23:5)

webpack compiled with 1 error (9cdf093728c8f5d4)
Warning: command "webpack-cli build --node-env=production" exited with non-zero status code

 NX   Running target build for project api and 2 tasks it depends on failed

Failed tasks:

- api:build
Cammisuli commented 5 months ago

You might have to change the dist of the napi build to outside of the source directory.