Skip to content

os/exec: Cmd.Dir is not checked when looking for executable on Windows #21675

@atn832

Description

@atn832

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.9 windows/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

(windows 10)
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\atn\go
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config

What did you do?

I created the following files in the folder of this source code file:
.\subfolder\list.bat: file content is simply "dir"
.\subfolder\list2.bat: file content is simply "dir"
.\list2.bat: file content is empty, or can be anything

package main

import (
	"fmt"
	"os"
	"os/exec"
)

func main() {
	// says it cannot find list.bat in the path.
	runScript("subfolder", "list.bat")
	// correctly runs .\subfolder\list2.bat from .\subfolder
	runScript("subfolder", "list2.bat")
	// runs .\subfolder\list.bat from .\
	runScript("", "subfolder\\list.bat")
	// runs .\subfolder\list2.bat from .\
	runScript("", "subfolder\\list2.bat")
}

func runScript(folder, filename string) bool {
	fmt.Println("Running", folder, filename)
	cmd := exec.Command(filename)
	cmd.Dir = folder
	// Stream to std out
	cmd.Stdout = os.Stdout
	cmd.Stdin = os.Stdin
	cmd.Stderr = os.Stderr

	err := cmd.Run()
	if err != nil && err.Error() != "exit status 1" {
		fmt.Println("Error running script: " + err.Error())
		return false
	}
	return true
}

Recipe:

  • create the .bat files as described above
  • build the executable from the go code above
  • run the executable
  • see that list.bat is not found, but list2.bat is indeed found

What did you expect to see?

I expect list.bat and list2.bat to list the content of subfolder.

What did you see instead?

list.bat is "not found", even though it is indeed in subfolder

EDIT: I've just go-fmt'd the source code as at https://play.golang.org/p/LDKmGQpxRH and also added the markdown tag for Go source code.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions