Skip to content

os/exec: look in current directory for executable on all platforms? #7570

@alexbrainman

Description

@alexbrainman
This test

# cat main_test.go
package main_test

import (
        "io/ioutil"
        "os"
        "os/exec"
        "path/filepath"
        "testing"
)

func testALEX(t *testing.T, exe string) {
        tmp, err := ioutil.TempDir("", "TestALEX")
        if err != nil {
                t.Fatal(err)
        }
        defer os.RemoveAll(tmp)

        newdir := filepath.Join(tmp, "d")
        err = os.Mkdir(newdir, 0777)
        if err != nil {
                t.Fatal(err)
        }
        err = os.Symlink("/bin/echo", filepath.Join(newdir, "c"))
        if err != nil {
                t.Fatal(err)
        }

        cmd := exec.Command(exe, "-n", "foo")
        cmd.Dir = newdir
        out, err := cmd.CombinedOutput()
        if err != nil {
                t.Error(err)
                return
        }
        want := "foo"
        if got := string(out); got != want {
                t.Errorf("exec: want %q, got %q", want, got)
        }
}

func TestALEX(t *testing.T) {
        testALEX(t, "./c")
        testALEX(t, "c")
}
#

should succeed, but fails:

# PATH=.:$PATH go test -v
=== RUN TestALEX
--- FAIL: TestALEX (0.00 seconds)
        main_test.go:32: exec: "c": executable file not found in $PATH
FAIL
exit status 1
FAIL    t       0.003s
# hg -R $GOROOT id
17d15bc23bce+ tip
#

I am not fussed, if we decide either way. But I would like to know how to handle this
scenario, because that is what windows does (windows always look in the . before
searching the PATH). This is just another side of issue #7377.

Alex

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions