From c3ca1fd6a326af670c7d7b86674be677d23529b0 Mon Sep 17 00:00:00 2001 From: zongz Date: Wed, 17 Jul 2024 15:43:18 +0800 Subject: [PATCH] fix: fix wrong root path with 'kcl run' Signed-off-by: zongz --- pkg/client/client_test.go | 1 + pkg/client/run.go | 12 ++++++++++++ .../test_data/test_run_options/no_args/run_8/kcl.mod | 5 +++++ .../test_run_options/no_args/run_8/kcl.mod.lock | 0 .../test_data/test_run_options/no_args/run_8/main.k | 1 + .../test_run_options/no_args/run_8/sub/main.k | 1 + pkg/client/visitor.go | 4 ++-- pkg/downloader/source.go | 3 +++ 8 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod create mode 100644 pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod.lock create mode 100644 pkg/client/test_data/test_run_options/no_args/run_8/main.k create mode 100644 pkg/client/test_data/test_run_options/no_args/run_8/sub/main.k diff --git a/pkg/client/client_test.go b/pkg/client/client_test.go index 4658d1a0..bfb18eb6 100644 --- a/pkg/client/client_test.go +++ b/pkg/client/client_test.go @@ -1760,6 +1760,7 @@ func TestRunLocalWithoutArgs(t *testing.T) { {"run_5", true, "", "kcl_6: KCL 6\na: sub6\nkcl_7: KCL 7\nb: sub7"}, {filepath.Join("run_6", "main"), true, "", "The_sub_kcl_program: Hello Sub World!\nThe_first_kcl_program: Hello World!"}, {"run_7", true, "", "hello: Hello World!\nThe_first_kcl_program: Hello World!"}, + {filepath.Join("run_8", "sub"), true, "", "sub: Hello Sub !"}, } for _, test := range tests { diff --git a/pkg/client/run.go b/pkg/client/run.go index 31250c82..a8380b3c 100644 --- a/pkg/client/run.go +++ b/pkg/client/run.go @@ -461,6 +461,18 @@ func (o *RunOptions) getRootPkgSource() (*downloader.Source, error) { } } } + + if rootPkgSource.IsLocalKPath() || rootPkgSource.IsDir() { + rootPath, err = rootPkgSource.FindRootPath() + if err != nil { + return nil, err + } + + rootPkgSource, err = downloader.NewSourceFromStr(rootPath) + if err != nil { + return nil, err + } + } } if rootPkgSource == nil { diff --git a/pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod b/pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod new file mode 100644 index 00000000..f74a16fd --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "run_7" +edition = "v0.9.0" +version = "0.0.1" + diff --git a/pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod.lock b/pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/pkg/client/test_data/test_run_options/no_args/run_8/main.k b/pkg/client/test_data/test_run_options/no_args/run_8/main.k new file mode 100644 index 00000000..9d8d829a --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_8/main.k @@ -0,0 +1 @@ +hello = 'Hello World!' \ No newline at end of file diff --git a/pkg/client/test_data/test_run_options/no_args/run_8/sub/main.k b/pkg/client/test_data/test_run_options/no_args/run_8/sub/main.k new file mode 100644 index 00000000..ea25265c --- /dev/null +++ b/pkg/client/test_data/test_run_options/no_args/run_8/sub/main.k @@ -0,0 +1 @@ +sub = 'Hello Sub !' \ No newline at end of file diff --git a/pkg/client/visitor.go b/pkg/client/visitor.go index edc10c55..7f32972d 100644 --- a/pkg/client/visitor.go +++ b/pkg/client/visitor.go @@ -72,7 +72,7 @@ func (vpv *VirtualPkgVisitor) Visit(s *downloader.Source, v visitFunc) error { return fmt.Errorf("source is not local") } - sourcePath, err := s.FindRootPath() + sourcePath, err := s.ToFilePath() if err != nil { return err } @@ -244,7 +244,7 @@ func NewVisitor(source downloader.Source, kpmcli *KpmClient) Visitor { } else if source.IsLocalTarPath() || source.IsLocalTgzPath() { return NewArchiveVisitor(NewPkgVisitor(kpmcli)) } else if source.IsLocalPath() { - rootPath, err := source.FindRootPath() + rootPath, err := source.ToFilePath() if err != nil { return nil } diff --git a/pkg/downloader/source.go b/pkg/downloader/source.go index c40aad8a..5cec444e 100644 --- a/pkg/downloader/source.go +++ b/pkg/downloader/source.go @@ -109,6 +109,9 @@ func (local *Local) IsLocalKPath() bool { } func (local *Local) IsDir() bool { + if local == nil { + return false + } fileInfo, err := os.Stat(local.Path) if err != nil { return false