Skip to content

kratos run fails with relative paths (package ... is not in std) #3850

Description

@MoyashiWithDevice

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):

  1. fd.Dir changes the working directory to <project>/cmd/foo.
  2. go run cmd/foo is then evaluated relative to that directory.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions