DelSkayn / rquickjs

High level bindings to the quickjs javascript engine
MIT License
434 stars 59 forks source link

Path-related problems with `rquikjs::embed! []` macros #240

Closed sisungo closed 5 months ago

sisungo commented 7 months ago

Introduction

My project has a structure like:

/workspace
          /Cargo.toml
          /crate_a
                  /Cargo.toml
                  /src
                      /main.rs
                      /main.js

When using rquickjs::embed! [] macro, with main.rs looks like:

static BUNDLE: Bundle = rquickjs::embed! [
    "app_main": "main.js",
];

It fails with the error of No such file or directory. When inputing an empty path, it shows that the file is being found at a directory beginning with $HOME/.cargo/registry/src.

Environment

$ uname -a
Darwin pworks-laptop.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct  9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 arm64

$ rustc --version
rustc 1.75.0-nightly (4b85902b4 2023-11-04)
DelSkayn commented 7 months ago

The embed macro import paths are relative to CARGO_MANIFEST_DIR i.e. the directory where the Cargo.toml file is located.

Have you tried importing the main.js with the path src/main.js?

DelSkayn commented 7 months ago

I know this is different from how rust does file imports with include!() but there is no good way to replicate that behavior in proc macro like embed so that is way file paths in the embed macro are relative to the crate root.

sisungo commented 7 months ago

The embed macro import paths are relative to CARGO_MANIFEST_DIR i.e. the directory where the Cargo.toml file is located.

Have you tried importing the main.js with the path src/main.js?

@DelSkayn I tried src/main.js, crate_a/src/main.js, and them with ./ prefix. Neither of them are working. When I input an empty path like this:

static JSBUNDLE: Bundle = rquickjs::embed! [
        "app_main": "",
];

The error message looks like:

error: Error loading embedded js module from path `/Users/sisungo/.cargo/registry/src/rsproxy.cn-0dccff568467c15b/rquickjs-macro-0.4.0-beta.4`: Is a directory (os error 21)
 --> airlogd/src/main.rs:5:9
  |
5 |         "app_main": "",
  |         ^^^^^^^^^^

Deleting all README.md in my project, and input README.md as path, the error message turned to:

error: Exception generated by quickjs
 --> airlogd/src/main.rs:4:31
  |
4 |       static JSBUNDLE: Bundle = rquickjs::embed! [
  |  _______________________________^
5 | |         "app_main": "README.md",
6 | |     ];
  | |_____^
  |
  = note: this error originates in the macro `rquickjs::embed` (in Nightly builds, run with -Z macro-backtrace for more info)

I think the procedure macro is finding file from ~/.cargo/registry/src/*/rquickjs-macro-*. (the problem also happens on rquickjs 0.3.x)

DelSkayn commented 7 months ago

I have found the problem and it is already fixed in the master branch. Turns out the 0.3 and the current beta still have a bug in retrieving CARGO_MANIFEST_DIR, They accidentally use the environment variable from compile time instead of runtime.

It is already fixed in main, I am hoping to release the full 0.4 this week so it will be fixed then. I'll leave this issue open until then.

DelSkayn commented 5 months ago

Closing this issue as it should be fixed in the new 0.4 version