From: "mame (Yusuke Endoh)" Date: 2022-08-30T07:37:20+00:00 Subject: [ruby-core:109783] [Ruby master Bug#18784] `FileUtils.rm_f` and `FileUtils.rm_rf` should not mask exceptions Issue #18784 has been updated by mame (Yusuke Endoh). Here is a patch. Revert fa65d676ece93a1380b9e6564efa4b4566c7a44b and apply: ```diff diff --git a/lib/fileutils.rb b/lib/fileutils.rb index 4ba7d18..178db6e 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -1422,6 +1422,7 @@ module FileUtils begin ent.remove rescue + warn "failed to remove: %s" % $!.message if $VERBOSE raise unless force end end ``` I would like to discuss this at the next dev meeting with other committers. --- In fact, I think raising an exception is more "correct". I have even tried that change. However, I am afraid that the advantage of the correctness is not likely to pay its disadvantage. This change will allow us to find a bug, but none of the bugs found so far are critical. On the other hand, fixing the bugs correctly was very time consuming (or almost impossible). So the change will force users to write a patch to swallow exceptions with `rescue Exception`. In fact, when I talked this issue to @ko1, he said he would add a defensive rescue for `rm_rf` in debug.gem. Users have to work, code gets messy, and bugs don't get fixed. ---------------------------------------- Bug #18784: `FileUtils.rm_f` and `FileUtils.rm_rf` should not mask exceptions https://bugs.ruby-lang.org/issues/18784#change-99019 * Author: deivid (David Rodr��guez) * Status: Open * Priority: Normal * Assignee: mame (Yusuke Endoh) * ruby -v: ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [arm64-darwin21] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN ---------------------------------------- In recent times, I've been having issues with these methods because they don't let you know when some issue happened while trying to remove the given folders/files. IMO most users expect all pre-existing folders/files that are passed to these methods to be actually removed by the methods. Instead, when this happens, errors are silently swallowed and normally the result is that you will get some other issue further down the road, making the problem hard to debug. The current workaround I'm using is to double check whether the files still exist after the method, and raise a custom error if they do, but I still can't see the original problem, so issues are similarly hard to debug. This is also a deviation from how `rm -rf` and `rm -f` work, since these tools finish with a failure exit code when they fail to remove the given files. Given that `fileutils` is intended to mimic shell functionality, I think this is just a bug. I think the intention of the `force` flag here is to: * Don't prompt for confirmation. * Ignore given arguments that are not files that already exist. But any issue other than that should not be swallowed, and in general I think the method should succeed if and only if the given list of file names does not exist after the methods are done. I think this is in line with the following note I get when I run `man rm`, but also suggests that this is not the standard behavior of "historical implementations" > COMPATIBILITY > > The rm utility differs from historical implementations in that the -f option only masks attempts to remove non-existent files instead of masking a large variety of errors. I implemented this at https://github.com/ruby/fileutils/pull/58, but treating this as a bug. I can also implement a more conservative for approach for users that might be using `FileUtils.rm_rf` or `FileUtils.rm_f` but don't really care if the files are removed or not. Alternative proposals would be `FileUtils.rm_rf(force: strict)`, or `FileUtils.strict_rm_rf`, but to be honest, if this is considered a breaking change, I would ship it as a new major version, and let users update their code to swallow errors themselves if they need to. Happy to hear any feedback! -- https://bugs.ruby-lang.org/ Unsubscribe: