necaris / conda.el

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

feat: try `default-directory` to infer env when `filename` is nil #140

Closed martibosch closed 1 year ago

martibosch commented 1 year ago

In a few cases, e.g., when opening a ipynb notebook via code-cells, buffer-file-name will return nil but the working directory can still obtained via the default-directory variable.

necaris commented 1 year ago

Thank you @martibosch !

martibosch commented 1 year ago

Hello again!

I tried using conda.el after the merging of this PR and autoactivation for buffer is now giving a "No Conda environment found for nil".

I am sorry because I think I did not review it properly and the function became over-complicated. I think it can be refactored to a cleaner version that ensures that buffer-file-name is used if present but uses default-directory otherwise:

(defun conda--infer-env-from-buffer ()
  "Search up the project tree for an `environment.yml` defining a conda env."
  (let* ((filename (buffer-file-name))
         (working-dir (if filename
                          (f-dirname filename)
                        default-directory)))
    (when working-dir    
      (or
       (conda--get-name-from-env-yml (conda--find-env-yml working-dir))
       (if (or
            conda-activate-base-by-default
            (alist-get 'auto_activate_base (conda--get-config)))
           "base"
     nil)))))

Maybe even the (when working-dir ...) can be omitted.

I have tried it (with the function as defined in the snippet) and it seems to work properly. Should I submit a PR with a fix?

necaris commented 1 year ago

@martibosch please do!

Renkai commented 1 year ago

Hi! I'm a new user of Emacs(the doomemacs flavor) and I want to use this package. After set (conda-env-autoactivate-mode t) in config, I got Error: (void-function working-dir). Because the working-dir is first appeared in this patch, maybe you guys know how to solve it. Maybe working-dir is a function too new or too old for Emacs 28.2 or something require another package installed? Any help is welcomed!

In case you may interested in discord, there are some detailed information, I and another poor guy encountered the same thing. https://discord.com/channels/406534637242810369/1079338201157746738/1079338201157746738