From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Alex =?utf-8?Q?Benn=C3=A9e?= Newsgroups: gmane.emacs.devel Subject: Problem with compilation-start resetting default-directory Date: Tue, 19 Nov 2019 14:10:13 +0000 Message-ID: <87y2wcez3u.fsf@linaro.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="150975"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: mu4e 1.3.5; emacs 27.0.50 To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue Nov 19 15:10:37 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1iX4DF-000d1z-8h for ged-emacs-devel@m.gmane.org; Tue, 19 Nov 2019 15:10:37 +0100 Original-Received: from localhost ([::1]:45890 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iX4D8-0003Zx-DX for ged-emacs-devel@m.gmane.org; Tue, 19 Nov 2019 09:10:30 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:41420) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iX4D1-0003YX-5U for emacs-devel@gnu.org; Tue, 19 Nov 2019 09:10:25 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iX4Cy-0004AZ-U5 for emacs-devel@gnu.org; Tue, 19 Nov 2019 09:10:23 -0500 Original-Received: from mail-wm1-x331.google.com ([2a00:1450:4864:20::331]:51237) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iX4Cy-00049U-K1 for emacs-devel@gnu.org; Tue, 19 Nov 2019 09:10:20 -0500 Original-Received: by mail-wm1-x331.google.com with SMTP id q70so3343771wme.1 for ; Tue, 19 Nov 2019 06:10:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=user-agent:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=VHHgsQvqAaVNogiwn0Y/ZndQsL459ByfmxY+mVfPH8c=; b=eemi+FnyI5mSRWcBi6XO0UpbDRZxy3qARe+VEW4zfebJ6p3ndJCbIUTX1J2dceJFPv JCVZ7bMQsuFwNSukyjJhqh6WpmN3jwdQhCtVmElssk9JNEUGK8kQ8DNbkfCtHvBIDj40 b06bceyOFx+okoHEnKH6IM9aecHOUWbrC3AZEWvHYmilPrSJUjs202PV8Jvk1qBDP6DY fDlaz4cFhoxFhfV9cVXdUHKPXWbyK1CeAbOaIhDiNa12Zt+QN5wx3Ye1ovkh/AyFzItt OWeYz1u75VTzIbsE/D6Lk8UiYYt6s1ynVfhZP0SIYokDc+9yo2MdDDx+3342KVjac4M9 v3OA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:user-agent:from:to:subject:date:message-id :mime-version:content-transfer-encoding; bh=VHHgsQvqAaVNogiwn0Y/ZndQsL459ByfmxY+mVfPH8c=; b=kgMw2c90zLH5g4ygAW5iTYOx6UFjf1WWhrSbu6DF0pL3cAHHTnmzS2YnTs6gXr2kEX Fo0KOfOpKQxbmlUWFO/Z5/KpRHHK1Izn0mUypzar24LIcAEbjo7QYEZrB1MeW49EXAdm l/OEssdz09oyS1zExl0vjI1JGp4trtvB45G6HFscakx8NdnmtkxtZoLkW3vPQL50pZ25 53f0WJWqr+QJCysfiBt/+coNKxw4V50OXFhaC+RTxGFfFKn8vsSySzpgfok/BwRasrYr RiIejAwKR3q1scvhaKGyTWSdR9NHbzPg72Z8QplgP/zdrn0g0ZiU4LKmeu44psTg5FFe PCAQ== X-Gm-Message-State: APjAAAWcZkZZ2ujy9OtK1rrdVTviOXG7YPu+M5lu7H3LqhYPdmiN3iOI IpftX91JhB42BMWs3687Im89F33B2co= X-Google-Smtp-Source: APXvYqyyp3v9YVdDJInkaAGFfzhWwwoyTOmJNW0H+TOwJULW0oA08ZlUScYwVKqqQlJuXrpF6PBFyQ== X-Received: by 2002:a1c:3dc4:: with SMTP id k187mr5730970wma.167.1574172616680; Tue, 19 Nov 2019 06:10:16 -0800 (PST) Original-Received: from zen.linaroharston ([51.148.130.216]) by smtp.gmail.com with ESMTPSA id h205sm3385412wmf.35.2019.11.19.06.10.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Nov 2019 06:10:15 -0800 (PST) Original-Received: from zen (localhost [127.0.0.1]) by zen.linaroharston (Postfix) with ESMTP id 2C7231FF87 for ; Tue, 19 Nov 2019 14:10:13 +0000 (GMT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::331 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:242429 Archived-At: Hi, For a while I've been trying to track down an annoying bug where the default-directory of my code/magit buffers was getting reset. Thanks to the power of add-variable-watcher I've finally managed to track the culprit down to compilation-start however I'm unsure what is going on. I've instrumented compilation-start to illustrate the problem: (defun compilation-start (command &optional mode name-function highlight-re= gexp) "Run compilation command COMMAND (low level interface). If COMMAND starts with a cd command, that becomes the `default-directory'. The rest of the arguments are optional; for them, nil means use the default. MODE is the major mode to set in the compilation buffer. Mode may also be t meaning use `compilation-shell-minor-mode' under `comint-mode= '. If NAME-FUNCTION is non-nil, call it with one argument (the mode name) to determine the buffer name. Otherwise, the default is to reuses the current buffer if it has the proper major mode, else use or create a buffer with name based on the major mode. If HIGHLIGHT-REGEXP is non-nil, `next-error' will temporarily highlight the matching section of the visited source line; the default is to use the global value of `compilation-highlight-regexp'. Returns the compilation buffer created." (or mode (setq mode 'compilation-mode)) (let* ((name-of-mode (if (eq mode t) "compilation" (replace-regexp-in-string "-mode\\'" "" (symbol-name mode)))) (thisdir default-directory) (thisenv compilation-environment) outwin outbuf) (with-current-buffer (setq outbuf (get-buffer-create (compilation-buffer-name name-of-mode mode name-function))) (message "outbuf is %s" outbuf) (let ((comp-proc (get-buffer-process (current-buffer)))) (if comp-proc (if (or (not (eq (process-status comp-proc) 'run)) (eq (process-query-on-exit-flag comp-proc) nil) (yes-or-no-p (format "A %s process is running; kill it? " name-of-mode))) (condition-case () (progn (interrupt-process comp-proc) (sit-for 1) (delete-process comp-proc)) (error nil)) (error "Cannot have two processes in `%s' at once" (buffer-name))))) ;; first transfer directory from where M-x compile was called (message "set dd1 in %s" (current-buffer)) (setq default-directory thisdir) ;; Make compilation buffer read-only. The filter can still write it. ;; Clear out the compilation buffer. (let ((inhibit-read-only t) (default-directory thisdir)) ;; Then evaluate a cd command if any, but don't perform it yet, else ;; start-command would do it again through the shell: (cd "..") AND ;; sh -c "cd ..; make" (cd (cond ((not (string-match "\\`\\s *cd\\(?:\\s +\\(\\S +?\\|'[^']*'\\= |\"\\(?:[^\"`$\\]\\|\\\\.\\)*\"\\)\\)?\\s *[;&\n]" command)) default-directory) ((not (match-end 1)) "~") ((eq (aref command (match-beginning 1)) ?\') (substring command (1+ (match-beginning 1)) (1- (match-end 1)))) ((eq (aref command (match-beginning 1)) ?\") (replace-regexp-in-string "\\\\\\(.\\)" "\\1" (substring command (1+ (match-beginning 1)) (1- (match-end 1))))) ;; Try globbing as well (bug#15417). (t (let* ((substituted-dir (substitute-env-vars (match-string 1 command))) ;; FIXME: This also tries to expand `*' that were ;; introduced by the envvar expansion! (expanded-dir (file-expand-wildcards substituted-dir))) (if (=3D (length expanded-dir) 1) (car expanded-dir) substituted-dir))))) (erase-buffer) ;; Select the desired mode. (if (not (eq mode t)) (progn (buffer-disable-undo) (funcall mode)) (setq buffer-read-only nil) (with-no-warnings (comint-mode)) (compilation-shell-minor-mode)) ;; Remember the original dir, so we can use it when we recompile. ;; default-directory' can't be used reliably for that because it ma= y be ;; affected by the special handling of "cd ...;". ;; NB: must be done after (funcall mode) as that resets local varia= bles (set (make-local-variable 'compilation-directory) thisdir) (set (make-local-variable 'compilation-environment) thisenv) (if highlight-regexp (set (make-local-variable 'compilation-highlight-regexp) highlight-regexp)) (if (or compilation-auto-jump-to-first-error (eq compilation-scroll-output 'first-error)) (set (make-local-variable 'compilation-auto-jump-to-next) t)) ;; Output a mode setter, for saving and later reloading this buffer. (insert "-*- mode: " name-of-mode "; default-directory: " (prin1-to-string (abbreviate-file-name default-directory)) " -*-\n" (format "%s started at %s\n\n" mode-name (substring (current-time-string) 0 19)) command "\n") (message "set dd2 in %s" (current-buffer)) (setq thisdir default-directory)) (set-buffer-modified-p nil)) ;; Pop up the compilation buffer. ;; https://lists.gnu.org/r/emacs-devel/2007-11/msg01638.html (setq outwin (display-buffer outbuf '(nil (allow-no-window . t)))) (with-current-buffer outbuf (message "wcb @ %s" outbuf) (let ((process-environment (append compilation-environment (comint-term-environment) (list (format "INSIDE_EMACS=3D%s,compile" emacs-version)) (copy-sequence process-environment)))) (set (make-local-variable 'compilation-arguments) (list command mode name-function highlight-regexp)) (set (make-local-variable 'revert-buffer-function) 'compilation-revert-buffer) (and outwin ;; Forcing the window-start overrides the usual redisplay ;; feature of bringing point into view, so setting the ;; window-start to top of the buffer risks losing the ;; effect of moving point to EOB below, per ;; compilation-scroll-output, if the command is long ;; enough to push point outside of the window. This ;; could happen, e.g., in `rgrep'. (not compilation-scroll-output) (set-window-start outwin (point-min))) ;; Position point as the user will see it. (let ((desired-visible-point ;; Put it at the end if `compilation-scroll-output' is set. (if compilation-scroll-output (point-max) ;; Normally put it at the top. (point-min)))) (goto-char desired-visible-point) (when (and outwin (not (eq outwin (selected-window)))) (set-window-point outwin desired-visible-point))) ;; The setup function is called before compilation-set-window-height ;; so it can set the compilation-window-height buffer locally. (if compilation-process-setup-function (funcall compilation-process-setup-function)) (and outwin (compilation-set-window-height outwin)) ;; Start the compilation. (if (fboundp 'make-process) (let ((proc (if (eq mode t) ;; On remote hosts, the local `shell-file-name' ;; might be useless. (with-connection-local-variables ;; comint uses `start-file-process'. (get-buffer-process (with-no-warnings (comint-exec outbuf (downcase mode-name) shell-file-name nil `(,shell-command-switch ,command))))) (start-file-process-shell-command (downcase mode-name) outbuf command)))) ;; Make the buffer's mode line show process state. (setq mode-line-process '((:propertize ":%s" face compilation-mode-line-run) compilation-mode-line-errors)) ;; Set the process as killable without query by default. ;; This allows us to start a new compilation without ;; getting prompted. (when compilation-always-kill (set-process-query-on-exit-flag proc nil)) (set-process-sentinel proc #'compilation-sentinel) (unless (eq mode t) ;; Keep the comint filter, since it's needed for proper ;; handling of the prompts. (set-process-filter proc #'compilation-filter)) ;; Use (point-max) here so that output comes in ;; after the initial text, ;; regardless of where the user sees point. (set-marker (process-mark proc) (point-max) outbuf) (when compilation-disable-input (condition-case nil (process-send-eof proc) ;; The process may have exited already. (error nil))) (run-hook-with-args 'compilation-start-hook proc) (compilation--update-in-progress-mode-line) (push proc compilation-in-progress)) ;; No asynchronous processes available. (message "Executing `%s'..." command) ;; Fake mode line display as if `start-process' were run. (setq mode-line-process '((:propertize ":run" face compilation-mode-line-run) compilation-mode-line-errors)) (force-mode-line-update) (sit-for 0) ; Force redisplay (save-excursion ;; Insert the output at the end, after the initial text, ;; regardless of where the user sees point. (goto-char (point-max)) (let* ((inhibit-read-only t) ; call-process needs to modify outbuf (compilation-filter-start (point)) (status (call-process shell-file-name nil outbuf nil "-c" command))) (run-hooks 'compilation-filter-hook) (cond ((numberp status) (compilation-handle-exit 'exit status (if (zerop status) "finished\n" (format "exited abnormally with code %d\n" status)))) ((stringp status) (compilation-handle-exit 'signal status (concat status "\n"))) (t (compilation-handle-exit 'bizarre status status))))) (set-buffer-modified-p nil) (message "Executing `%s'...done" command))) ;; Now finally cd to where the shell started make/grep/... (message "set dd3 to %s in %s" thisdir (current-buffer)) (setq default-directory thisdir) ;; The following form selected outwin ever since revision 1.183, ;; so possibly messing up point in some other window (bug#1073). ;; Moved into the scope of with-current-buffer, though still with ;; complete disregard for the case when compilation-scroll-output ;; equals 'first-error (martin 2008-10-04). (when compilation-scroll-output (goto-char (point-max)))) ;; Make it so the next C-x ` will use this buffer. (setq next-error-last-buffer outbuf))) And an example output is: outbuf is *compilation* set dd1 in *compilation* set dd2 in *compilation* wcb @ *compilation* set dd3 to /home/alex/lsrc/qemu.git/builds/all/ in backtrace.el.gz This is confusing as the final setq default-directory is wrapped inside a with-current-buffer form which is correct at according to the log. Any ideas what could be happening? I guess there is nothing to stop something inside the form changing the current active buffer? Any tips on how to track this down would be appreciated. Currently running Emacs HEAD @ 0415ad210c8c99e771b6273b6f683fe3f3614c6f but this problem has been occurring for a while. -- Alex Benn=C3=A9e