rules_rust icon indicating copy to clipboard operation
rules_rust copied to clipboard

Runfiles should return `Result` instead of panicing

Open UebelAndre opened this issue 1 year ago • 8 comments

I haven't been keeping up too closely with the recent changes to runfiles but I wanna make sure the interface doesn't have panics in it and that we allow users to handle results at runtime.

https://github.com/bazelbuild/rules_rust/blob/0.44.0/tools/runfiles/runfiles.rs#L146-L155

This seems concerning. In practice does it not ever get hit?

UebelAndre avatar May 15 '24 04:05 UebelAndre

cc @matts1 @dzbarsky @scentini

UebelAndre avatar May 15 '24 04:05 UebelAndre

Looks like this dates all the way back to https://github.com/bazelbuild/rules_rust/commit/b8219ac7f74d06c4983e96a1b9d0dbe00d7a52c7 from 3 years ago. This is only hit if you try to lookup a path to a missing file when using the manifest strategy. I'd imagine this is not a very common use case - if you need the file and don't have it, you're typically out of luck, and I expect virtually all usage will just unwrap the result anyway. But I suppose we could offer a secondary result-based API if anyone does want to do this conditional lookup and handle it more carefully?

DavidZbarsky-at avatar May 15 '24 04:05 DavidZbarsky-at

A Result type generally has value when we think that a failure is expected (and not a bug). In this case, if the file doesn't exist in the manifest, it's pretty much guaranteed to be a bug, rather than an actual error (since we already validated that the runfiles directory exists on creation).

I agree with @DavidZbarsky-at that offering a secondary API seems reasonable.

FWIW, I really don't like how all implementations of runfiles (not just rules_rust's) fail if a file is missing for manifest-based runfiles, but succeed for directory based runfiles. This leads to an interesting consequence where directory based runfiles can use runfiles to look up directories, but manifest based ones can't.

matts1 avatar May 15 '24 06:05 matts1

I'd be ok with making a breaking change for this, but at least offering an alternative would make sense - not sure how others feel...

illicitonion avatar May 15 '24 09:05 illicitonion

I'd prefer not breaking here - I think providing an alternative would make sense.

Alternatively, we may want to consider bringing up with the bazel team the possibility of changing the spec so that if the file doesn't exist it returns it anyway, since it's a source of inconsistency between manifest-based runfiles and directory-based runfiles.

matts1 avatar May 16 '24 02:05 matts1

I think the current state of things with the panic should be considered a critical oversight. The library itself should not make the assumption that if the rlocationpath the user asked for doesn't exist, then the entire process should exit (which is the implication of a panic). In both manifest-based and directory-based runfiles, I'm proposing Result is returned and it it's up to the user to decide what to do with that.

UebelAndre avatar May 16 '24 03:05 UebelAndre

Alternatively, we may want to consider bringing up with the bazel team the possibility of changing the spec so that if the file doesn't exist it returns it anyway, since it's a source of inconsistency between manifest-based runfiles and directory-based runfiles.

Can you outline the behavior you expect in each case?

UebelAndre avatar May 16 '24 03:05 UebelAndre

Directory based runfiles just returns the path. If the file doesn't exist in the manifest, then path.is_file will return false, but the command will still succeed

matts1 avatar May 16 '24 12:05 matts1

I have created https://github.com/bazelbuild/rules_rust/pull/2847 which converts panics to Result. The change mostly affects manifest-based runfiles which used to panic when a missing runfile was requested but in general the larger change is that rlocation! now returns an Option. If there are major concerns with this please let me know!

UebelAndre avatar Sep 08 '24 03:09 UebelAndre