FileSystem::deleteRecursive() shouldn't log a message when it tries to delete a non-existent directory

Created on 13 July 2023, over 1 year ago
Updated 25 August 2023, about 1 year ago

Problem/Motivation

When you pass a non-existent directory to FileSystem::deleteRecursive(), you get a message from FileSystem::delete() that you're trying to delete a file that doesn't exist.

I'm not entirely sure that message is useful for an individual file, but it seems especially not useful for a directory, where we just want to get rid of the directory and everything in it.

Steps to reproduce

See 🐛 Keep assets directory when clearing cache Closed: duplicate

Proposed resolution

Check file_exists()

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

🐛 Bug report
Status

Fixed

Version

10.1

Component
File system 

Last updated about 11 hours ago

Created by

🇬🇧United Kingdom catch

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @catch
  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    29,800 pass, 8 fail
  • 🇬🇧United Kingdom catch

    Here's a patch.

  • 🇬🇧United Kingdom catch

    There are no explicit tests for this currently, we have some for FileSystem but not for ::delete() or ::deleteRecursive().

  • Status changed to Needs work over 1 year ago
  • 🇬🇧United Kingdom longwave UK

    I think this is a reasonable change to make, if you ask to delete a directory and it's already deleted, then the job is done.

    1. +++ b/core/lib/Drupal/Core/File/FileSystem.php
      @@ -336,6 +336,7 @@ public function delete($path) {
      +      dump($path);
      

      Debug code :)

    2. +++ b/core/lib/Drupal/Core/File/FileSystem.php
      @@ -367,7 +372,6 @@ public function deleteRecursive($path, callable $callback = NULL) {
      -
      

      Whitespace changes are out of scope.

  • Status changed to Needs review over 1 year ago
  • last update over 1 year ago
    29,811 pass
  • 🇬🇧United Kingdom catch

    Tidied up the cruft.

    @longwave what do you think about removing the notice altogether, i.e. from FileSystem::delete()?

  • Status changed to RTBC over 1 year ago
  • 🇬🇧United Kingdom longwave UK

    @catch I am leaning towards yes, but not 100% convinced just yet. 📌 Remove dependency of "file_system" service on "logger" Needs work makes a similar argument that the filesystem service is low level, and should not be logging or presenting messages directly to users. In the long term it feels like the service should throw exceptions in the case of error, and the caller should decide what to do with them, because it usually depends on why the methods are being called in the first place. But changing all that in a backward-compatible way is non trivial.

  • 🇷🇺Russia Chi

    FileSystem::deleteRecursive() shouldn't log a message when it tries to delete an empty directory

    I think the title is not accurate. The file system does not complain on empty directories. It only complains on non-existing directories.

  • 🇬🇧United Kingdom longwave UK

    Fixed title, thanks!

  • 🇬🇧United Kingdom catch

    Given we have 📌 Remove dependency of "file_system" service on "logger" Needs work I think the narrow scope here is fine. Even if we added explicit test coverage for ::deleteRecursive() I don't think we'd add test coverage for this issue since it would be asserting against historical behaviour not current behaviour (i.e. we could only assert the absence of a log message, which we'd never do if there hadn't been a log message in the first place), so hoping this can go in as-is.

  • 🇦🇺Australia kim.pepper 🏄‍♂️🇦🇺Sydney, Australia

    re: #7 I think if calling code wants to do something special if a directory doesn't exist, then it should check for that itself.

  • last update over 1 year ago
    29,815 pass
  • last update over 1 year ago
    29,815 pass
  • 🇦🇺Australia acbramley

    I like the approach in #10, the tests were a bit naff anyway (see 🐛 Keep assets directory when clearing cache Closed: duplicate )

  • last update over 1 year ago
    29,822 pass
    • lauriii committed e983ee9d on 11.x
      Issue #3374319 by catch, longwave: FileSystem::deleteRecursive() shouldn...
    • lauriii committed 23140022 on 10.1.x
      Issue #3374319 by catch, longwave: FileSystem::deleteRecursive() shouldn...
  • Status changed to Fixed over 1 year ago
  • 🇫🇮Finland lauriii Finland

    Committed e983ee9 and pushed to 11.x. Also cherry-picked to 10.1.x. Thanks!

  • 🇹🇷Turkey orkut murat yılmaz Istanbul

    Hello all,

    Thanks for the contribution.

    I've downloaded commit #23140022 as a patch and applied to my core/lib/Drupal/Core/File/FileSystem.php file. After "drush cr", the annoying "The file assets://css was not deleted because it does not exist.
    The file assets://js was not deleted because it does not exist. " warning is gone.

    But I notice that, it still deletes the js & css directories under the assets directory.

    My environment is PHP 8.1, Ubuntu 22.04, nginx and PostgreSQL 15.

    What can I do? Should I reactivate the issue?

    Best,
    Orkut

  • 🇬🇧United Kingdom catch

    @Orkut Murat Yılmaz the directory deletion itself isn't a bug as such - it will be recreated when the next aggregate is written and starts of empty when you install a site / enable aggregation. We don't need to delete the directory though, so a follow-up to try to optimize things so we only delete the files would be fine.

  • 🇮🇹Italy finex

    Hi, I've tried the patch but the css/js folders are not recreated.

  • Status changed to Needs work over 1 year ago
  • 🇹🇷Turkey orkut murat yılmaz Istanbul

    @catch, sorry but the directories are not generated neither after fresh install, nor after enabling aggregation, as @FiNeX mentioned on comment #18.

    I've also tried on another environment (docker4drupal) and the result is the same.

    I'm changing the issue's status, but I'm not sure that I can do it, since it's a core issue. Please correct me, if I'm wrong.

  • 🇮🇹Italy finex

    Hi, after lot of searching I've solved the issue thanks to: https://www.drupal.org/project/drupal/issues/3368769#comment-15118911 🐛 10.1.x breaks Nginx + PHP-FPM with too many redirects even when nginx config is changed Closed: works as designed

  • 🇹🇷Turkey orkut murat yılmaz Istanbul

    After changing my nginx config, as mentioned on @FiNeX's comment #20, nothing has changed.

  • Status changed to Fixed over 1 year ago
  • 🇬🇧United Kingdom catch

    @Orkut Murat Yılmaz please open a new issue for the problem you're having, it's in the same general area as this issue, but this is just about the unnecessary log message.

    The directories are not generated automatically after deletion, they're only generated when an asset URL is requested (i.e. by hitting a page when aggregation is enabled).

    You will need to check the HTML of the page, check the URL of the js and css aggregates, check the headers (for example X-Powered-By Drupal means it was served by PHP), and then see if the file exists on disk.

    If the CSS/JS aggregate looks correct, but it's not written to disk, then it's possible you're running into something like 🐛 Aggregation URL hashes should be built from normalized list of libraries Fixed , although it would be quite extreme for this to happen to every single aggregate. You should also see if you get the same issue with a clean install of Drupal core vs. with any contrib modules you might have installed.

    Lastly, the place that the files get written to disk or not is in system module's AssetControllerBase and the two controllers that extend from it, so if you're comfortable debugging, and everything else seems to be working, you could check where it's getting to in that logic before (not) writing the file.

  • 🇹🇷Turkey orkut murat yılmaz Istanbul

    @catch, thanks for your beautiful & detailed explanation:)

    I'll try the things you've written and if I couldn't solve the error, I'd open a new issue.

    have a nice one and take care:)

  • Automatically closed - issue fixed for 2 weeks with no activity.

  • Status changed to Fixed about 1 year ago
  • 🇹🇷Turkey orkut murat yılmaz Istanbul

    Sorry, I forgot to mention that, it was all about nginx config. Changing it solved the error.

Production build 0.71.5 2024