Skip to content

Commit

Permalink
Merge pull request #386 from zong-zhe/fix-run-bug
Browse files Browse the repository at this point in the history
fix: fix wrong root path with 'kcl run'
  • Loading branch information
Peefy committed Jul 17, 2024
2 parents 5e556c8 + c3ca1fd commit b65d5ed
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions pkg/client/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/client/test_data/test_run_options/no_args/run_8/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "run_7"
edition = "v0.9.0"
version = "0.0.1"

Empty file.
1 change: 1 addition & 0 deletions pkg/client/test_data/test_run_options/no_args/run_8/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello = 'Hello World!'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sub = 'Hello Sub !'
4 changes: 2 additions & 2 deletions pkg/client/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/downloader/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b65d5ed

Please sign in to comment.