16

A few days ago, cross-compiling to JavaScript via Emscripten has finally hit nightly. I wanted to compile a project using glium in that manner. However, there are still many Emscripten-related bugs in many crates. While maintainers usually fix those bugs quickly, they don't necessarily release those bug fixes to crates.io immediately.

In my case, glium depends on glutin. glutin had a bug which is fixed now, but only in the git repository, not on crates.io. Note: glutin is not a direct dependency of my project; only an indirect one through glium!

How do I tell Cargo to use the glutin repository as source for glutin instead of crates.io?

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305

1 Answers1

15

You can use the [replace] section in your project's Cargo.toml. You can find the documentation about that feature here in the Cargo documentation.

In your case, glium depends on glutin 0.6.1. The version 0.6.1 on crates.io still contains the bug. So just add this to your Cargo.toml:

[replace]
"glutin:0.6.1" = { git = 'https://github.com/tomaka/glutin' }

Note however,

[...] that the replaced crate must not only have the same name but also the same version.

But even in the case of a version-mismatch (the repository already contains a newer version), you could still be in luck if the crate's maintainer creates git tags for every version (many in the Rust community do). In that case you can just specify the tag:

[replace]
"glutin:0.6.1" = { 
    git = 'https://github.com/tomaka/glutin' 
    tag = 'v0.6.1'
}

Sadly, this won't work with glutin, because the maintainer did not create tags for every version. In that case you can simply find the last commit before the version was bumped and specify it with rev = 'b4a3d0...' or specify a specific branch with the branch = '...' key.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • 1
    Is there a distinction between `"x:version.number" = ...` and `"x = { version = "version.number", ... }`? – Linear Oct 16 '16 at 20:39
  • 2
    @Jsor Yes, the latter doesn't work. The LHS defines *what* to replace and you can only replace specific versions of crates, not some crates in general (it may happen that you have two different versions of a crate in your dep-tree). The RHS says what to replace it *with* and as mentioned in the answer, the version has to match the LHS-version, so there is no point in writing it on the right side. – Lukas Kalbertodt Oct 16 '16 at 22:03
  • Note that `[replace]` is deprecated and `[patch]` should be used instead. See cargo docs linked in the answer for more. – code_dredd Jul 31 '20 at 19:56