Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: whence not behaving as expected with noaliases set



On Sun, Oct 23, 2022 at 08:31:58PM +0200, Stefan Schmidt wrote:
> Hello!
> 
> The documentation on whence states:
> 
> > For each name, indicate how it would be interpreted if used as a command name.
> https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Commands.html
> 
> This works as expected if an alias is defined but if the `noaliases` option is set `whence` still returns the alias (same for `type`, `where` and `which`).

> Am I misinterpreting or misunderstanding something here or is there in fact a contradiction between the documentation and the implementation?

I think your interpretation is correct. This seems straight forward
enough to fix. Diff is mostly indentation changes.

diff --git a/Src/builtin.c b/Src/builtin.c
index a7b7755a7..2298b1559 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3902,9 +3902,11 @@ bin_whence(char *nam, char **argv, Options ops, int func)
 		 * We're not using it, so search for ... */
 
 		/* aliases ... */
-		informed +=
-		scanmatchtable(aliastab, pprog, 1, 0, DISABLED,
-			       aliastab->printnode, printflags);
+		if (isset(ALIASESOPT)) {
+		    informed +=
+		    scanmatchtable(aliastab, pprog, 1, 0, DISABLED,
+		                   aliastab->printnode, printflags);
+		}
 
 		/* and reserved words ... */
 		informed +=
@@ -3944,21 +3946,23 @@ bin_whence(char *nam, char **argv, Options ops, int func)
 	if (!OPT_ISSET(ops,'p') && !allmatched) {
 	    char *suf;
 
-	    /* Look for alias */
-	    if ((hn = aliastab->getnode(aliastab, *argv))) {
-		aliastab->printnode(hn, aliasflags);
-		informed = 1;
-		if (!all)
-		    continue;
-	    }
-	    /* Look for suffix alias */
-	    if ((suf = strrchr(*argv, '.')) && suf[1] &&
-		suf > *argv && suf[-1] != Meta &&
-		(hn = sufaliastab->getnode(sufaliastab, suf+1))) {
-		sufaliastab->printnode(hn, printflags);
-		informed = 1;
-		if (!all)
-		    continue;
+	    if (isset(ALIASESOPT)) {
+		/* Look for alias */
+		if ((hn = aliastab->getnode(aliastab, *argv))) {
+		    aliastab->printnode(hn, aliasflags);
+		    informed = 1;
+		    if (!all)
+			continue;
+		}
+		/* Look for suffix alias */
+		if ((suf = strrchr(*argv, '.')) && suf[1] &&
+		    suf > *argv && suf[-1] != Meta &&
+		    (hn = sufaliastab->getnode(sufaliastab, suf+1))) {
+		    sufaliastab->printnode(hn, printflags);
+		    informed = 1;
+		    if (!all)
+			continue;
+		}
 	    }
 	    /* Look for reserved word */
 	    if ((hn = reswdtab->getnode(reswdtab, *argv))) {
diff --git a/Test/B13whence.ztst b/Test/B13whence.ztst
index 3b35835fe..658a9bcab 100644
--- a/Test/B13whence.ztst
+++ b/Test/B13whence.ztst
@@ -32,3 +32,13 @@
     whence -S flip || whence -S loop || whence -s flip || whence -s loop
   )
 1:whence deals with symlink loops gracefully
+
+  (
+    alias echo=print
+    whence echo
+    setopt noaliases
+    whence echo
+  )
+0:whence ignore aliases with NO_ALIASES set
+>print
+>echo




Messages sorted by: Reverse Date, Date, Thread, Author