1

I am trying to import and use a library I made myself in rust into another library I made in rust for wasm web. The first library is my UI library. The second library that depends on the UI library is my Game library.

the directories are:

make/ui/src/lib.rs
make/ui/Cargo.toml 
make/game/src/lib.rs 
make/game/Cargo.toml

My UI library builds without error. and has the following Cargo.toml

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

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

I link my Game library to my local UI library in my Game library's Cargo.toml with

[dependencies]
ui = {path = "../ui/", version="0.1.0"}

The following build command executed in make/game:

wasm-pack build --release --target web 

gives the compilation error: in make/game/src/lib.rs

use ui::*; 

error[E0432]: unresolved import ui ^^^^^ use of undeclared crate or module `ui

the make/ui/src/lib.rs begins like this

pub mod ui 
{
  struct UI_A {...}
  impl UI_A {...}
  //etc...
}

What's missing?

UPDATE I am able to get Game library to build if I replace 'use ui' with

use crate::ui::*;
#[path = "../../ui/src/lib.rs"] mod ui;

and if I also remove pub mod ui from make/ui/src/lib.rs so I just have

struct UI_A {...}
impl UI_A {...}
//etc...

(from one of the answers posted here: How can I include a module from another file from the same project?)

I just find this a bit odd, since the path is already described in Cargo.toml. Also, although 'it works' the way I'm using it depends on having access to the source code - so not ideal if I wanted to divy up work to a team of developers to just 'use' the library without having access to the library source code.

If this is the way, please let me know.

user1709076
  • 2,538
  • 9
  • 38
  • 59
  • 1
    Is the name of the ui lib in the make/ui/Cargo.toml file also "ui"? – andber1 Mar 27 '23 at 16:01
  • This `#[path = "../../ui/src/lib.rs"] mod ui;` should make your line in the Cargo.toml obsolete. – andber1 Mar 27 '23 at 16:05
  • make/ui/Cargo.toml has [package] name = "ui" – user1709076 Mar 27 '23 at 16:29
  • When I recreate your example I get "error[E0659]: `ui` is ambiguous". Which makes sense, since you do not need the `pub mod ui` in make/ui/src/lib.rs. – andber1 Mar 27 '23 at 17:49
  • thanks. yes if i remove pub mod ui, and do: use crate::ui::*; #[path = "../../ui/src/lib.rs"] mod ui; it works. I guess I just thought I'd be able to do "use ui" if I had "pub mod ui {...}" at the top of my lib.rs – user1709076 Mar 27 '23 at 18:03
  • The line `#[path = "../../ui/src/lib.rs"] mod ui;` should not be necessary. `use crate::ui::*;`alone should work. – andber1 Mar 27 '23 at 18:20
  • I tried this out (the dependency one, you shouldn't need to use `#[path]`) and it didn't give any errors. Just two default cargo libraries with the indicated changes, except built with `cargo build` instead of wasm-pack. – drewtato Mar 27 '23 at 19:58
  • thanks. maybe the issue is wasm-pack then – user1709076 Mar 28 '23 at 22:40

0 Answers0