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
- Create a Drupal site using composer and the drupal/recommended-project project
- Copy example.gitignore to .gitignore, within the web/ directory
- Initialize a git repository in the project root (git init)
- stage the whole project (git add .)
- 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:
- 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
- Make the vendor ignore a separate file, and include instructions to copy it to ~project root~/.gitignore
- 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
Comments
Comment #2
cilefen commentedCan you point me to the instructions that stipulate copying example.gitignore to .gitignore in the web/ directory?
Edited for clarity.
Comment #3
ben coleman commentedThey occur in the first paragraph of comments in the example.gitignore file itself:
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).
Comment #5
ben coleman commentedLooks 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.