Subject: [PATCH] Skip the non-exists path when loading a file on Windows

From: Lin Sun <[email-masked]>
Date: Thu, 8 May 2025 00:19:24 +0000

Emacs will searching file with a suffix even the dir is not exists.
For example, (add-to-list 'load-path "/path-not-exists"), then (require 'debug) will lead searching the debug.dll/debug.elc/debug.el under the "/path-not-exists" on windows.

This patch will distinguish the non-exists-dir to skip the unnecessary file operations.
From bb8b2c6b95eb74d224910fb09b8e682cc006c066 Mon Sep 17 00:00:00 2001
From: Lin Sun <[email-masked]>
Date: Wed, 7 May 2025 06:30:02 -0700
Subject: [PATCH] Skip the non-exists path when loading a file on Windows

* src/lread.c (faccessat): Assign errno=ENOTDIR for dir not exists
* src/w32.c (openp): Skip current path if it does not exists
---
 src/lread.c | 5 ++++-
 src/w32.c   | 6 ++++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/lread.c b/src/lread.c
index 95c9e71..5d044c4 100644
--- a/src/lread.c
+++ b/src/lread.c
@@ -2152,8 +2152,11 @@ openp (Lisp_Object path, Lisp_Object str, Lisp_Object suffixes,
                     it.  Only open the file when we are sure that it
                     exists.  */
 #ifdef WINDOWSNT
-                if (sys_faccessat (AT_FDCWD, pfn, R_OK, AT_EACCESS))
+                if (sys_faccessat (AT_FDCWD, pfn, R_OK, AT_EACCESS)) {
+		  if (errno == ENOTDIR) /* dir not exists, continue on next path */
+		    break;
                   fd = -1;
+		}
                 else
 #endif
 		  {
diff --git a/src/w32.c b/src/w32.c
index d7bf173..0fd4ea4 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -4185,12 +4185,14 @@ faccessat (int dirfd, const char * path, int mode, int flags)
 	    }
 	  /* FALLTHROUGH */
 	  FALLTHROUGH;
-	case ERROR_FILE_NOT_FOUND:
 	case ERROR_PATH_NOT_FOUND:
 	case ERROR_INVALID_DRIVE:
-	case ERROR_NOT_READY:
 	case ERROR_BAD_NETPATH:
+	  errno = ENOTDIR;
+	  break;
 	case ERROR_BAD_NET_NAME:
+	case ERROR_FILE_NOT_FOUND:
+	case ERROR_NOT_READY:
 	  errno = ENOENT;
 	  break;
 	default:
-- 
2.49.0