Skip to content

Commit

Permalink
Rollup merge of rust-lang#121978 - GuillaumeGomez:dylib-duplicated-pa…
Browse files Browse the repository at this point in the history
…th, r=bjorn3

Fix duplicated path in the "not found dylib" error

While working on the gcc backend, I couldn't figure out why I had this error:

```
error: couldn't load codegen backend /checkout/compiler/rustc_codegen_gcc/target/release/librustc_codegen_gcc.so/checkout/compiler/rustc_codegen_gcc/target/release/librustc_codegen_gcc.so: cannot open shared object file: No such file or directory
```

As you can see, the path is duplicated for some reason. After investigating a bit more, I realized that `libloading::Error::LoadLibraryExW` starts with the path of the not found dylib, making it appear twice in our error afterward (because we do render it like this: `{path}{err}`, and since the `err` starts with the path...).

Thanks to ````@bjorn3```` for linking me to rust-lang#121392. :)
  • Loading branch information
matthiaskrgr authored Mar 5, 2024
2 parents 81970fd + 5e6e140 commit 07dadc5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion compiler/rustc_metadata/src/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,13 @@ fn load_dylib(path: &Path, max_attempts: usize) -> Result<libloading::Library, S
Err(err) => {
// Only try to recover from this specific error.
if !matches!(err, libloading::Error::LoadLibraryExW { .. }) {
return Err(err.to_string());
let err = format_dlopen_err(&err);
// We include the path of the dylib in the error ourselves, so
// if it's in the error, we strip it.
if let Some(err) = err.strip_prefix(&format!(": {}", path.display())) {
return Err(err.to_string());
}
return Err(err);
}

last_error = Some(err);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/codegen/duplicated-path-in-error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ only-linux
//@ compile-flags: -Zcodegen-backend=/non-existing-one.so

// This test ensures that the error of the "not found dylib" doesn't duplicate
// the path of the dylib.

fn main() {}
2 changes: 2 additions & 0 deletions tests/ui/codegen/duplicated-path-in-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: couldn't load codegen backend /non-existing-one.so: cannot open shared object file: No such file or directory

0 comments on commit 07dadc5

Please sign in to comment.