Skip to content

Commit a13ad93

Browse files
committed
Apply a project's own ignore rules when listing its files
`projectile-project-files' takes a project root, but the ignore rules it filtered by came from the project you were visiting: the chain from `projectile-remove-ignored' and `projectile-dir-files-native' down to `projectile-parse-dirconfig-file' resolved everything against `projectile-project-root' with no way to say which project was meant. That was invisible while every caller asked about the project it was already in. The commands that walk several projects - find-file in the known projects, and now the sibling commands - are the ones it bites, and it bites twice: the listing is wrong, and with caching on the wrong list is stored under the other project's key, so an ordinary find-file there offers ignored files afterwards. Persistent caching writes it to that project's cache file, where it survives a restart. The root now travels as an argument the whole way down.
1 parent 8927963 commit a13ad93

4 files changed

Lines changed: 93 additions & 73 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
### Bugs fixed
2020

21+
- [#2162](https://github.com/bbatsov/projectile/pull/2162): Listing another project's files now applies that project's own ignore rules instead of the rules of whichever project you happen to be visiting. `projectile-find-file-in-known-projects` and the sibling commands used to filter every other project by the current one's dirconfig, and with caching on they stored that wrong list under the other project's key - so an ordinary `projectile-find-file` there kept offering ignored files, and with persistent caching it survived a restart.
2122
- [#2157](https://github.com/bbatsov/projectile/pull/2157): `projectile-use-comint-mode` now covers the named tasks run by `projectile-run-task`, which were always given a read-only compilation buffer however it was set - so a task that needs to ask for a sudo password had nowhere to type one ([#2156](https://github.com/bbatsov/projectile/issues/2156)). Name `task` in the list, or set the option to `t`.
2223

2324
## 3.4.0 (2026-08-10)

‎doc/modules/ROOT/pages/across_repositories.adoc‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,9 @@ a group of your own is a two-line wrapper:
253253
`projectile-search-in-projects` is the same shape for searching.
254254

255255
NOTE: A group search skips the ripgrep fast-path, which runs one `rg` over
256-
one directory tree, so it's slower than searching a single project. And a
257-
project's own `.projectile` ignore rules only apply while you're inside it,
258-
so the other members of a group are filtered by the global ignore rules
259-
alone - long-standing behaviour that
260-
`projectile-find-file-in-known-projects` shares.
256+
one directory tree, so it's slower than searching a single project. Each
257+
project in the group is filtered by its own ignore rules, wherever you
258+
happen to be sitting.
261259

262260
== What else checkouts share
263261

‎projectile.el‎

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3174,7 +3174,7 @@ To enable, set `uniquify-dirname-transform' to this function:
31743174
"Get the list of PROJECT-DIR directories that are of interest to the user.
31753175
When the dirconfig file has no `+' keep entries, return a single-
31763176
element list with PROJECT-DIR itself."
3177-
(let* ((cfg (projectile-parse-dirconfig-file))
3177+
(let* ((cfg (projectile-parse-dirconfig-file (expand-file-name project-dir)))
31783178
(keep (and cfg (projectile-dirconfig-keep cfg))))
31793179
(if keep
31803180
(mapcar (lambda (subdir) (concat project-dir subdir)) keep)
@@ -3184,9 +3184,12 @@ element list with PROJECT-DIR itself."
31843184
"Checks if DIRECTORY is a string designating a valid directory."
31853185
(and (stringp directory) (file-directory-p directory)))
31863186

3187-
(defun projectile-dir-files (directory)
3187+
(defun projectile-dir-files (directory &optional root)
31883188
"List the files in DIRECTORY and in its sub-directories.
3189-
Files are returned as relative paths to DIRECTORY."
3189+
Files are returned as relative paths to DIRECTORY.
3190+
ROOT names the project whose ignore rules apply, defaulting to
3191+
DIRECTORY itself - pass it when DIRECTORY is a subdirectory of the
3192+
project being listed."
31903193
(unless (projectile--directory-p directory)
31913194
(user-error "Directory %S does not exist" directory))
31923195
;; check for a cache hit first if caching is enabled
@@ -3195,19 +3198,21 @@ Files are returned as relative paths to DIRECTORY."
31953198
;; cache disabled or cache miss
31963199
(or files-list
31973200
(pcase projectile-indexing-method
3198-
('native (projectile-dir-files-native directory))
3201+
('native (projectile-dir-files-native directory root))
31993202
;; use external tools to get the project files
32003203
('hybrid (let ((vcs (projectile-project-vcs directory)))
3201-
(projectile-adjust-files directory vcs
3204+
(projectile-adjust-files (or root directory) vcs
32023205
(projectile-dir-files-alien directory vcs))))
32033206
('alien (projectile-dir-files-alien directory))
32043207
(_ (user-error "Unsupported indexing method `%S'" projectile-indexing-method))))))
32053208

32063209
;;; Native Project Indexing
32073210
;;
32083211
;; This corresponds to `projectile-indexing-method' being set to native.
3209-
(defun projectile-dir-files-native (directory)
3210-
"Get the files under DIRECTORY using just Emacs Lisp."
3212+
(defun projectile-dir-files-native (directory &optional root)
3213+
"Get the files under DIRECTORY using just Emacs Lisp.
3214+
ROOT names the project whose ignore rules apply, defaulting to
3215+
DIRECTORY itself."
32113216
(let ((progress-reporter
32123217
(make-progress-reporter
32133218
(format "Projectile is indexing %s"
@@ -3221,7 +3226,9 @@ Files are returned as relative paths to DIRECTORY."
32213226
(prefix-len (length (file-name-as-directory (expand-file-name directory)))))
32223227
;; we need the files with paths relative to the project root
32233228
(mapcar (lambda (file) (substring file prefix-len))
3224-
(projectile-index-directory directory (projectile-filtering-patterns)
3229+
(projectile-index-directory directory
3230+
(projectile-filtering-patterns
3231+
(expand-file-name (or root directory)))
32253232
progress-reporter))))
32263233

32273234
(defun projectile--global-ignore-regexp-p (path)
@@ -4297,19 +4304,25 @@ PROJECT-ROOT defaults to the current project."
42974304

42984305
(defun projectile-adjust-files (project vcs files)
42994306
"First remove ignored files from FILES, then add back unignored files."
4300-
(projectile-add-unignored project vcs (projectile-remove-ignored files)))
4307+
(projectile-add-unignored project vcs (projectile-remove-ignored files project)))
43014308

4302-
(defun projectile-remove-ignored (files)
4309+
(defun projectile-remove-ignored (files &optional root)
43034310
"Remove ignored files and folders from FILES.
43044311

43054312
FILES are paths relative to the project root. They are matched against
43064313
the project's ignore patterns (see `projectile--ignore-patterns'), the
43074314
same gitignore-style rules the native indexer and the alien push-down
4308-
apply; `!' ensure patterns rescue files from them."
4315+
apply; `!' ensure patterns rescue files from them.
4316+
4317+
ROOT names the project whose rules apply, defaulting to the current one.
4318+
Pass it whenever the files being filtered belong to a project other than
4319+
the one you are visiting - listing another project's files otherwise
4320+
filters them by the rules of the project you happen to be in, and with
4321+
caching on stores that wrong answer under the other project's key."
43094322
(let* (;; Ignore matching is case-sensitive; `string-match-p' would
43104323
;; otherwise fold case, since `case-fold-search' defaults to t.
43114324
(case-fold-search nil)
4312-
(filtering-patterns (projectile-filtering-patterns))
4325+
(filtering-patterns (projectile-filtering-patterns root))
43134326
(ignore-re (projectile--compile-ignore-patterns
43144327
(car filtering-patterns)))
43154328
(ensure-re (projectile--compile-ignore-patterns
@@ -4352,7 +4365,8 @@ this case unignored files will be absent from FILES."
43524365
(projectile-keep-ignored-directories
43534366
project
43544367
vcs
4355-
(projectile-unignored-directories-rel)))))
4368+
(projectile-unignored-directories-rel))
4369+
project)))
43564370
(append files unignored-files unignored-paths)))
43574371

43584372
(defun projectile-buffers-with-file (buffers)
@@ -4682,14 +4696,14 @@ directory counts as ignored."
46824696
projectile-globally-ignored-directories
46834697
projectile-globally-unignored-directories))
46844698

4685-
(defun projectile--dirconfig-ignore ()
4686-
"Return the IGNORE entries from the project's dirconfig, or nil."
4687-
(when-let* ((cfg (projectile-parse-dirconfig-file)))
4699+
(defun projectile--dirconfig-ignore (&optional root)
4700+
"Return the IGNORE entries from ROOT's dirconfig, or nil."
4701+
(when-let* ((cfg (projectile-parse-dirconfig-file root)))
46884702
(projectile-dirconfig-ignore cfg)))
46894703

4690-
(defun projectile--dirconfig-ensure ()
4691-
"Return the ENSURE entries from the project's dirconfig, or nil."
4692-
(when-let* ((cfg (projectile-parse-dirconfig-file)))
4704+
(defun projectile--dirconfig-ensure (&optional root)
4705+
"Return the ENSURE entries from ROOT's dirconfig, or nil."
4706+
(when-let* ((cfg (projectile-parse-dirconfig-file root)))
46934707
(projectile-dirconfig-ensure cfg)))
46944708

46954709
(defun projectile-unignored-files ()
@@ -4762,21 +4776,19 @@ ROOT defaults to the current project's root and only matters for the
47624776
dirconfig entries; outside a project the global patterns are returned on
47634777
their own. See `projectile--ignore-pattern-to-regexp' for the matching
47644778
rules."
4765-
(let ((default-directory (or root default-directory)))
4766-
(append
4767-
(mapcar (lambda (name) (concat (directory-file-name name) "/"))
4768-
(projectile-globally-ignored-directory-names))
4769-
(seq-difference projectile-globally-ignored-files
4770-
projectile-globally-unignored-files)
4771-
(projectile--globally-ignored-file-suffixes-glob)
4772-
(projectile--dirconfig-ignore))))
4779+
(append
4780+
(mapcar (lambda (name) (concat (directory-file-name name) "/"))
4781+
(projectile-globally-ignored-directory-names))
4782+
(seq-difference projectile-globally-ignored-files
4783+
projectile-globally-unignored-files)
4784+
(projectile--globally-ignored-file-suffixes-glob)
4785+
(projectile--dirconfig-ignore root)))
47734786

47744787
(defun projectile--ensure-patterns (&optional root)
47754788
"Return ROOT's ensure rules as a list of gitignore-style patterns.
47764789
These are the `!' entries of the project's dirconfig; a path they match
47774790
is kept even when `projectile--ignore-patterns' also matches it."
4778-
(let ((default-directory (or root default-directory)))
4779-
(projectile--dirconfig-ensure)))
4791+
(projectile--dirconfig-ensure root))
47804792

47814793
(defun projectile-filtering-patterns (&optional root)
47824794
"Return ROOT's ignore and ensure patterns as a cons cell."
@@ -4789,9 +4801,11 @@ is kept even when `projectile--ignore-patterns' also matches it."
47894801
(projectile-expand-paths (projectile-files-to-ensure)))))
47904802

47914803

4792-
(defun projectile-dirconfig-file ()
4793-
"Return the absolute path to the project's dirconfig file."
4794-
(expand-file-name projectile-dirconfig-file (projectile-project-root)))
4804+
(defun projectile-dirconfig-file (&optional root)
4805+
"Return the absolute path to ROOT's dirconfig file.
4806+
ROOT defaults to the current project's root."
4807+
(expand-file-name projectile-dirconfig-file
4808+
(or root (projectile-project-root))))
47954809

47964810
(cl-defstruct projectile-dirconfig
47974811
"Parsed contents of a project's dirconfig file.
@@ -4864,10 +4878,11 @@ compatibility but are tracked separately so callers can warn."
48644878
:ensure (nreverse ensure)
48654879
:prefixless-ignore (nreverse prefixless))))
48664880

4867-
(defun projectile--parse-dirconfig-file-uncached ()
4868-
"Parse the dirconfig file without caching.
4869-
Return a `projectile-dirconfig' or nil if the file doesn't exist."
4870-
(let ((dirconfig (projectile-dirconfig-file)))
4881+
(defun projectile--parse-dirconfig-file-uncached (&optional root)
4882+
"Parse ROOT's dirconfig file without caching.
4883+
Return a `projectile-dirconfig' or nil if the file doesn't exist.
4884+
ROOT defaults to the current project."
4885+
(let ((dirconfig (projectile-dirconfig-file root)))
48714886
(when (projectile-file-exists-p dirconfig)
48724887
(projectile--parse-dirconfig-string
48734888
(with-temp-buffer
@@ -4896,8 +4911,11 @@ to nil to silence this warning."
48964911
", "))
48974912
:warning)))
48984913

4899-
(defun projectile-parse-dirconfig-file ()
4900-
"Parse project ignore file and return its rules.
4914+
(defun projectile-parse-dirconfig-file (&optional root)
4915+
"Parse ROOT's ignore file and return its rules.
4916+
ROOT defaults to the current project; pass it to read the rules of a
4917+
project you are not visiting, which is what listing the files of several
4918+
projects at once needs.
49014919

49024920
The return value is a `projectile-dirconfig' struct with three
49034921
slots: KEEP (subdirectories to restrict the project to), IGNORE
@@ -4918,8 +4936,8 @@ are treated as comments.
49184936

49194937
Results are cached per project root and invalidated when the
49204938
dirconfig file's modification time changes."
4921-
(let* ((dirconfig (projectile-dirconfig-file))
4922-
(project-root (projectile-project-root))
4939+
(let* ((project-root (or root (projectile-project-root)))
4940+
(dirconfig (projectile-dirconfig-file project-root))
49234941
(cached (gethash project-root projectile--dirconfig-cache))
49244942
(attrs (file-attributes dirconfig))
49254943
(mtime (when attrs (file-attribute-modification-time attrs)))
@@ -4928,7 +4946,7 @@ dirconfig file's modification time changes."
49284946
(equal cached-dirconfig dirconfig)
49294947
(equal cached-mtime mtime))
49304948
cached-result
4931-
(let ((parsed (projectile--parse-dirconfig-file-uncached)))
4949+
(let ((parsed (projectile--parse-dirconfig-file-uncached project-root)))
49324950
(when mtime
49334951
(puthash project-root
49344952
(list dirconfig mtime parsed)
@@ -5056,7 +5074,7 @@ CALLER is accepted for backward compatibility but no longer used."
50565074
;; equivalent to the batched call above.
50575075
(mapcan
50585076
(lambda (dir)
5059-
(let ((files (projectile-dir-files dir)))
5077+
(let ((files (projectile-dir-files dir project-root)))
50605078
;; `projectile-dir-files' already returns paths
50615079
;; relative to DIR, so when DIR is the project root
50625080
;; itself (the single-directory case - native, or
@@ -14674,12 +14692,8 @@ re-search, the hand-off to the replace reviewer - then covers the whole
1467414692
group.
1467514693

1467614694
Searching more than one project skips the ripgrep fast-path, which runs
14677-
one `rg' over one directory tree. Note also that a project's own
14678-
dirconfig ignores are only applied while you are inside it -
14679-
`projectile-project-files' resolves them against the current project
14680-
rather than the one it was handed - so the other members of a group are
14681-
filtered by the global ignore rules alone. That is long-standing
14682-
behaviour, shared with `projectile-find-file-in-known-projects'."
14695+
one `rg' over one directory tree. Each member of the group is filtered
14696+
by its own ignore rules, wherever you happen to be sitting."
1468314697
(let* ((projects (projectile--project-group projects "projects"))
1468414698
(term (projectile--read-search-string-with-default
1468514699
(or prompt (format "Search %d project%s%s for"

‎test/projectile-project-group-test.el‎

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,26 +104,37 @@ the two truename'd roots and `parent' to the directory holding them."
104104
(expect (seq-filter #'file-name-absolute-p files) :to-equal files)
105105
(expect (sort (mapcar (lambda (f) (file-relative-name f parent)) files)
106106
#'string<)
107-
:to-equal '("alpha/.projectile" "alpha/skip.log"
108-
"alpha/src/a.txt"
107+
:to-equal '("alpha/.projectile" "alpha/src/a.txt"
109108
"beta/.projectile" "beta/lib/b.txt")))))
110109

111-
(it "applies a member's own dirconfig ignores only while you are in it"
112-
;; Long-standing behaviour, not a property of grouping:
113-
;; `projectile-project-files' resolves dirconfig against the current
114-
;; project rather than the one it was handed, so alpha's `-/skip.log'
115-
;; only bites from inside alpha. `projectile-find-file-in-known-projects'
116-
;; has always worked this way. This spec pins it so a fix is a
117-
;; deliberate change rather than a surprise.
110+
(it "applies each member's own dirconfig ignores, wherever you are"
111+
;; alpha's `-/skip.log' has to bite from outside alpha too. It used
112+
;; not to: `projectile-project-files' resolved dirconfig against the
113+
;; current project rather than the one it was handed, so listing a
114+
;; group filtered every member by the rules of whichever project you
115+
;; happened to be visiting - and, with caching on, stored that wrong
116+
;; answer under the member's own key.
118117
(projectile-group-test--with-projects
119118
(let ((skipped (expand-file-name "skip.log" alpha)))
120119
(expect (projectile-project-group-files (list alpha beta))
121-
:to-contain skipped)
120+
:not :to-contain skipped)
122121
(let ((default-directory alpha))
123122
(spy-on 'projectile-project-root :and-return-value alpha)
124123
(expect (projectile-project-group-files (list alpha beta))
125124
:not :to-contain skipped)))))
126125

126+
(it "does not cache a member's files under the rules of another project"
127+
;; The damaging half: the wrong list used to be stored under the other
128+
;; project's key, so an ordinary find-file in it offered ignored files
129+
;; afterwards - and with persistent caching that survived a restart.
130+
(projectile-group-test--with-projects
131+
(let ((projectile-enable-caching t))
132+
(spy-on 'projectile-project-root :and-return-value beta)
133+
(let ((default-directory beta))
134+
(projectile-project-group-files (list alpha beta)))
135+
(expect (gethash alpha projectile-projects-cache)
136+
:not :to-contain "skip.log"))))
137+
127138
(it "skips a project that has been moved away"
128139
(projectile-group-test--with-projects
129140
(expect (projectile-project-group-files
@@ -157,8 +168,7 @@ the two truename'd roots and `parent' to the directory holding them."
157168
(projectile-test-use-plain-grep)
158169
(let ((buf (projectile-group-test--search (list alpha beta) "needle")))
159170
(expect (projectile-test-match-files buf)
160-
:to-equal '("alpha/skip.log" "alpha/src/a.txt"
161-
"beta/lib/b.txt")))))
171+
:to-equal '("alpha/src/a.txt" "beta/lib/b.txt")))))
162172

163173
(it "names the matches relative to the directory holding the group"
164174
(projectile-group-test--with-projects
@@ -181,8 +191,7 @@ the two truename'd roots and `parent' to the directory holding them."
181191
(projectile-replace--regather)
182192
(expect (length projectile-replace--matches) :to-equal before)
183193
(expect (projectile-test-match-files buf)
184-
:to-equal '("alpha/skip.log" "alpha/src/a.txt"
185-
"beta/lib/b.txt")))))))
194+
:to-equal '("alpha/src/a.txt" "beta/lib/b.txt")))))))
186195

187196
(it "scopes back to one project when the buffer is reused for a plain search"
188197
(projectile-group-test--with-projects
@@ -221,7 +230,7 @@ the two truename'd roots and `parent' to the directory holding them."
221230
;; `g' must re-scan what is left, not abort on the missing member
222231
(projectile-replace--regather)
223232
(expect (projectile-test-match-files buf)
224-
:to-equal '("alpha/skip.log" "alpha/src/a.txt"))))))
233+
:to-equal '("alpha/src/a.txt"))))))
225234

226235
(it "refuses an empty group instead of searching the whole filesystem"
227236
(projectile-group-test--with-projects
@@ -244,8 +253,7 @@ the two truename'd roots and `parent' to the directory holding them."
244253
(expect (sort (mapcar (lambda (f) (file-relative-name f parent))
245254
(cadr (spy-calls-args-for 'projectile-completing-read 0)))
246255
#'string<)
247-
:to-equal '("alpha/.projectile" "alpha/skip.log"
248-
"alpha/src/a.txt"
256+
:to-equal '("alpha/.projectile" "alpha/src/a.txt"
249257
"beta/.projectile" "beta/lib/b.txt"))))))
250258

251259
(describe "projectile-find-file-in-sibling-projects"
@@ -257,7 +265,7 @@ the two truename'd roots and `parent' to the directory holding them."
257265
(spy-on 'find-file)
258266
(projectile-find-file-in-sibling-projects)
259267
(expect (length (cadr (spy-calls-args-for 'projectile-completing-read 0)))
260-
:to-equal 5)))
268+
:to-equal 4)))
261269

262270
(it "says so when the project has no siblings"
263271
(projectile-group-test--with-projects
@@ -275,8 +283,7 @@ the two truename'd roots and `parent' to the directory holding them."
275283
(projectile-search-in-sibling-projects))
276284
(expect (projectile-test-match-files
277285
(get-buffer projectile-search-buffer-name))
278-
:to-equal '("alpha/skip.log" "alpha/src/a.txt"
279-
"beta/lib/b.txt")))))
286+
:to-equal '("alpha/src/a.txt" "beta/lib/b.txt")))))
280287

281288
(provide 'projectile-project-group-test)
282289

0 commit comments

Comments
 (0)