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

Accessing rust lib from NestJS throws error #55

Open
cskiwi opened this issue Jun 1, 2024 · 4 comments
Open

Accessing rust lib from NestJS throws error #55

cskiwi opened this issue Jun 1, 2024 · 4 comments

Comments

@cskiwi
Copy link

cskiwi commented Jun 1, 2024

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
Copy link
Owner

Cammisuli commented Jun 10, 2024

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
Copy link
Owner

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.

@Cammisuli Cammisuli reopened this Jun 10, 2024
@cskiwi
Copy link
Author

cskiwi commented Jun 11, 2024

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
Copy link
Owner

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

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

2 participants