Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix 'edge already exists' when kpm run #549

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ func (c *KpmClient) AcquireDepSum(dep pkg.Dependency) (string, error) {

// ResolveDepsIntoMap will calculate the map of kcl package name and local storage path of the external packages.
func (c *KpmClient) ResolveDepsIntoMap(kclPkg *pkg.KclPkg) (map[string]string, error) {
err := c.ResolvePkgDepsMetadata(kclPkg, true)
var err error
if kclPkg.IsVendorMode() {
err = c.VendorDeps(kclPkg)
} else {
kclPkg, err = c.Update(
WithUpdatedKclPkg(kclPkg),
)
}
if err != nil {
return nil, err
}
Expand Down
53 changes: 47 additions & 6 deletions pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func TestWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestDownloadGitWithPackage", testDownloadGitWithPackage)
test.RunTestWithGlobalLock(t, "TestModandLockFilesWithGitPackageDownload", testModandLockFilesWithGitPackageDownload)
test.RunTestWithGlobalLock(t, "TestDependencyGraph", testDependencyGraph)
test.RunTestWithGlobalLock(t, "TestVendorWithGlobalLock", testVendorWithGlobalLock)
test.RunTestWithGlobalLock(t, "TestPull", testPull)
test.RunTestWithGlobalLock(t, "TestPullWithInsecureSkipTLSverify", testPullWithInsecureSkipTLSverify)
test.RunTestWithGlobalLock(t, "TestPullWithModSpec", testPullWithModSpec)
Expand Down Expand Up @@ -561,8 +560,8 @@ func testResolveDepsVendorMode(t *testing.T) {
kpm_home := filepath.Join(testDir, "kpm_home")
home_path := filepath.Join(testDir, "my_kcl_resolve_deps_vendor_mode")
os.RemoveAll(home_path)
kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1"))
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))
kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1_0.0.1"))
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2_0.0.1"))

depKcl1 := pkg.Dependency{
Name: "kcl1",
Expand Down Expand Up @@ -642,7 +641,7 @@ func testCompileWithEntryFile(t *testing.T) {
entry_file := filepath.Join(home_path, "main.k")
os.RemoveAll(vendor_path)

kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1"))
kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1_0.0.1"))
depKcl1 := pkg.Dependency{
Name: "kcl1",
FullName: "kcl1_0.0.1",
Expand All @@ -651,12 +650,12 @@ func testCompileWithEntryFile(t *testing.T) {
Source: downloader.Source{
Oci: &downloader.Oci{
Reg: "ghcr.io",
Repo: "kcl-lang/kcl2",
Repo: "kcl-lang/kcl1",
Tag: "0.0.1",
},
},
}
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2_0.0.1"))
depKcl2 := pkg.Dependency{
Name: "kcl2",
FullName: "kcl2_0.0.1",
Expand Down Expand Up @@ -2254,3 +2253,45 @@ func testPushWithInsecureSkipTLSverify(t *testing.T) {

assert.Equal(t, buf.String(), "Called Success\n")
}

func TestIssues(t *testing.T) {
// "kcl-lang/kcl/issue/1760" is the repo where the issue was actually raised and the issue id.
// "testIssue1760" is the test case cover the issue.
RunTestWithGlobalLockAndKpmCli(t, "kcl-lang/kcl/issue/1760", testIssue1760)
}

func testIssue1760(t *testing.T, kpmcli *KpmClient) {
rootPath := getTestDir("issues")
mainKFilePath := filepath.Join(rootPath, "kcl-lang/kcl/issue/1760", "a", "main.k")
var buf bytes.Buffer
kpmcli.SetLogWriter(&buf)

res, err := kpmcli.Run(
WithRunSource(
&downloader.Source{
Local: &downloader.Local{
Path: mainKFilePath,
},
},
),
)

if err != nil {
t.Fatal(err)
}

assert.Contains(t,
utils.RmNewline(buf.String()),
"downloading 'kcl-lang/fluxcd-source-controller:v1.3.2' from 'ghcr.io/kcl-lang/fluxcd-source-controller:v1.3.2'",
)
assert.Contains(t,
utils.RmNewline(buf.String()),
"downloading 'kcl-lang/k8s:1.31.2' from 'ghcr.io/kcl-lang/k8s:1.31.2'",
)

assert.Contains(t,
utils.RmNewline(buf.String()),
"downloading 'kcl-lang/fluxcd-helm-controller:v1.0.3' from 'ghcr.io/kcl-lang/fluxcd-helm-controller:v1.0.3'",
)
assert.Equal(t, res.GetRawYamlResult(), "The_first_kcl_program: Hello World!")
}
68 changes: 68 additions & 0 deletions pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
## Bug Report

https://github.com/kcl-lang/kcl/issues/1760

### 1. Minimal reproduce step (Required)

1. Two empty modules within the same directory:

|- modules
| |- a
| | |- kcl.mod
| | |- main.k
| |- b
| | |- kcl.mod
| | |- main.k

2. Content `/modules/a/kcl.mod` :

```
[package]
name = "a"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
b = { path = "../b" }
fluxcd-source-controller = "v1.3.2"
fluxcd-helm-controller = "v1.0.3"
```

3. Content `/modules/b/kcl.mod` :

```
[package]
name = "b"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
fluxcd-source-controller = "v1.3.2"
```

4. Ensure your `$USER/.kcl/kpm/` directory is empty.


### 2. What did you expect to see? (Required)
```
$ kcl run /modules/a/main.k
downloading 'kcl-lang/fluxcd-helm-controller:v1.0.3' from 'ghcr.io/kcl-lang/fluxcd-helm-controller:v1.0.3'
downloading 'kcl-lang/fluxcd-source-controller:v1.3.2' from 'ghcr.io/kcl-lang/fluxcd-source-controller:v1.3.2'
downloading 'kcl-lang/k8s:1.31.2' from 'ghcr.io/kcl-lang/k8s:1.31.2'
The_first_kcl_program: Hello World!
```

### 3. What did you see instead (Required)
```
$ kcl run /modules/a/main.k
downloading 'kcl-lang/fluxcd-helm-controller:v1.0.3' from 'ghcr.io/kcl-lang/fluxcd-helm-controller:v1.0.3'
downloading 'kcl-lang/fluxcd-source-controller:v1.3.2' from 'ghcr.io/kcl-lang/fluxcd-source-controller:v1.3.2'
downloading 'kcl-lang/k8s:1.31.2' from 'ghcr.io/kcl-lang/k8s:1.31.2'
edge already exists
```

--> **kcl exited with error code 1**

### 4. What is your KCL components version? (Required)

0.10.8-linux-amd64
9 changes: 9 additions & 0 deletions pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "a"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
b = { path = "../b", version = "0.0.1" }
fluxcd-helm-controller = "v1.0.3"
fluxcd-source-controller = "v1.3.2"
29 changes: 29 additions & 0 deletions pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/a/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[dependencies]
[dependencies.b]
name = "b"
full_name = "b_0.0.1"
version = "0.0.1"
[dependencies.fluxcd-helm-controller]
name = "fluxcd-helm-controller"
full_name = "fluxcd-helm-controller_v1.0.3"
version = "v1.0.3"
sum = "IfDYAw0NW8XsbUSygAfqkgrJ8LdI7x4NIq7e9TuTbKY="
reg = "ghcr.io"
repo = "kcl-lang/fluxcd-helm-controller"
oci_tag = "v1.0.3"
[dependencies.fluxcd-source-controller]
name = "fluxcd-source-controller"
full_name = "fluxcd-source-controller_v1.3.2"
version = "v1.3.2"
sum = "iZlc0kXOSajJNUmzvOlcIZgk60WGJbl4xKy4AVWYfkM="
reg = "ghcr.io"
repo = "kcl-lang/fluxcd-source-controller"
oci_tag = "v1.3.2"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.31.2"
version = "1.31.2"
sum = "xBZgPsnpVVyWBpahuPQHReeRx28eUHGFoaPeqbct+vs="
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.31.2"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
7 changes: 7 additions & 0 deletions pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "b"
edition = "v0.10.0"
version = "0.0.1"

[dependencies]
fluxcd-source-controller = "v1.3.2"
15 changes: 15 additions & 0 deletions pkg/client/test_data/issues/kcl-lang/kcl/issue/1760/b/kcl.mod.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[dependencies]
[dependencies.fluxcd-source-controller]
name = "fluxcd-source-controller"
full_name = "fluxcd-source-controller_v1.3.2"
version = "v1.3.2"
reg = "ghcr.io"
repo = "kcl-lang/fluxcd-source-controller"
oci_tag = "v1.3.2"
[dependencies.k8s]
name = "k8s"
full_name = "k8s_1.31.2"
version = "1.31.2"
reg = "ghcr.io"
repo = "kcl-lang/k8s"
oci_tag = "1.31.2"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The_first_kcl_program = 'Hello World!'
8 changes: 8 additions & 0 deletions pkg/client/test_data/resolve_deps/my_kcl_compile/kcl.mod.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
name = "kcl1"
full_name = "kcl1_0.0.1"
version = "0.0.1"
sum = "c5bjxiHbwJqWCdBwXLOr9MydCTis3nJotrOzozkPsKo="
reg = "ghcr.io"
repo = "kcl-lang/kcl1"
oci_tag = "0.0.1"
[dependencies.kcl2]
name = "kcl2"
full_name = "kcl2_0.0.1"
version = "0.0.1"
sum = "OiA7IJfhi9bLp3d+Phc6ncgWE8XwpXqkGvhF5BOpf34="
reg = "ghcr.io"
repo = "kcl-lang/kcl2"
oci_tag = "0.0.1"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ="
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.2"
2 changes: 1 addition & 1 deletion pkg/client/test_data/test_run_git_package/kcl.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "v0.8.0"
version = "0.0.1"

[dependencies]
k8s = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "k8s" }
k8s = { git = "https://github.com/kcl-lang/modules.git", commit = "ee03122b5f45b09eb48694422fc99a0772f6bba8", package = "k8s", version = "1.31" }
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ edition = "v0.10.0"
version = "0.0.1"

[dependencies]
helloworld = { version = "0.1.2" }
helloworld = "0.1.2"
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name = "helloworld"
full_name = "helloworld_0.1.2"
version = "0.1.2"
sum = "PN0OMEV9M8VGFn1CtA/T3bcgZmMJmOo+RkBrLKIWYeQ="
reg = "ghcr.io"
repo = "kcl-lang/helloworld"
oci_tag = "0.1.2"
22 changes: 8 additions & 14 deletions pkg/client/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,15 @@ import (
"kcl-lang.io/kpm/pkg/features"
pkg "kcl-lang.io/kpm/pkg/package"
"kcl-lang.io/kpm/pkg/settings"
"kcl-lang.io/kpm/pkg/test"
"kcl-lang.io/kpm/pkg/utils"
)

func testVendorDeps(t *testing.T) {
func testVendorDeps(t *testing.T, kpmcli *KpmClient) {
testDir := getTestDir("resolve_deps")
kpm_home := filepath.Join(testDir, "kpm_home")
os.RemoveAll(filepath.Join(testDir, "my_kcl"))
kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1"))
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2"))
kcl1Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl1_0.0.1"))
kcl2Sum, _ := utils.HashDir(filepath.Join(kpm_home, "kcl2_0.0.1"))

depKcl1 := pkg.Dependency{
Name: "kcl1",
Expand Down Expand Up @@ -75,10 +74,8 @@ func testVendorDeps(t *testing.T) {

mykclVendorPath := filepath.Join(filepath.Join(testDir, "my_kcl"), "vendor")
assert.Equal(t, utils.DirExists(mykclVendorPath), false)
kpmcli, err := NewKpmClient()
kpmcli.homePath = kpm_home
assert.Equal(t, err, nil)
err = kpmcli.VendorDeps(&kclPkg)
err := kpmcli.VendorDeps(&kclPkg)
assert.Equal(t, err, nil)
assert.Equal(t, utils.DirExists(mykclVendorPath), true)
assert.Equal(t, utils.DirExists(filepath.Join(mykclVendorPath, "kcl1_0.0.1")), true)
Expand All @@ -91,7 +88,7 @@ func testVendorDeps(t *testing.T) {
os.RemoveAll(filepath.Join(testDir, "my_kcl"))
}

func testVendorWithMVS(t *testing.T) {
func testVendorWithMVS(t *testing.T, kpmcli *KpmClient) {
features.Enable(features.SupportMVS)
defer features.Disable(features.SupportMVS)
testDir := getTestDir("test_vendor_mvs")
Expand All @@ -101,9 +98,6 @@ func testVendorWithMVS(t *testing.T) {
pkg.WithSettings(settings.GetSettings()),
)
assert.Equal(t, err, nil)

kpmcli, err := NewKpmClient()
assert.Equal(t, err, nil)
err = kpmcli.VendorDeps(kPkg)
assert.Equal(t, err, nil)

Expand All @@ -112,7 +106,7 @@ func testVendorWithMVS(t *testing.T) {
assert.Equal(t, utils.DirExists(filepath.Join(pkgPath, "vendor", "helloworld_0.1.1")), false)
}

func testVendorWithGlobalLock(t *testing.T) {
test.RunTestWithGlobalLock(t, "TestVendorDeps", testVendorDeps)
test.RunTestWithGlobalLock(t, "TestVendorWithMVS", testVendorWithMVS)
func TestVendorWithGlobalLock(t *testing.T) {
RunTestWithGlobalLockAndKpmCli(t, "TestVendorDeps", testVendorDeps)
RunTestWithGlobalLockAndKpmCli(t, "TestVendorWithMVS", testVendorWithMVS)
}
12 changes: 12 additions & 0 deletions pkg/visitor/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,22 @@ func (rv *RemoteVisitor) Visit(s *downloader.Source, v visitFunc) error {
// update the local module path with the latest version.
if len(rv.VisitedSpace) != 0 {
modFullPath = s.LocalPath(filepath.Join(rv.VisitedSpace, s.Type(), "src"))
if !utils.DirExists(modFullPath) {
err = os.MkdirAll(modFullPath, 0755)
if err != nil {
return err
}
}
}
// update the cache path with the latest version.
if rv.EnableCache {
cacheFullPath = s.CachePath(filepath.Join(rv.CachePath, s.Type(), "cache"))
if !utils.DirExists(cacheFullPath) {
err = os.MkdirAll(cacheFullPath, 0755)
if err != nil {
return err
}
}
}
}

Expand Down