diff --git a/pkg/api/kpm_run.go b/pkg/api/kpm_run.go index 28aa30d8..7f607ab2 100644 --- a/pkg/api/kpm_run.go +++ b/pkg/api/kpm_run.go @@ -143,6 +143,8 @@ 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 0f01e4a6..76568ae4 100644 --- a/pkg/api/kpm_run_test.go +++ b/pkg/api/kpm_run_test.go @@ -113,3 +113,12 @@ func TestRunTar(t *testing.T) { os.RemoveAll(untarPath) } } + +func TestRunWithWorkdir(t *testing.T) { + pkgPath := getTestDir(filepath.Join("test_work_dir", "dev")) + opts := opt.DefaultCompileOptions() + opts.SetPkgPath(pkgPath) + result, err := RunPkgInPath(opts) + assert.Equal(t, err, nil) + assert.Equal(t, result, "base: base\nmain: main") +} diff --git a/pkg/api/test_data/test_work_dir/base/base.k b/pkg/api/test_data/test_work_dir/base/base.k new file mode 100644 index 00000000..36c04d8f --- /dev/null +++ b/pkg/api/test_data/test_work_dir/base/base.k @@ -0,0 +1 @@ +base = "base" \ No newline at end of file diff --git a/pkg/api/test_data/test_work_dir/dev/kcl.mod b/pkg/api/test_data/test_work_dir/dev/kcl.mod new file mode 100644 index 00000000..38c7e776 --- /dev/null +++ b/pkg/api/test_data/test_work_dir/dev/kcl.mod @@ -0,0 +1,8 @@ +[package] +name = "helloworld" +edition = "0.5.0" +version = "0.1.0" + +[profile] +entries = ["../base/base.k", "main.k"] + diff --git a/pkg/api/test_data/test_work_dir/dev/kcl.mod.lock b/pkg/api/test_data/test_work_dir/dev/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/api/test_data/test_work_dir/dev/main.k b/pkg/api/test_data/test_work_dir/dev/main.k new file mode 100644 index 00000000..cc7380ab --- /dev/null +++ b/pkg/api/test_data/test_work_dir/dev/main.k @@ -0,0 +1 @@ +main = "main" \ No newline at end of file