banner
Madinah

Madinah

github
twitter
telegram
email
bilibili

Setting up the development environment using napi-rs

napi-rs#

https://napi.rs/docs/introduction/getting-started provides convenient commands for us to develop node addons based on Rust. Farm is also developed based on napi-rs. This section records the process of setting up napi-rs and adds debugging commands to enhance the debugging experience.
Detailed process

  1. Initialize the project: Follow the steps on https://napi.rs/docs/introduction/getting-started to initialize the project directory.
  2. Transform the existing project into a monorepo project using pnpm + cargo
    a. Delete the original yarn file, create pnpm-workspace.yaml, and then execute pnpm install.
    b. Transform the original Rust project into a monorepo form. The specific configuration files are as follows:
# pnpm-workspace.yaml
packages:
  - packages/*
  - examples/*
  - crates/*
<!-- Cargo.toml -->
[workspace]
members = ["crates/*"]
resolver = "2"

[workspace.dependencies]
napi = {version = "2.15.0", default-features = false, features = [
  "napi4",
  "error_anyhow",
  "serde-json",
]}
napi-derive = "2.15.0"
  1. Add the corresponding packages
cli
    ├── Cargo.toml
    ├── build.rs
    └── src
        └── lib.rs

Specific file contents

# Cargo.toml
[package]
edition = "2021"
name = "farm_cli"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib"]

[dependencies]
napi = {workspace = true}
napi-derive = {workspace = true}

[build-dependencies]
napi-build = "2.0.1"

[profile.release]
lto = true
strip = "symbols"

build.rs

use napi_build::setup;

fn main() {
    setup()
}
#![deny(clippy::all)]

#[macro_use]
extern crate napi_derive;

#[napi]
pub fn sum(a: i32, b: i32) -> i32 {
  a + b
}
  1. Packages file directory
cli
    ├── binding
    │   ├── binding.cjs
    │   ├── binding.d.ts
    │   └── index.darwin-arm64.node
    ├── package.json
    └── src
        └── index.js

How to generate the binding file

Add the following command

"build:rs:debug": "napi build --platform --cargo-name farm_cli -p farm_cli --cargo-cwd ../../ binding --js binding.cjs --dts binding.d.ts"

index.cjs

const { sum }  = require('../binding/binding.cjs')
const total = sum(1,2);
console.log(total)

Test execution, no problem with the output

image

Add debugging
Add the following under .vscode

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "lldb",
      "request": "launch",
      "sourceLanguages": ["rust"],
      "name": "debug rust",
      "program": "node",
      "preLaunchTask": "npm: build:debug",
      "args": ["--inspect", "${file}"]
    }
  ]
}

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "type": "npm",
      "script": "build:dev",
      "group": "build",
      "problemMatcher": [],
      "label": "npm: build:debug"
    }
  ]
}

Debugging effect

image

The environment setup is complete. Code repository: https://github.com/Maidang1/rust-learn-code/commit/51f5d71cdf9c6a3aa691057523e9145a9bde81af

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.