TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Can a Rust binary use incompatible versions of the same library?

84 pointsby braxxox9 months ago

13 comments

richardwhiuk9 months ago
You can do this in one crate:<p><pre><code> [dependencies] foo_v1 = { package = &quot;foo&quot;, version = &quot;1&quot; } foo_v2 = { package = &quot;foo&quot;, version = &quot;2&quot; }</code></pre>
WD-429 months ago
This isn’t a magic bullet. Using multiple versions of the same crate can still blow up your project.<p>For example, the compiler error in this example:<p>note: perhaps two different versions of crate `smithay_client_toolkit` are being used?<p><a href="https:&#x2F;&#x2F;github.com&#x2F;pop-os&#x2F;launcher&#x2F;issues&#x2F;237">https:&#x2F;&#x2F;github.com&#x2F;pop-os&#x2F;launcher&#x2F;issues&#x2F;237</a>
评论 #41288769 未加载
woodruffw9 months ago
I thought this was about loading two incompatible versions of a shared object into the same address space at first :-)<p>The author correctly contrasts Rust (and NPM&#x27;s) behavior with that of Python&#x2F;pip, where only one version per package name is allowed. The Python packaging ecosystem <i>could</i> in theory standardize a form of package name mangling wherein multiple versions could be imported simultaneously (akin to what&#x27;s currently possible with multiple vendored versions), but that would likely be a significant undertaking given that a <i>lot</i> of applications probably - accidentally - break the indirect relationship and directly import their transitive dependencies.<p>(The more I work in Python, the more I think that Python&#x27;s approach is actually a good one: preventing multiple versions of the same package prevents dependency graph spaghetti when every subdependency depends on a slightly different version, and provides a strong incentive to keep public API surfaces small and flexible. But I don&#x27;t think that was the intention, more of an accidental perk of an otherwise informal approach to packaging.)
评论 #41287432 未加载
评论 #41287388 未加载
评论 #41287336 未加载
gorgoiler9 months ago
For fun, you could add this to Python and I think it would it cover a lot of edge cases?<p>You would need:<p>A function <i>v_tree_install(spec)</i> which installs a versioned pypi package like “foo=3.2” and all its dependencies in its own tree, rather than in site-packages.<p>Another pair of functions <i>v_import</i> and <i>v_from_import</i> to wrap importlib with a name, version, and symbols. These functions know how to find the versioned package in its special tree <i>and</i> push that tree to sys.path before starting the import.<p>To cover the case for when the imported code has dynamic imports you could also wrap any callable code (functions, classes) with a wrapper that <i>also</i> does the sys.push&#x2F;pop before&#x2F;after each call.<p>You then replace third party imports in your code with calls assigning to symbols in your module:<p><pre><code> # import foo foo = v_import(“foo==3.2”) # from foo import bar, baz as q bar, q = v_from_import( “foo&gt;=3.3”, “bar”, “baz”, ) </code></pre> Finally, provide a function (or CLI tool) to statically scan your code looking for <i>v_import</i> and calling <i>v_tree_install</i> ahead of time. Or just let <i>v_import</i> do it.<p>Edit: …and you’d need to edit the sys.modules cache too, or purge it after each “clever” import?
评论 #41294852 未加载
评论 #41288701 未加载
评论 #41289643 未加载
btilly9 months ago
This is great for avoiding conflicts when you try to get your project running.<p>It sucks when there is a vulnerability in a particular library, and you&#x27;re trying to track all of the ways in which that vulnerable code is being pulled into your project.<p>My preference is to force the conflict up front by saying that you can&#x27;t import conflicting versions. This creates a constant stream of small problems, but avoids really big ones later. However I absolutely understand why a lot of people prefer it the other way around.
评论 #41288197 未加载
评论 #41288410 未加载
评论 #41288000 未加载
alkonaut9 months ago
How does this work? Assume that the log crate in its internal state has a lock it uses for synchronizing writing to some log endpoint. If I have two versions of log in my process then they must have two copies of their internal state. So they both point to the same log endpoint, but they have one mutex <i>each</i>? That means it &quot;works&quot; at compile time but fails at runtime? That&#x27;s the worst kind of &quot;works!&quot;<p>Or if I depend transitively on two versions of a library (e.g. a matrix math lib) through A and B and try to read a value from A and send it into B. Then presumably due to type namespacing that will fail at compile time?<p>So the options when using incompatible dependencies are a) it compiles, but fails at runtime, b) it doesn&#x27;t compile, or c) it compiles and works at runtime?
评论 #41289263 未加载
dboreham9 months ago
Every language will re-create its own version (sic) of DLL-hell.
pjmlp9 months ago
And this is why one gets to watch some crates being compiled from scratch multiple times in a single &quot;make world&quot; build.
aragilar9 months ago
FYI, Python can&#x2F;did support multiple versions via buildout (<a href="http:&#x2F;&#x2F;www.buildout.org&#x2F;en&#x2F;latest&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.buildout.org&#x2F;en&#x2F;latest&#x2F;</a>) but it&#x27;s complicated and wide-scale support has probably bit-rotted away.
anonymoushn9 months ago
You can do this, but you can&#x27;t use two semver-compatible versions of the same library in *different binaries* in the same workspace.
hsfzxjy9 months ago
So both versions of log crate manage their own internal states within the same process? Would this lead to surprising results?
评论 #41287730 未加载
jcelerier9 months ago
How does that work if you want to export a symbol for dlopen?
评论 #41291059 未加载
评论 #41290998 未加载
gmueckl9 months ago
I cannot shake the feeling that this is actually a misfeature that will get people into trouble in new and puzzling ways. The isolated classloaders in Java and the assembly domains in .Net didn&#x27;t turn out to be very bright ideas and from a software design perspective this is virtually identical.
评论 #41292031 未加载