From 4f049310f9e0b921c81b22140353899901fc2c60 Mon Sep 17 00:00:00 2001 From: zongz Date: Tue, 5 Sep 2023 20:33:46 +0800 Subject: [PATCH] fix: set pwd as workdir --- pkg/api/kpm_run.go | 2 -- pkg/api/kpm_run_test.go | 1 + pkg/cmd/cmd_run.go | 8 ++++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 7f607ab2..28aa30d8 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -143,8 +143,6 @@ func RunPkgInPath(opts *opt.CompileOptions) (string, error) { // no entry opts.Merge(kcl.WithKFilenames(opts.PkgPath())) } - // set the 'kcl.mod' path as the work dir. - opts.Merge(kcl.WithWorkDir(opts.PkgPath())) // Calculate the absolute path of entry file described by '--input'. compiler := runner.NewCompilerWithOpts(opts) diff --git a/pkg/api/kpm_run_test.go b/pkg/api/kpm_run_test.go index 76568ae4..678280a8 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -118,6 +118,7 @@ func TestRunWithWorkdir(t *testing.T) { pkgPath := getTestDir(filepath.Join("test_work_dir", "dev")) opts := opt.DefaultCompileOptions() opts.SetPkgPath(pkgPath) + opts.Merge(kcl.WithWorkDir(pkgPath)) result, err := RunPkgInPath(opts) assert.Equal(t, err, nil) assert.Equal(t, result, "base: base\nmain: main") diff --git a/pkg/cmd/cmd_run.go b/pkg/cmd/cmd_run.go index 7985b72d..29feeb3b 100644 --- a/pkg/cmd/cmd_run.go +++ b/pkg/cmd/cmd_run.go @@ -4,6 +4,7 @@ package cmd import ( "fmt" + "os" "github.com/urfave/cli/v2" "kcl-lang.io/kcl-go/pkg/kcl" @@ -79,6 +80,13 @@ func NewRunCmd() *cli.Command { func KpmRun(c *cli.Context) error { kclOpts := CompileOptionFromCli(c) + // set the pwd as the work dir. + pwd, err := os.Getwd() + if err != nil { + return err + } + + kclOpts.Merge(kcl.WithWorkDir(pwd)) pkgWillBeCompiled := c.Args().First() // 'kpm run' compile the current package undor '$pwd'. if len(pkgWillBeCompiled) == 0 {