Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bash-completion is very slow on MSYS2

Bash-completion is very slow on MSYS2

Bash-completion is very slow on MSYS2 when the current user is a domain user. This describes the cause and the solutions.

Cause

Expansion of ~* is very slow when you use a domain user. For example:

$ time echo ~*
~*

real    0m23.151s
user    0m0.000s
sys     0m0.000s

When the tab key is pressed, bash-completion tries to evaluate ~*. That's why bash-completion is slow.

Solution 1: Disable ~* in bash_completion

~* is used inside /usr/share/bash-completion/bash_completion. Disabling it can solve the problem.

--- /usr/share/bash-completion/bash_completion.org
+++ /usr/share/bash-completion/bash_completion
@@ -542,9 +542,9 @@
     elif [[ $1 == \'* ]]; then
         # Leave out first character
         printf -v $2 %s "${1:1}"
-    elif [[ $1 == ~* ]]; then
-        # avoid escaping first ~
-        printf -v $2 ~%q "${1:1}"
+    #elif [[ $1 == ~* ]]; then
+    #    # avoid escaping first ~
+    #    printf -v $2 ~%q "${1:1}"
     else
         printf -v $2 %q "$1"
     fi

Solution 2: Disable db in /etc/nsswitch.conf

MSYS2 obtains the user information from the system database (in Windows) by default, but it is very slow when the current user is a domain user. Disabling it and make MSYS2 to obtain the user information from files solves the problem.

First you need to create /etc/passwd and /etc/group with the information of the local users (-l) and the current (domain) user (-c).

$ mkpasswd -l -c > /etc/passwd
$ mkgroup -l -c > /etc/group

Then you need to modify /etc/nsswitch.conf to disable db from group and passwd.

--- /etc/nsswitch.conf.org
+++ /etc/nsswitch.conf
@@ -1,7 +1,7 @@
 # Begin /etc/nsswitch.conf
 
-passwd: files db
-group: files db
+passwd: files #db
+group: files #db
 
 db_enum: cache builtin

If you don't update /etc/passwd and /etc/group properly, bash prompt might show your account as Unknown+User.

See also:

@waqasilyas

This comment has been minimized.

Copy link

waqasilyas commented Jul 14, 2017

Thanks a lot! I had been frustrated with the lag in each command while running scripts. I tried several times in vain to solve the issue until i stumbled upon this solution. Now i can finally enjoy using msys2! Thanks again!

@calexandrepcjr

This comment has been minimized.

Copy link

calexandrepcjr commented Feb 5, 2018

Thank you very much!!! Amazing solution!

@kelvin-mkc

This comment has been minimized.

Copy link

kelvin-mkc commented May 25, 2018

Thank you for this, it makes it possible to get things done with git when stuck
in a domain.

Note: This will only work for the user that executes mkpasswd/mkgroup,
since it's the current user that appears in the output. I ran into trouble
recently because of this: git-for-windows/git#1684

Note 2: Unfortunately, /etc/nsswitch.conf gets replaced on every
upgrade in Git for Windows, so this has to be done after each one.

I put that part in a sed script and added to the mk commands. This way, it can
just be executed after every GFW upgrade, to prevent the db requests.

#!/bin/sh
mkpasswd -l -c > /etc/passwd
mkgroup -l -c > /etc/group

sed -E \
  -e 's/^(passwd:).*/\1 files # db/' \
  -e 's/^(group:).*/\1 files # db/' \
  -i'' \
  '/etc/nsswitch.conf'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.