Problem/Motivation

When installing Drupal via composer, using drupal/recommended-project, where drupal itself is installed in the web/ directory under the project root, and using git for source version control (having followed directions and copied example.gitignore to .gitignore in the web/ directory), the vendor directory in the project root directory is not ignored.

It appears to me that it is assumed that by specifying the ignore of the vendor directory with a leading separator (/vendor/), it will be relative to the project root, rather than the directory the .gitignore file is in. This, however is not how git works. According to the git documentation (https://git-scm.com/docs/gitignore):

The slash / is used as the directory separator. Separators may occur at the beginning, middle or end of the .gitignore search pattern.

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.


Thus the gitignore specification '/vendor/' will ignore a vendor directory in the web/ directory, not in the root directory.

Steps to reproduce

  1. Create a Drupal site using composer and the drupal/recommended-project project
  2. Copy example.gitignore to .gitignore, within the web/ directory
  3. Initialize a git repository in the project root (git init)
  4. stage the whole project (git add .)
  5. commit the staged files (git commit -m "Initial version")

The resulting commit will include files in the project root vendor directory.

Proposed resolution

Possible approaches to fix this:

  1. Adjust the paths in the example.gitignore file to be from project root, and change the instructions to be to copy to ~project root~/.gitignore
  2. Make the vendor ignore a separate file, and include instructions to copy it to ~project root~/.gitignore
  3. Adjust the paths in the example.gitignore file to be from project root, and have it installed in project root by drupal/recommended-project. In this case, the current example.gitignore might be left in place, to be used when Drupal is installed by methods other than composer

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Ben Coleman created an issue. See original summary.

cilefen’s picture

Can you point me to the instructions that stipulate copying example.gitignore to .gitignore in the web/ directory?

Edited for clarity.

ben coleman’s picture

They occur in the first paragraph of comments in the example.gitignore file itself:

# This file contains default .gitignore rules. To use it, copy it to .gitignore,
# and it will cause files like your settings.php and user-uploaded files to be
# excluded from Git version control. This is a common strategy to avoid
# accidentally including private information in public repositories and patch
# files.

As the example.gitignore file gets installed in the web/ directory (at least in sites installed via composer (I'm assuming there are still people out there who are installing Drupal 9 without composer)), this seems to imply copying to .gitignore in the same directory. Also, the other (uncommented) settings in example.gitignore won't work unless the copied .gitignore is in the web/ directory (they all begin 'sites/*', and according to the git rules referenced above, they are only relative to the same directory the .gitignore is in. Hence, they won't work if example.gitignore is copied to .gitignore in the project root).

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

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should 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.

ben coleman’s picture

Looks like this is at least mentioned in the general .gitignore issue. At least there's a comment that the /vendor/ line won't work unless the .gitignore is in the project root.

Version: 9.5.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. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.