Generated .htaccess files should have a trailing line break

Created on 13 February 2013, almost 12 years ago
Updated 8 June 2023, over 1 year ago

Problem/Motivation

To comply with the Drupal coding standards the generated .htaccess files in the public and private files directories should end in a trailing \n character.

All text files should end in a single newline (\n). This avoids the verbose "\ No newline at end of file" patch warning and makes patches easier to read since it's clearer what is being changed when lines are added to the end of a file.

From Indenting and Whitespace

The comment about "No newline at end of file" probably isn't relevant as you wouldn't be patching generated content, but it is slightly confusing when you use the cat .htaccess command there is no new line before the next command.

Patches for D8 and D7 are attached.

Steps to reproduce

Proposed resolution

Adds the new lines where relevant and change writeFile() to include a newline.

Remaining tasks

Update to 10.0 and sort out test failures

User interface changes

API changes

Data model changes

Release notes snippet

📌 Task
Status

Postponed

Version

11.0 🔥

Component
File system 

Last updated 1 day ago

Created by

🇦🇺Australia mstrelan

Live updates comments and jobs are added and updated live.
  • Coding standards

    It involves compliance with, or the content of coding standards. Requires broad community agreement.

  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • 🇮🇳India chaitanyadessai Goa

    Please review patch.

  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Seems reroll is missing some changes.

  • 🇨🇦Canada jodavidson

    Going to take a swing at this one at the Mentored Contribution session at DrupalCon2023 Pittsburgh

  • 🇨🇦Canada jodavidson

    Looking at the crucial method writeFile($directory, $filename, $contents, $force) the code as included in the #42 patch is complete.

    All cases are handled and the method will always return a boolean. The additional tests from #34 ((file_exists($directory) && is_writable($directory)) are implied in the @file_put_contents() call.

    This should work fine as written.

    I'm now looking at how to efficiently test that it is indeed working in the DrupalPod environment.

  • 🇨🇦Canada jodavidson

    Unfortunately I have not been able to create a situation where this code fires and a .htaccess file is created so testing is still required.

  • 🇺🇸United States Amber Himes Matz Portland, OR USA

    1.

    +++ b/core/lib/Drupal/Component/FileSecurity/FileSecurity.php
    @@ -157,7 +157,7 @@ protected static function writeFile($directory, $filename, $contents, $force) {
    +    if (@file_put_contents($file_path, $contents ."\n")) {
    

    Coding standards nit. Should be a space after the concatenation dot.

    2. And, it needs another reroll.

    3. @mstrelan -- Can you give reviewers a clue on how to manually test your patches? Looks like there's some confusion there.

    4. I'll add a Drupal 10 test run for the other patch (1915772-34-generated-htaccess-new-line.patch) in #34.

    5. Hiding some files. It's confusing because we're dealing with 2 files. (I'm not entirely clear on why that is -- maybe because one is dynamically generated.)

  • Open in Jenkins → Open on Drupal.org →
    Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    Patch Failed to Apply
  • 🇦🇺Australia mstrelan

    #47.3

    This can be tested by deleting the web/sites/default/files directory and running drush site:install minimal, then run something like tail web/sites/default/files/.htaccess. If it worked you'll get a line break after </IfModule>.

    You can also test the private file system by adding something like this to settings.php
    $settings['file_private_path'] = 'sites/default/files/private';

    Then ensure the directory exists with this
    mkdir web/sites/default/files/private

    Then go to the status report page at /admin/reports/status which should trigger the .htaccess file to be generated in the private directory.

    But as we discussed on slack, it might be worth investigating if we can replace \Drupal\Component\FileSecurity\FileSecurity::writeFile with a simple file copy from core/assets/scaffold/files so we don't need the file contents to live in php code.

  • Status changed to Postponed over 1 year ago
  • 🇦🇺Australia mstrelan

    Postponing on 📌 Update FileSecurity class to copy files from scaffold directory Needs work which should solve this.

Production build 0.71.5 2024