Skip to content

Module not found on Windows (again) #531

Closed
@kkew3

Description

@kkew3

I encounter the same issue as #101 while developing a very simple lua module using rust: https://github.com/kkew3/expose-shared-library/tree/ci/examples/mixed-rust-lua-project. The build is run on github windows-2022 runner. I tried setting LUA_INC, LUA_LIB, LUA_LIB_NAME and copying lua51.dll to lua.dll as mentioned in chipsenkbeil/distant.nvim#57 by @khvzak, yet with no luck. The error is:

D:\a\expose-shared-library\expose-shared-library\.lua\bin\lua.exe: error loading module 'sum_as_string_rs' from file '.\sum_as_string_rs.dll':
	The specified module could not be found.


stack traceback:
	[C]: ?
	[C]: in function 'require'
	main.lua:1: in main chunk
	[C]: ?

I confirm exactly the same code works on macOS and ubuntu.

The excerpt of github workflow:

name: CI
runs-on: ${{ matrix.job.os }}
strategy:
  fail-fast: false
  matrix:
    lang:
      - lua
    job:
      - { os: windows-2022, target: x86_64-pc-windows-msvc }

steps:
  - name: Install lua prerequisite
    if: matrix.lang == 'lua'
    uses: ilammy/msvc-dev-cmd@v1
  - name: Setup lua
    if: matrix.lang == 'lua'
    uses: leafo/gh-actions-lua@v10
    with:
      luaVersion: '5.1.5'
  # Reference: https://github.com/chipsenkbeil/distant.nvim/issues/57#issuecomment-971992482
  - name: Copy lua lib files
    if: matrix.lang == 'lua' && matrix.job.os == 'windows-2022'
    shell: bash
    run: |
      cp .lua/lib/lua51.dll .lua/lib/lua.dll
      cp .lua/lib/lua51.lib .lua/lib/lua.lib

  - name: Install Rust toolchain
    uses: dtolnay/rust-toolchain@stable
    with:
      targets: ${{ matrix.job.target }}

  - name: Build and check (without '--target')
    shell: bash
    run: |
      case ${{ matrix.lang }} in
        lua)
          cd examples/mixed-rust-lua-project
          find lua -type f -name '*.so' -o -name '*.dll' -exec rm -f {} +
          cargo clean
          # Reference: https://github.com/chipsenkbeil/distant.nvim/issues/57#issuecomment-969256617
          export LUA_INC="$GITHUB_WORKSPACE/.lua/include"
          export LUA_LIB="$GITHUB_WORKSPACE/.lua/lib"
          export LUA_LIB_NAME=lua
          # Reference: https://github.com/rust-lang/cargo/issues/3687#issuecomment-336762567
          case ${{ matrix.job.os }} in
            windows-*)
              MSVC_PATH="$(echo "$PATH" | tr : '\n' | grep MSVC | head -n1)"
              export PATH="$MSVC_PATH:$PATH"
              cargo build --release
              ;;
          esac
          cd lua
          lua main.lua
          ;;
      esac

For your convenience, here are related files:

Cargo.toml:

[package]
name = "sum_as_string_rs"
version = "0.1.0"
edition = "2021"

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

[dependencies]
mlua = { version = "0.10", features = ["lua51", "module"] }

[build-dependencies]
expose-shared-library = { path = "../..", features = ["lua"] }

There is an expose-shared-library in build-dependencies. It's used to copy the output dll file to the directory where main.lua file resides.

build.rs:

use std::path::PathBuf;

fn main() {
    add_extension_module_link_args();
    let my_package_dir: PathBuf =
        [env!("CARGO_MANIFEST_DIR"), "lua"].iter().collect();
    expose_shared_library::expose_shared_library(my_package_dir).unwrap();
}

#[cfg(target_os = "macos")]
fn add_extension_module_link_args() {
    println!("cargo::rustc-link-arg=-undefined");
    println!("cargo::rustc-link-arg=dynamic_lookup");
}

#[cfg(not(target_os = "macos"))]
fn add_extension_module_link_args() {}

src/lib.rs:

use mlua::prelude::*;

fn sum_as_string(_: &Lua, (a, b): (usize, usize)) -> LuaResult<String> {
    Ok((a + b).to_string())
}

#[mlua::lua_module]
fn sum_as_string_rs(lua: &Lua) -> LuaResult<LuaTable> {
    let exports = lua.create_table()?;
    exports.set("sum_as_string", lua.create_function(sum_as_string)?)?;
    Ok(exports)
}

main.lua:

print(require("sum_as_string_rs").sum_as_string(1, 2))

Thank you so much for your help!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions