Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 204e5d0f20aa4b3e6e38b5160e12ef5be2ccccc702d6bb56ab29b87d3a470d03
Ticket: 596936dd2d55ba70dcdc980b9f58b3dccf248605
auto_execok uses cmd in preference to cmd.exe despite path order (windows)
User & Date: juliannoble2 on 2023-11-06 07:00:40
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-plain"
  4. comment changed 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]]
                }
            }
            }
        }
    
  5. foundin changed to: "8.6+"
  6. is_private changed to: "0"
  7. login: "juliannoble2"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "957fdfcd8744611e3fe1f4896c7538b1d9bf47b2"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "juliannoble2"
  14. subsystem changed to: "38. Init - Library - Autoload"
  15. title changed to:
    auto_execok uses cmd in preference to cmd.exe despite path order (windows)
    
  16. type changed to: "Bug"