Don't let-bind default-directory across switch-project-action#2006
Open
natetarrh wants to merge 1 commit into
Open
Don't let-bind default-directory across switch-project-action#2006natetarrh wants to merge 1 commit into
natetarrh wants to merge 1 commit into
Conversation
projectile-switch-project-by-name wrapped the action call in (let* ((default-directory project-to-switch)) ...). Because default-directory is auto-buffer-local, the let-binding mutated the calling buffer's local default-directory for the duration of the action. When a switch-project-action iterates (buffer-list) and reads each buffer's default-directory via with-current-buffer (e.g. projectile-switch-to-buffer set as projectile-switch-project-action, or any custom action built around projectile-project-buffers), the originating buffer's transiently-mutated default-directory matched the target project's root and the buffer was misclassified as a project buffer. Set default-directory on the temporary buffer the action runs in instead. Same effect (action runs with default-directory pointing at the target root) without leaking into the caller. Adds a buttercup regression test.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
projectile-switch-project-by-namewraps the call toprojectile-switch-project-actionin(let* ((default-directory project-to-switch)) ...). Becausedefault-directoryis auto-buffer-local, the let-binding mutates the calling buffer's buffer-localdefault-directoryfor the duration of the action.Switch-project actions that iterate
(buffer-list)and read each buffer'sdefault-directoryviawith-current-bufferthen misclassify the originating buffer as belonging to the target project. The simplest way to hit this is setting…and pressing
s-p pfrom any buffer that isn't already in the target project: that buffer shows up as a "buffer of the target project" in the picker, even though it isn't, becauseprojectile-project-buffer-preads itsdefault-directorywhile the let-binding is in scope and finds the target root.I hit this with
projectile-switch-to-bufferfrom a local vterm into a/docker:TRAMP project — the local vterm's(file-remote-p default-directory)transiently equaled(file-remote-p "/docker:dev-in-docker:/src/")and the buffer got grouped with the docker project's buffers. I traced it viaadd-variable-watcherondefault-directory; the final unlet event reverts the originating buffer's value, with the call stack rooted inprojectile-switch-project-by-name.Fix
Set
default-directoryon the temporary buffer the action runs in instead of let-binding it in the caller. Same effect (action body seesdefault-directory= target root) without the leak.Nothing else in
projectile-switch-project-by-namereadsdefault-directoryoutside thewith-temp-buffer, so the change is local. All 326 existing specs pass.Test
Added a buttercup regression test that:
default-directoryset to a known dir.projectile-switch-project-actionto a lambda that reads the origin buffer'sdefault-directorywhile the action is running.default-directory, not the project being switched to.Confirmed the test fails on
master(observesproject/) and passes after the fix (observesorigin/).Notes
default-directorybeing the target root, since the temp buffer still carries that value.let*was load-bearing only for thedefault-directoryit set; with that moved intowith-temp-buffer, it collapses to a plainletof one binding.