Tcl Source Code

View Ticket
Login
Ticket UUID: 4ab2ccc4a26e39e630ab96f7e9281b60181ea5e9
Title: auto_exec and exec inconsistent in Windows
Type: Bug Created on: 2014-12-02 12:24:11
Submitter: gerhardr Assigned to: dkf
Subsystem: 38. Init - Library - Autoload Severity: Important
Priority: 5 Medium Last modified: 2026-05-18 10:26:54
Status: Closed Closed by: apnadkarni
Resolution: Fixed Closed on: 2026-05-18 10:26:54
Version: 8.6, 8.5
Description:
#
# Inconsistency between execution (exec) and exec_ok result
# Incorrect exec_ok result for CMD files (prob. also other executables)
# Incorrect error message: no such file or directory on exec
# 
catch {console show}

puts "Tcl/Tk [info patchlevel] - TCL bug exec/exec_ok"

foreach ext {BAT CMD TXT} {
  set exepath [file join $env(TEMP) ${ext}_Test]
  puts "\nexepath=$exepath"

  set cmdfile $exepath.$ext
  if {! [file exists $cmdfile]} {
    puts "File doesn't exists, creating $cmdfile ..."
    set fd [open $cmdfile {WRONLY TRUNC CREAT}]
    puts $fd "@echo %~dpn0 started successfully... "
    close $fd
  }

  set execok [auto_execok $exepath]
  set fileex [file exists $execok]
  puts "execok=$execok, file exists=$fileex"

  catch {unset tmp}
  set oserrorcode [catch { set tmp [exec  $execok] } oserrormsg]
  if {[info exists tmp]} {
    puts "cmdfile executed: oserrorcode=$oserrorcode, tmp='$tmp'"
  } else {
    puts "cmdfile executed: oserrorcode=$oserrorcode, tmp not set"
  }

  if {[info exists oserrormsg]} {
    puts "oserrormsg='$oserrormsg'"
  }
}
User Comments:
gerhardr (claiming to be Output of the code) added on 2014-12-02 14:52:29:
Tcl/Tk 8.5.15 - TCL bug exec/exec_ok

exepath=C:/TEMP/BAT_Test
File doesn't exists, creating C:/TEMP/BAT_Test.BAT ...
execok=C:/TEMP/BAT_Test.BAT, file exists=1
cmdfile executed: oserrorcode=0, tmp='C:\Temp\BAT_Test started successfully... '
oserrormsg='C:\Temp\BAT_Test started successfully... '

exepath=C:/TEMP/CMD_Test
File doesn't exists, creating C:/TEMP/CMD_Test.CMD ...
execok=C:/TEMP/CMD_Test.CMD, file exists=1
cmdfile executed: oserrorcode=1, tmp not set
oserrormsg='couldn't execute "C:\TEMP\CMD_Test.CMD": no such file or directory'

exepath=C:/TEMP/TXT_Test
File doesn't exists, creating C:/TEMP/TXT_Test.TXT ...
execok=, file exists=0
cmdfile executed: oserrorcode=1, tmp not set
oserrormsg='couldn't execute "": no such file or directory'

=== Remarks ===
Test 1: extension BAT - auto_execok expanding ok, file exists, cmd executed
Test 2: extension CMD - auto_execok expanding ok, file exists, cmd not executed
Test 2: extension TXT - auto_execok expanding not ok, further tests fail (ok!)

From the exec manpage: Then, in order, .com, .exe, and .bat are appended ...
In this case the CMD file must not become expanded by autoexec_ok.

From the auto_execok: Determines whether there is an executable file or shell builtin by the name cmd. If so, it returns a list of arguments to be passed to exec to execute the executable file or shell builtin named by cmd.
In this case the CMD file must be executable (which is tha case on Windows) and it can also be executed by CMD.exe.

The error message: "... no such file or directory" 
is definitely not correct as the "file exists" stetes before.

Information: Windows treats all extensions which are shown in the evironment variable PATHEXT as executable (created on some registry entry), e. g.:
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

dkf added on 2015-05-17 11:22:58:

I'm guessing that the problem is that it needs the native name. (I wonder what searches were actually done, and what the COMSPEC, PATH, WINDIR and PATHEXT environment variables contain.)


apnadkarni added on 2016-07-09 15:45:59:

One part of this has been fixed with check-in [2b6ee27091] for 8.6.6. exec now understands .cmd extensions. Other inconsistencies between exec and auto_execok remain.


anonymous (claiming to be Tom Turkey) added on 2019-09-08 00:49:58:
The "file" command needs help also.
file executable x.cmd  returns 0
file executable x.bat  returns 1

In each case the file exists.

apnadkarni added on 2026-05-18 10:26:54:

Purportedly fixed for 9.1 in TIP 753 commit de961e8d.

Aside from some underlying bugs, the original code was incorrect.

  • exec $execok is the wrong idiom, should be exec -- {*}$execok,
  • auto_execok will not attach .TXT, has to be explicitly specified.