airblade / vim-rooter

Changes Vim working directory to project root.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

g:rooter_patterns setting not seen/ignored on initial launch

jwmann opened this issue · comments

This is the workflow:

  1. Open file from Finder or Terminal
  2. vim-rooter sets the directory using the defaults
    let g:rooter_patterns #=> [ '.git', '.git/', '_darcs/', '.hg/', '.bzr/', '.svn/' ]
  3. Reloading vimrc will actually set the variable now
    let g:rooter_patterns #=> [ 'package.json', '.git', '.git/', '_darcs/', '.hg/', '.bzr/', '.svn/' ]
  4. Manually engage :Rooter (nothing happens, don't know why)

This is how I'm setting the patterns, it sets as expected.

  " Add custom roots to the front so they take priority over the defaults
  let g:rooter_patterns = ['package.json'] + g:rooter_patterns

Here's a link to my vim-rooter setup incase you think I'm setting it up wrong.

Expected outcome:
Vim's initial open should read the set variable of g:rooter_patterns and Rooter should find the project root using its priority based list.

It's odd that your rooter patterns aren't set until step 3. As soon as you open vim, with or without a file, vim should read your vimrc and set g:rooter_patterns to ['package.json', ...].

You could verify/debug that by echoing g:rooter_patterns in your vimrc just after you set the variable, and also in vim-roooter just before it looks for the variable.

As for :Rooter doing nothing, the best way to debug that is more echo statements. You could add one at the top of s:ChangeDirectory(directory):

echom a:dir . ' :: ' . getcwd()

While in the middle of setting up the debug echos I noticed this error while opening vim.
screen shot 2017-01-17 at 16 48 26

Maybe rooter isn't even being initialized properly? 😕

After adding in all the echos you mentioned, I got this error when trying to open a file.
screen shot 2017-01-17 at 16 53 47

After pressing Enter, nothing was echo'd.
Removing echom a:dir . ' :: ' . getcwd() for the error also results in no echos.

Something really strange is going on.


This is what I vim-rooter had after changes, just so you can verify it.

echo g:rooter_patterns
if !exists('g:rooter_patterns')
  let g:rooter_patterns = ['.git', '.git/', '_darcs/', '.hg/', '.bzr/', '.svn/']
endif

function! s:ChangeDirectory(directory)
  echom a:dir . ' :: ' . getcwd()
  if a:directory !=# getcwd()
    let cmd = g:rooter_use_lcd == 1 ? 'lcd' : 'cd'
    execute ':'.cmd fnameescape(a:directory)
    if !g:rooter_silent_chdir
      echo 'cwd: '.a:directory
    endif
    execute 'silent doautocmd' s:nomodeline 'User RooterChDir'
  endif
endfunction

And from my vimrc

      " Add custom roots to the front so they take priority over the defaults
      let g:rooter_patterns = ['package.json'] + g:rooter_patterns
      echo g:rooter_patterns

I think the problem is that in your vimrc you define g:rooter_patterns with reference to itself but the vimrc is evaluated before plugins are loaded so g:rooter_patterns doesn't exist yet.

You'll either have to change your let g:rooter_patterns = ... line to explicitly list the patterns you want, rather than by reference to g:rooter_patterns, or you'll have to move that line to a file in your vim config's after/ directory where it'll be evaluated after plugins are loaded.

The problem with echom a:dir ... is that it should have been echom a:directory .... Sorry about that.

Ahhh I understand.

g:rooter_patterns is only defined if doesn't already exist.
I'm trying to concatenate to that variable but because it technically doesn't exist yet, I can't reference it.
That explains why it only works after I've reloaded my vimrc and not initially.

Is the empty check even necessary?
I'm assuming plugins are loaded prior/during the vimrc, whatever people set in their vimrc with overwrite the initial variable. Probably a dangerous assumption to make.

Not sure what the general convention for that is.

Plugins are loaded after the vimrc (see steps 3 and 4 of :help startup).

Your best bet is to make your rooter patterns explicit. For example:

let g:rooter_patterns = ['package.json', '.git', '.git/', '_darcs/', '.hg/', '.bzr/', '.svn/']

Your best bet is to make your rooter patterns explicit. For example:
let g:rooter_patterns = ['package.json', '.git', '.git/', '_darcs/', '.hg/', '.bzr/', '.svn/']

That's what I ended up doing. I guess that's what I get for trying to be fancy haha
Thanks!