I am contributing to docx-rs, to put its wasm dependencies behind a feature flag

But I am having trouble...

it is a workspace that looks like this

[workspace]

members = [
    "docx-core",
    "docx-wasm",
]

docx-core/

[package]
name = "docx-rs"

[lib]
name = "docx_rs"
path = "src/lib.rs"

[features]
wasm = ["wasm-bindgen", "ts-rs"]

[dependencies]
wasm-bindgen = { version = "0.2.78", optional = true }
ts-rs = { version = "6.1", optional = true }

docx-wasm/

[package]
name = "docx-wasm"

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

[dependencies]
wasm-bindgen = "0.2.78"
docx-rs= { path = "../docx-core", features = ["wasm"] }

In my crate's workspace root i pull in my patched version

[patch.crates-io]
# https://github.com/bokuweb/docx-rs/pull/622
docx-rs = { git = 'https://github.com/arifd/docx-rs.git', default-features = false }

And while my lockfile has changed, it still is pulling in the wasm stuff? :frowning:

[[package]]
name = "docx-rs"
version = "0.4.6"
source = "git+https://github.com/arifd/docx-rs.git#971fceb2a165710e80ab588d3e301d359d29d0e7"
dependencies = [
 "base64 0.13.1",
 "image",
 "serde",
 "serde_json",
 "thiserror",
 "ts-rs",
 "wasm-bindgen",
 "xml-rs",
 "zip",
]

Any advice?

did you miss a branch name in the patch? the url repo doesn't contain the modification you mentioned.

Yes! That's true. I don't know how to specify the branch! It's called wasm-feature-gate

well, just add a branch = <name> attribute:

[patch.crates-io]
docx-rs = { git = 'https://github.com/arifd/docx-rs.git', branch = "wasm-feature-gate", default-features = false }
1 Like

That's great! it fixed it! :smiley:

[[package]]
name = "docx-rs"
version = "0.4.6"
source = "git+https://github.com/arifd/docx-rs.git?branch=wasm-feature-gate#6cf43c71561a2fa12df3231e8e85a516fad01295"
dependencies = [
 "base64 0.13.1",
 "image",
 "serde",
 "serde_json",
 "thiserror",
 "xml-rs",
 "zip",
]

One question though, considering in the workspace, docx-wasm pulls in wasm-bindgen on its own... Why is this working? Where is cargo being told that it doesn't need to compile docx-wasm?

you are compiling in your root workspace. workspace is a local configuration, dependencies can't have their own workspaces. it is crates that get published, not the workspace. the fact that repo is a virtual workspace and the source code for a paritular crate resides inside a workspace, it is the configuration of the crate's developer. when they are published to a registry such as z"crate.io", each crate is published individually, and the local path dependencies are verified and converted during an normalization process before upload.

Oh that's very interesting!

So there will be a crate at https://crates.io/crates/docx-rs

and somewhere crates.io is configured to point to docx-core?

and if you want to build for wasm, how will it know to pull in docx-wasm? since wasm depends on core, and not the other way around?

no, there's no docx-core crate, the crate name is docs-rs, check the Cargo.toml file:

when the crate get published, cargo just packs the crate files into an archive file and upload it to the registry (default registry is crates.io), the local directory name or the repository url (or home page url, for that matter) can be arbitrary value, and have nothing to do with the crate name, they are just meta data for the crate.


well, since the docx-wasm crate is not published to crates.io, you'd better consult the author about the usage. but I guess most likely you are supposed use the git repo directly as your dependency instead of pull it from crates.io:

[dependencies]
docx-wasm = { git = "https://github.com/bokuweb/docx-rs" }

don't worry about the crate isn't at the root of the repo, cargo understand workspace members.