Problem/Motivation

Currently, file_system service depends on three other services:

  file_system:
    class: Drupal\Core\File\FileSystem
    arguments: ['@stream_wrapper_manager', '@settings', '@logger.channel.file']

Inside of Drupal\Core\File\FileSystem class, instance of logger is used only once in the chmod() method to log error when chmod() PHP function returns FALSE.

Not only this is a side effect, but an instance of logger.channel.file contains references to other services: requests stack, current user object. And from the source of Drupal\Core\Logger\LoggerChannel::log() method, it looks like it can even execute a database query.

Logger makes dependency tree of file_system service unnecessary complicated, and I would expect such a low-level service to have as little side-effects as possible.

Remaining tasks

None

User interface changes

None.

API changes

Change of Drupal\Core\File\FileSystem::__construct() signature.
FileSystem no longer logs any errors. This is up to the caller.

Data model changes

None.

Issue fork drupal-2838474

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

20th created an issue. See original summary.

20th’s picture

Title: Remove dependency of <code>file_system</code> service on <code>logger</code> » Remove dependency of "file_system" service on "logger"

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

andypost’s picture

ndobromirov’s picture

I vote for solution 1 as the cleaner OO based one.

mxr576’s picture

+1 for solution 1

fgm’s picture

Wouldn't it be better not to throw but to just have the function return an error value instead ? (like traditional C/UNIX I/O).

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new10.85 KB

Here's an initial go at solution 1.

ndobromirov’s picture

+++ b/core/lib/Drupal/Core/Template/Attribute.php
@@ -357,3 +357,4 @@ public function jsonSerialize() {
+w

Seems to be breaking the patch :D

kim.pepper’s picture

StatusFileSize
new10.55 KB
new306 bytes

Oops!

andypost’s picture

  1. +++ b/core/lib/Drupal/Core/File/FileSystem.php
    @@ -63,10 +57,12 @@ class FileSystem implements FileSystemInterface {
        * @param \Psr\Log\LoggerInterface $logger
        *   The file logger channel.
    ...
    -  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger) {
    +  public function __construct(StreamWrapperManagerInterface $stream_wrapper_manager, Settings $settings, LoggerInterface $logger = NULL) {
    

    php doc needs to point Optional for logger

  2. +++ b/core/lib/Drupal/Core/File/FileSystem.php
    @@ -752,9 +713,6 @@ protected function doScanDirectory($dir, $mask, array $options = [], $depth = 0)
    -    else {
    -      $this->logger->error('@dir can not be opened', ['@dir' => $dir]);
    -    }
    

    the only regression place

Status: Needs review » Needs work

The last submitted patch, 14: 2838474-14.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new14.33 KB
new4.35 KB

Thanks @andypost.

  1. Fixed
  2. Not sure what do here? I don't think it makes sense to throw an exception.

This patch should fix a few test fails related to the manual registration of the FileSystem service in install.core.inc.

Status: Needs review » Needs work

The last submitted patch, 17: 2838474-117.patch, failed testing. View results

kim.pepper’s picture

Status: Needs work » Needs review
StatusFileSize
new15.1 KB
new790 bytes

Fix test fails in FileSystemDeprecationTest.

andypost’s picture

Issue tags: +blocker

It blocks further fixes for file system and config storage

kim.pepper’s picture

Had a chat about this with @larowlan and couldn't find anything about a bc compatibility policy for logging. Given we are throwing exceptions, I think this means we are covered for the most part.

The only outlier is:

+++ b/core/lib/Drupal/Core/File/FileSystem.php
@@ -752,9 +713,6 @@ protected function doScanDirectory($dir, $mask, array $options = [], $depth = 0)
-    else {
-      $this->logger->error('@dir can not be opened', ['@dir' => $dir]);
-    }

...which currently logs an error when scanning a directory tree and gets to a directory it can't open.

ndobromirov’s picture

A new exception should work in that case as well.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

kim.pepper’s picture

Re-roll of #19

kim.pepper’s picture

StatusFileSize
new14.35 KB

Forgot to attach the file!

kim.pepper’s picture

Re: #22

A new exception should work in that case as well.

I don't think it will, because that will halt the scan. Currently it just logs a warning and continues.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

chi’s picture

Inside of Drupal\Core\File\FileSystem class, instance of logger is used only once in the chmod() method to log error when chmod() PHP function returns FALSE.

The issues is quite old. At the moment, file system makes a heavy use of the logger.

rpayanm made their first commit to this issue’s fork.

rpayanm’s picture

Status: Needs work » Needs review

Please review.

kim.pepper’s picture

Status: Needs review » Needs work

NW for MR feedback above.

alexpott’s picture

I think given that we have code like

if (is_dir($directory) && !is_writable($directory) && !$file_system->chmod($directory)) {

we should go for solution 2 as the less disruptive for now. And leave it up to the calling code to log if necessary. In the above example, logging is actually not appropriate as this is used to inform the user.

alexpott’s picture

Hiding patches as we have an MR now.

alexpott’s picture

Status: Needs work » Needs review

I've gone for a halfway house for #40 and added $throw to \Drupal\Core\File\FileSystem::chmod() so the caller is in control.

I don't think there are any use-cases for $throw in core but I've ensured there is test coverage.

alexpott’s picture

Issue summary: View changes

Updated issue summary with latest changes.

mfb’s picture

I updated the change record to more completely describe the impacts of logging being removed. Now I realize it needs a bit more work as I see @alexpott added an additional new change record, so they are partially redundant.

longwave’s picture

Why do we need the $throw argument, the caller can detect failure via the return value already? We're already inconsistent where some methods throw exceptions and others return FALSE, but this doesn't make it any better.

alexpott’s picture

Issue summary: View changes

@longwave yeah I went that way originally but then got cold feet when deleting the exception.. but I agree - let's remove $throw.

longwave’s picture

If we think it's worth cleaning up, perhaps in a followup we can add deprecations in all the cases we currently return FALSE and notify users that we will start throwing exceptions instead in the next major.

alexpott’s picture

@longwave I don't think we want that though - in the case of chod there are times that throwing an exception would lead to way more complex calling code.

mfb’s picture

@alexpott I'd say it should still be mentioned in the change record that logging is removed from those other methods, not just chmod. But you removed my mention of those. This is relevant if the calling code caught the exception and assumed an error was already logged by the file_system service.

alexpott’s picture

@mfb I've listed the public method that no longer log in the CR and detailed that it is the callers responsibility to log if any method results in FALSE or throws an exception.

kim.pepper’s picture

Status: Needs review » Needs work
longwave’s picture

Status: Needs work » Needs review

Fixed deprecation, also fixed a nit in the docblock.

#3416354: Inject services into LoggerChannelFactory is somewhat related, there is special code in LoggerChannelFactory to handle the case where the injected dependencies are not present (during the installer, because it needs file_system), once this dependency is gone I think that can also be removed.

spokje’s picture

Issue tags: +Needs followup

Tagging with Needs followup for #52

spokje’s picture

Status: Needs review » Reviewed & tested by the community

Code changes make sense and are primarily removing logging/loggers and altering comments about logging/loggers.
Checked CR and that mentions all methods involved in not being logged any more.
Tests are green.

For me this is RTBC.

longwave’s picture

Issue tags: -Needs followup

The followup isn't really needed until both issues land, just they overlap a bit, so we can probably fix it directly in whichever lands second - removing the tag.

spokje’s picture

I'm glad somebody is an optimist and is sure we'll remember ;)

longwave’s picture

Well it's only a bit of dead code if we don't. And #3416697: Remove install container definitions of FileSystem and StreamWrapperManager is making me think that this might not even get used...

socialnicheguru’s picture

I am saying that I thought that this issue would help me solve circular recurrence. But it did not.

I applied this #2838474: Remove dependency of "file_system" service on "logger" I get this:
Circular reference detected for service "workspaces.manager", path: "scheduler.manager -> dat
e.formatter -> workspaces.manager -> logger.channel.workspaces -> logger.factory -> logger.syslog".

I have filecache, workspace, and scheduler enabled.

this issue solved this problem for me #3103620: Dependency on config storage causes circular reference in service container

This comment, https://www.drupal.org/project/drupal/issues/3103620#comment-15408346, redirected to this issue for a fix. I do not think it is a fx.

spokje’s picture

#58: You might have c/p a wrong issue link (it is to this very issue) in there?

alexpott’s picture

@SocialNicheGuru we're going to need some more information about how to reproduce what you are seeing. I have installed:

  1. Standard profile
  2. Enabled scheduler, workspaces, filecache
  3. Configured filecache
  4. Enabled syslog
  5. Boom things are broken
  6. Applied this MR and cleared the cached - things are fixed

I can create the datetime.formatter and workspaces.manager services just fine after this. But my datetime.formatter does not depend on workspaces.manager - how is that happening on your install?

This MR is definitely resolving a bug.

catch’s picture

Status: Reviewed & tested by the community » Needs work

One question on the MR.

kim.pepper’s picture

Status: Needs work » Needs review
alexpott’s picture

Status: Needs review » Reviewed & tested by the community

@catch's feedback has been addressed. This looks good to go now. Thanks @kim.pepper.

catch’s picture

Version: 11.x-dev » 10.3.x-dev
Status: Reviewed & tested by the community » Fixed

Committed/pushed to 11.x and cherry-picked to 10.3.x, thanks!

  • catch committed 9e377871 on 10.3.x
    Issue #2838474 by kim.pepper, rpayanm, alexpott, longwave, Spokje,...

  • catch committed 84fcf292 on 11.x
    Issue #2838474 by kim.pepper, rpayanm, alexpott, longwave, Spokje,...

Status: Fixed » Closed (fixed)

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

mfb’s picture

By the way, I noticed that at least one contrib file system decorator (s3fs) still has a dependency on logger. And logging seems even more useful/necessary for s3fs than the default local file_system (all kinds of API errors, network errors, etc.). So I still see some utility in the alternative work-around of a lazy config service that loggers could depend on (#3103620: Dependency on config storage causes circular reference in service container)