necaris / conda.el

Emacs helper library (and minor mode) to work with conda environments
MIT License
153 stars 49 forks source link

buffer local variable `conda-anaconda-home' not working #77

Closed AirManH closed 3 years ago

AirManH commented 3 years ago

If I set conda-anaconda-home as a buffer local variable, conda-env-activate can not activate the correct conda env.

Steps to reproduce the problem

Configuration

  1. In init file:

    (use-package conda)
  2. My python project is organized as:

    .
    ├── .dir-locals.el
    └── src
       └── main.py
  3. In .dir-locals.el:

    ((python-mode . ((conda-anaconda-home . "/home/user/programs/miniconda3/"))))

Operations

  1. open main.py
  2. M-x conda-env-activate, choose environment base, enter
  3. *Message* buffer shows:
    if: Error: executing command ""/home/user/.anaconda3/bin/conda" ..activate "bash" "/home/user/programs/miniconda3/"" produced error code 127

Specifications

AirManH commented 3 years ago

I did some debugging and found out what caused this error.

As the code shown below, in the line 190, the statement (with-current-buffer standard-output in function conda--get-path-prefix will override the buffer local variable conda-anaconda-home.

https://github.com/necaris/conda.el/blob/9f7eea16e9ad3eb34fe3d1cbd9d6162b8046c2f8/conda.el#L185-L197

If you change the code into this:

 (defun conda--get-path-prefix (env-dir) 
   "Get a platform-specific path string to utilize the conda env in ENV-DIR. 
 It's platform specific in that it uses the platform's native path separator." 
   (message "%s" conda-anaconda-home)  ;;; <========= /home/user/programs/miniconda3/
   (s-trim 
    (with-output-to-string 
      (with-current-buffer standard-output 
        (let* ((conda-executable-path 
                (concat (file-name-as-directory conda-anaconda-home) 
                        (file-name-as-directory conda-env-executables-dir) 
                        "conda"))
               (tmp (message "%s" conda-anaconda-home))  ;;; <========= /home/user/.anaconda3/

The first message prints: /home/user/programs/miniconda3/, and the second prints: /home/user/.anaconda3/

necaris commented 3 years ago

@AirManH thanks for the report and the debugging! It looks like capturing the current value of conda-anaconda-home outside the with-current-buffer block will fix this -- any interest in submitting a PR?