Description
When a relative path is passed to kratos run, the command fails because the path is effectively resolved twice.
Reproduction
mkdir -p cmd/foo
cat > go.mod <<'EOF'
module repro-test
go 1.21
EOF
cat > cmd/foo/main.go <<'EOF'
package main
import "fmt"
func main() {
fmt.Println("ok")
}
EOF
kratos run cmd/foo
Output:
package cmd/foo is not in std (/usr/lib/go-1.26/src/cmd/foo)
Using an absolute path works as expected:
kratos run "$(pwd)/cmd/foo"
Cause
In cmd/kratos/internal/run/run.go (Run() around lines 71–74), the user-provided dir is used both as:
fd := exec.Command("go", append([]string{"run", dir}, programArgs...)...)
fd.Dir = dir
When dir is relative (e.g. cmd/foo):
fd.Dir changes the working directory to <project>/cmd/foo.
go run cmd/foo is then evaluated relative to that directory.
- Go looks for
<project>/cmd/foo/cmd/foo, which does not exist, resulting in the package ... is not in std error.
This does not occur when findCMD() is used, because it stores absolute paths.
Suggested fix
Convert dir to an absolute path before it is used:
if !filepath.IsAbs(dir) {
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
dir = abs
}
This preserves the current behavior for absolute paths while making relative paths work correctly.
Description
When a relative path is passed to
kratos run, the command fails because the path is effectively resolved twice.Reproduction
Output:
Using an absolute path works as expected:
kratos run "$(pwd)/cmd/foo"Cause
In
cmd/kratos/internal/run/run.go(Run()around lines 71–74), the user-provideddiris used both as:When
diris relative (e.g.cmd/foo):fd.Dirchanges the working directory to<project>/cmd/foo.go run cmd/foois then evaluated relative to that directory.<project>/cmd/foo/cmd/foo, which does not exist, resulting in thepackage ... is not in stderror.This does not occur when
findCMD()is used, because it stores absolute paths.Suggested fix
Convert
dirto an absolute path before it is used:This preserves the current behavior for absolute paths while making relative paths work correctly.