Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The cdylib's name can be overriden by lib.name #339

Closed
konstin opened this issue Sep 23, 2018 · 0 comments · Fixed by #435
Closed

The cdylib's name can be overriden by lib.name #339

konstin opened this issue Sep 23, 2018 · 0 comments · Fixed by #435
Milestone

Comments

@konstin
Copy link
Contributor

konstin commented Sep 23, 2018

🐛 Bug description

Currently, wasm-pack assumes the cdylib's name is the crate name with underscores and .wasm as extension, while this value can be overwritten by lib.name in Cargo.toml.

👟 Steps to reproduce

Cargo.toml

[package]
name = "foo"
version = "0.1.0"
authors = ["konstin <konstin@mailbox.org>"]

[lib]
crate-type = ["cdylib"]
name = "bar" # <- "Renames" the artifact to bar.wasm

[dependencies]
wasm-bindgen = "0.2.22"

src/lib.rs

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

Output of wasm-pack build --mode no-install --debug

  [1/6] Checking crate configuration...
  [2/6] Compiling to WASM...
  [3/6] Creating a pkg directory...
  [4/6] Writing a package.json...
  :-) [WARN]: Field 'description' is missing from Cargo.toml. It is not necessary, but recommended
  :-) [WARN]: Field 'repository' is missing from Cargo.toml. It is not necessary, but recommended
  :-) [WARN]: Field 'license' is missing from Cargo.toml. It is not necessary, but recommended
  [5/6] Copying over your README...
  :-) [WARN]: origin crate has no README
| [6/6] Running WASM-bindgen...
wasm-bindgen failed to execute properly. stderr:

error: failed to read `target/wasm32-unknown-unknown/debug/foo.wasm`
	caused by: No such file or directory (os error 2)

🖥️ Relevant code

/// Get the crate name for the crate at the given path.
pub fn get_crate_name(path: &Path) -> Result<String, Error> {
Ok(read_cargo_toml(path)?.package.name)
}

And wasm_bindgen_build

🤔 Expected Behavior

While checking for lib.name would fix this particular issue, it's definitely not future proof and I assume there are already cases today where this still wouldn't work.

One solution would be asking cargo through cargo metadata. Relevant excerpt:

{
  "packages": [
    {
      "name": "c",
      "version": "0.1.0",
      "id": "c 0.1.0 (path+file:///home/konsti/wasm-pack/c)",
      "license": null,
      "license_file": null,
      "description": null,
      "source": null,
      "dependencies": [
        {
          "name": "wasm-bindgen",
          "source": "registry+https://github.com/rust-lang/crates.io-index",
          "req": "^0.2.22",
          "kind": null,
          "rename": null,
          "optional": false,
          "uses_default_features": true,
          "features": [],
          "target": null
        }
      ],
      "targets": [
        {
          "kind": [
            "cdylib"
          ],
          "crate_types": [
            "cdylib"
          ],
          "name": "d",
          "src_path": "/home/konsti/wasm-pack/c/src/lib.rs",
          "edition": "2015"
        }
      ],
      "features": {},
      "manifest_path": "/home/konsti/wasm-pack/c/Cargo.toml",
      "metadata": null,
      "authors": [
        "konstin <konstin@mailbox.org>"
      ],
      "categories": [],
      "keywords": [],
      "readme": null,
      "repository": null,
      "edition": "2015"
    },
  ]
}

The other option would be to use the info from --message-format=json in #333 to get the target name. This is e.g. the prettified last line from cargo build -q --message-format json --target wasm32-unknown-unknown:

{
  "features": [],
  "filenames": [
    "/home/konsti/wasm-pack/foo/target/wasm32-unknown-unknown/debug/bar.wasm"
  ],
  "fresh": true,
  "package_id": "foo 0.1.0 (path+file:///home/konsti/wasm-pack/foo)",
  "profile": {
    "debug_assertions": true,
    "debuginfo": 2,
    "opt_level": "0",
    "overflow_checks": true,
    "test": false
  },
  "reason": "compiler-artifact",
  "target": {
    "crate_types": [
      "cdylib"
    ],
    "edition": "2015",
    "kind": [
      "cdylib"
    ],
    "name": "bar",
    "src_path": "/home/konsti/wasm-pack/foo/src/lib.rs"
  }
}

Both solutions would supersede #25.

❔ Unresolved question

Should the lib name influence how the exports are named? Like we could use const js = import("@MYSCOPE/c/d.js"); in the example.

@ashleygwilliams ashleygwilliams added bug Something isn't working to-do stuff that needs to happen, so plz do it k thx labels Sep 23, 2018
@ashleygwilliams ashleygwilliams added this to the 0.6.0 milestone Sep 23, 2018
@ashleygwilliams ashleygwilliams added the question Further information is requested label Sep 23, 2018
alexcrichton added a commit to alexcrichton/wasm-pack that referenced this issue Nov 6, 2018
This commit fixes an issue where if a library is renamed via the `name`
key in the `[lib]` section of the manifest then `wasm-pack` would try to
generate bindings for an noexistent wasm-file, generating an error.

The fix was to internally use `cargo_metadata` more aggressively and
move around where this data is generated. This ended up refactoring a
few locations, but this should also bring improved error messages for
`cargo metadata` as well as caching the resulting data more aggressively
to avoid recalculating it too much.

Closes rustwasm#339
alexcrichton added a commit to alexcrichton/wasm-pack that referenced this issue Nov 6, 2018
This commit fixes an issue where if a library is renamed via the `name`
key in the `[lib]` section of the manifest then `wasm-pack` would try to
generate bindings for an noexistent wasm-file, generating an error.

The fix was to internally use `cargo_metadata` more aggressively and
move around where this data is generated. This ended up refactoring a
few locations, but this should also bring improved error messages for
`cargo metadata` as well as caching the resulting data more aggressively
to avoid recalculating it too much.

Closes rustwasm#339
alexcrichton added a commit to alexcrichton/wasm-pack that referenced this issue Nov 6, 2018
This commit fixes an issue where if a library is renamed via the `name`
key in the `[lib]` section of the manifest then `wasm-pack` would try to
generate bindings for an noexistent wasm-file, generating an error.

The fix was to internally use `cargo_metadata` more aggressively and
move around where this data is generated. This ended up refactoring a
few locations, but this should also bring improved error messages for
`cargo metadata` as well as caching the resulting data more aggressively
to avoid recalculating it too much.

Closes rustwasm#339
@ashleygwilliams ashleygwilliams added changelog - fix and removed bug Something isn't working question Further information is requested to-do stuff that needs to happen, so plz do it k thx labels Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants