Artifacts Associated With Ticket 596936dd2d55ba70
Ticket change [204e5d0f20] (rid 219980) by juliannoble2 on 2023-11-06 07:00:40:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-plain"
- comment initialized to:
init.tcl's auto_execok function has for some time since 8.57 looked for executable files with empty extensions first. Some tools such as msys2 provide various shims/scripts named like their MS counterparts either with or without a .exe extension. With c:\windows\system32 earliest in the path - one would expect that typing cmd at a prompt would preferentially find the normal c:\windows\system32\cmd.exe - or use the $env(COMSPEC) value. For shell builtins as defined in the auto_execok function such as 'echo' - the correct cmd.exe is used (from COMPSPEC). However - in the presence of some other file named cmd in the path - just typing cmd in the shell loads that other path into the ::auto_execs array. This can be worked around by typing cmd.exe instead of just cmd, but it is surprising, and I'm wondering if at least for cmd - this could specifically use the COMSPEC value - as this is what is chosen for the builtins, and I can't think of a circumstance where cmd should point to a different location than its builtins. The potential remains for other programs to be selected in an order based on extension that doesn't follow the order listed in PATH - presumably(?) it is done this way for performance so that the entire set of PATH dirs isn't searched for each extension - but I do also wonder at the reason for 'no extension' being ahead of .exe Current search order is {foreach extension {foreach path ...}} Generally on windows one expects the earlier entries in the path to be respected - but Tcl on windows has operated in this way for some time. Might it be worthwhile doing a pre-scan for all extensions in the dirname of $::env(COMPSPEC) (usually something like c:\windows\system32), then, noting the position of that folder in the PATH (or giving lowest pref if not present), only replace it in auto_execs if something was found earlier in the PATH? That proposal gives the folder of COMSPEC a special privilege - which is a bit ugly. Rather than that, my preference would be to reverse the the loop nesting and search once through the folders but check each of the 5 extensions - as this is the least surprising in terms of how users have their PATH configured - but I don't know why the current ordering was chosen. Some very rough testing.. I have a bit over 100 folders in my PATH - which I suspect is somewhat higher than most. Doing a tcl glob for the 5 extensions (with a specific filename) in one call for a folder with say 50 files - takes less than 1ms Doing the same on a large folder such as c:\windows\system32 (contains around 5k files) takes less than 3ms both these rough tests were for non-existent names e.g glob -nocomplain -dir $d -tail flubber flubber.exe flubber.cmd ... A single glob like this is a bit faster than testing file exists for each one. With my changes below - entering a non existent command returns about 4x faster (noticeable in interactive shell) and the selection respects the PATH order. Please review and consider. set lookfor [lmap ext $execExtensions {string cat ${name} ${ext}}] foreach dir [split $path {;}] { # Skip already checked directories if {[info exists checked($dir)] || ($dir eq "")} { continue } set checked($dir) {} foreach match [glob -nocomplain -dir $dir -tail {*}$lookfor] { set file [file join $dir $match] if {[file exists $file] && ![file isdirectory $file]} { return [set auto_execs($name) [list $file]] } } } if 0 { foreach ext $execExtensions { unset -nocomplain checked foreach dir [split $path {;}] { # Skip already checked directories if {[info exists checked($dir)] || ($dir eq "")} { continue } set checked($dir) {} set file [file join $dir ${name}${ext}] if {[file exists $file] && ![file isdirectory $file]} { return [set auto_execs($name) [list $file]] } } } } - foundin initialized to: "8.6+"
- is_private initialized to: "0"
- login: "juliannoble2"
- priority initialized to: "5 Medium"
- private_contact initialized to: "957fdfcd8744611e3fe1f4896c7538b1d9bf47b2"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "juliannoble2"
- subsystem initialized to: "38. Init - Library - Autoload"
- title initialized to:
auto_execok uses cmd in preference to cmd.exe despite path order (windows)
- type initialized to: "Bug"
Ticket change [14974c9cbd] (rid 219981) by juliannoble2 on 2023-11-06 07:16:03:
- icomment:
I did another test by copying whoami.exe to the last folder in my path and renaming it to whoami123.exe I ran unset auto_execs and then time {whoami123} a few times. Time for first run went from around 180ms for the original version to 190ms to the new code version. For executables earlier in the path - the new version is considerably faster than the old version - it only gets slightly worse for executables way down in the path list. - login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [edc6e5c2e4] (rid 219982) by juliannoble2 on 2023-11-06 08:59:30:
- icomment:
I note that it used to be this way around in 2012 and DGP changed it as shown here: https://core.tcl-lang.org/tcl/info/6909864f5655c69 with the comment: Make [auto_execok] search match [exec]. I'd like to understand more about why this was done Can anyone clarify why exec wasn't changed instead and what exactly is going on here?
- login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [32ba13684f] (rid 219984) by sebres on 2023-11-06 11:12:53:
- icomment:
<blockquote>> Can anyone clarify why exec wasn't changed instead and what exactly is going on here?</blockquote> Basically no matter what was primary the goal for the change, it got implemented not quite correct.<br/> Because the behaviour deviates now from standard windows search sequence, see [https://learn.microsoft.com/en-us/previous-versions//cc723564(v=technet.10)#command-search-sequence|Command Search Sequence] <div style="padding-left:10pt"><i>...<br/> Note that the shell tries all possible file extensions in a specific directory before moving on to search the next directory (if there is one).</i></div> Since [6909864f5655c69] Tcl does it vice versa (tries all possible directories for a specific file extension and then start from scratch for next extension).<br/> This contradicts also a common sense a bit: if no extension specified, one expects to find <b>first occurrence in path</b> (no matter which extension).
- login: "sebres"
- mimetype: "text/x-fossil-wiki"
- icomment:
Ticket change [99399b9906] (rid 219986) by juliannoble2 on 2023-11-06 12:04:28:
- icomment:
Yes, I tested in cmd and pwsh shells and they find things in a different order to Tcl - this really needs to be fixed. There are other demons here. The paths retrieved by splitting on the PATH env var sometimes end with a slash. When you do a glob -dir $dir -tail on a dir ending in a slash - it can return /name instead of just name Then the file join fails to work as it takes the name as an absolute path and doesn't add the dir. #with trailing slash in path % set path {C:\Program Files\PowerShell\7} C:\Program Files\PowerShell\7 % tcl::unsupported::representation $path value is a pure string with a refcount of 4, object pointer at 0x1ea036f3270, string representation "C:\Program Fi..." % append path \\ C:\Program Files\PowerShell\7\ % tcl::unsupported::representation $path value is a utf32string with a refcount of 2, object pointer at 0x1ea036ef2e0, internal representation 0x1ea038c70b0:0x0, string representation "C:\Program Fi..." % glob -dir $path -tail pwsh.exe /pwsh.exe #without trailing slash % set path {C:\Program Files\PowerShell\7} C:\Program Files\PowerShell\7 % tcl::unsupported::representation $path value is a pure string with a refcount of 5, object pointer at 0x1ea036f3270, string representation "C:\Program Fi..." % glob -dir $path -tail pwsh.exe pwsh.exe Whether or not a trailing slash should strictly exist at the end of a path - I believe it to be fairly common on windows (some installers do it) and it should work. Tcl8.5 on the other hand does it correctly: % glob -dir "c:\\" -tail windows-version.txt windows-version.txt versus testing in 8.6, 8.7, 9.0 % glob -dir "c:\\" -tail windows-version.txt /windows-version.txt - login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [30ad55d9af] (rid 219987) by juliannoble2 on 2023-11-06 12:19:01:
- icomment:
Though I see I should have just file normalized that path.. so the original code snippet above needs fixing in that regard too.
- login: "juliannoble2"
- mimetype: "text/plain"
- icomment:
Ticket change [9b14c4d34e] (rid 253655) by apnadkarni on 2026-05-18 10:46:35:
- icomment:
Partial fix with TIP 753 in terms of the search path. Complete fix still pending.
- login: "apnadkarni"
- mimetype: "text/plain"
- icomment:
Ticket change [09bcb020ea] (rid 253718) by apnadkarni on 2026-05-21 07:01:28:
- closedate changed to: "2461181.7926895255"
- closer changed to: "apnadkarni"
- icomment:
Closing because - search path fixed and (almost - see TIP 753) matches exec and Windows - "foo" no longer preferred over "foo.exe" - glob -dir issues is unrelated and now tracked in [e860418c2d](https://core.tcl-lang.org/tcl/tktview/e860418c2d)
- login: "apnadkarni"
- mimetype: "text/x-markdown"
- resolution changed to: "Fixed"
- status changed to: "Closed"