Emacs is ignoring my path when it runs a compile c

2019-01-14 03:48发布

I'm trying to get a compile command (rake cucumber) to run with a specific ruby version on my Mac OS X system, I use rvm to do this currently in the terminal. My ~/.MacOSX/environment.plist has the correct path in it, but emacs insists on prepending to this path and therefore making it useless. I've also tried:

(when (equal system-type 'darwin)
  (setenv "PATH" (concat "/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin:/Users/fearoffish/.rvm/bin"))
  (push "/Users/fearoffish/.rvm/bin" exec-path)
  (push "/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin" exec-path)
  (push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin" exec-path)
  (push "/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin" exec-path)
  (push "/Users/fearoffish/.rvm/bin" exec-path))

It was the desperate attempt of an emacs beginner to get what I wanted. It still prepends in front of it, so my path ends up being:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Users/fearoffish/.rvm/bin:/Users/fearoffish/.rvm/rubies/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249/bin:/Users/fearoffish/.rvm/gems/ruby-1.8.7-p249%global/bin

I don't want /usr/bin and others prepending, I want my path first and the emacs prepended path to be at the end, I reckon this would fix my problem.

I test this by simply opening Aquamacs and running meta-x compile and then echo $PATH.

Any ideas?

8条回答
放荡不羁爱自由
2楼-- · 2019-01-14 03:54

It worked for me with two things.

First I followed sanityinc advice

An improved and modified version of the code snippet is now published as elisp library called exec-path-from-shell; installable packages are available in Marmalade and Melpa

I still had a problem with compile commands. Valko Sipuli is right there was a problem involving path_helper.

I commented the corresponding line in /etc/profile and it did not help. Problem still there. I don't use bash but zsh. Digging a little I found /etc/zshenv. This file also calls path_helper.

After commenting the path_helper section in /etc/zshenv my path is finally correct

查看更多
欢心
3楼-- · 2019-01-14 04:12

A small modification to the solution by sanityinc (couldn't find a way to enter it in the comments above -- is that just me?)

  • I use -l option to the shell to force a login shell (which reads .profile or .bash_profile), rather than an interactive shell (which only reads .bashrc).
  • I do some string trimming on the returned path (as inspection shows a newline sneaking in).

Modified code:

(defun set-exec-path-from-shell-PATH ()
  (let ((path-from-shell 
      (replace-regexp-in-string "[[:space:]\n]*$" "" 
        (shell-command-to-string "$SHELL -l -c 'echo $PATH'"))))
    (setenv "PATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))
(when (equal system-type 'darwin) (set-exec-path-from-shell-PATH))
查看更多
Melony?
4楼-- · 2019-01-14 04:12

I don't have a Mac, so I cannot test this directly, but this can all be found in the *info* page Interactive Inferior Shell.

When you start a shell in Emacs, the process that gets spawned is the program in the Emacs variable explicit-shell-file-name (and if that is nil, the environment variables ESHELL and SHELL are used).

It then sends the contents of ~/.emacs_*shellname* (e.g. if your shell is csh, then ~/.emacs_csh would be sent over. Also, the appropriate .rc files for csh program is sourced, so you can update that as well (in my case .cshrc). Additionally, you can wrap customizations in the .rc file with a check for the environment variable INSIDE_EMACS (which which Emacs sets before it runs a shell).

You need to update those files to change the path in the shell, not the Emacs variable exec-path. exec-path - which is just a list of directories Emacs uses to find executable programs. The behavior of the executables are not affected by changes to exec-path.

查看更多
倾城 Initia
5楼-- · 2019-01-14 04:15

I find the environment.plist scheme on Macs pretty ugly, so I use the following snippet, which assumes you want Emacs to use the same PATH that you see in your Terminal.app:

(defun set-exec-path-from-shell-PATH ()
  (let ((path-from-shell (shell-command-to-string "$SHELL -i -c 'echo $PATH'")))
    (setenv "PATH" path-from-shell)
    (setq exec-path (split-string path-from-shell path-separator))))

(This works for me in Emacs 23; haven't tried it in other versions, but I'd expect it to work.)

查看更多
孤傲高冷的网名
6楼-- · 2019-01-14 04:16

try this maybe. replace path string with yours.

(add-to-list 'load-path "~/opt/swank-clojure/src/emacs")

查看更多
迷人小祖宗
7楼-- · 2019-01-14 04:16

As far as I observed, Emacs takes the path variable from the shell it is launched from, so one solution is to change $PATH in the shell before you launch Emacs.

One other approach I used, which is more flexible, is to use a Makefile and append a "source ~/script_that_set_path" in front of each make commands you have.

查看更多
登录 后发表回答