-
Notifications
You must be signed in to change notification settings - Fork 114
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
Got bitten by Drop impl running to early when passing TempDir to fn(p: impl AsRef<Path>) #115
Comments
This is an interesting issue. You're right that changing the impl to On the other hand, nearly all AsRef users immediately call For now, I'd document this pitfall in the top-level Note: Your fix works but there's an edge-case: Rc, Arc, etc. There's currently a blanket impl of |
(on the other hand, autoderef probably takes care of that in most cases) |
This commit adds a section to the `src/libs.rs` docs pointing out an easily encounterable error when ussing `tempdir()` with apis that are generic over `AsRef<Path>`, like `Command::current_dir`, resulting in the directory being dropped to early (As pointed out in Stebalien#115).
This commit adds a section to the `src/libs.rs` docs pointing out an easily encounterable error when ussing `tempdir()` with apis that are generic over `AsRef<Path>`, like `Command::current_dir`, resulting in the directory being dropped to early (As pointed out in #115).
I fell to this pitfall as well, took me a lot of debugging hours because I assumed the error was somewhere else, it's usually something you do not expect. Because |
I've been bitten by this too, and spent a few hours debugging a failing test because of this. Based on a colleague's suggestion, I'm wondering if the |
Do people put |
@jplatte https://grep.app/?q=Arc%3CTempDir%3E I'll keep this issue in mind if we ever have a v4 (and can introduce large breaking changes), but it's not enough to motivate a v4. |
Yesterday i got badly bitten by using
tempdir()
in conjunction withstd::process::Command::current_dir(...)
. My initial code looked like this:with
bin
doing file operations insidetmp_dir
. Unfortunately this did not work, and I received an error along the lines ofOs Error 2: No such file or directory
.After beating my head against the screen for some time i thought "maybe the tmp_dir is dropped to early and the directory deleted before
bin
is run?" so i changedto
and my program worked just as intended.
Like the issue title says, this was due to the signature of
Since
TempDir
implementsAsRef<Path>
i thought that moving it in would be the right thing to do (i didn't need to access the tmp_dir later in the method after constructing the command). But this lead toDrop
impl ofTempDir
being run aftercurrent_dir
finished but before the execution ofbin
.This kind of mistake can quickly happen, especially since many APIs dealing with Paths are generic over
AsRef<Path>
.My question is if this pitfall should be pointed out in the documentation and if so, where the best place would be to do this? I'd like to start contributing to some rust crates and this seems like a really easy thing to do, but which could have saved me some time and nerves :D
I was also thinking if it's possible to prevent this mistake, maybe by changing the
AsRef<Path>
impl to:but this would be a breaking change.
Edit: I assume all of this applies to
TempFile
as well.The text was updated successfully, but these errors were encountered: