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

[WIP] kpm fixes the order of dependencies and format in kcl.mod and kcl.mod.lock #336

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions pkg/package/modfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strings"

"github.com/BurntSushi/toml"
Expand Down Expand Up @@ -146,6 +147,21 @@ type Dependencies struct {
Deps map[string]Dependency `json:"packages" toml:"dependencies,omitempty"`
}

// SortMapByKey will sort the dependencies by key.
func (deps *Dependencies) SortMapByKey() {
Copy link
Contributor

@Peefy Peefy May 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use an ordered dictionary for serialization/deserialization instead of simply sorting, as KCL users often do not want unexpected changes to occur.

Besides, [dependencies] section and [profile] section lost the expect newline.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Peefy , i've done some changes at #334, please have a look over them if they are in the right direction.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Peefy , i've done some changes at #334, please have a look over them if they are in the right direction.

@d4v1d03 OK. But we should avoid commenting on irrelevant PRs.

keys := make([]string, 0, len(deps.Deps))
for k := range deps.Deps {
keys = append(keys, k)
}
sort.Strings(keys)

sortedDeps := make(map[string]Dependency)
for _, k := range keys {
sortedDeps[k] = deps.Deps[k]
}
deps.Deps = sortedDeps
}

// ToDepMetadata will transform the dependencies into metadata.
// And check whether the dependency name conflicts.
func (deps *Dependencies) ToDepMetadata() (*Dependencies, error) {
Expand Down
1 change: 1 addition & 0 deletions pkg/package/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (kclPkg *KclPkg) LocalVendorPath() string {
// updateModAndLockFile will update kcl.mod and kcl.mod.lock
func (kclPkg *KclPkg) UpdateModAndLockFile() error {
// Generate file kcl.mod.
kclPkg.ModFile.Dependencies.SortMapByKey()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @officialasishkumar 😃

Good job ! But I have a few different ideas about this feature.

The go community maybe have libraries to help us solve this problem. Try to research some of the libraries and see the possible problems with using them. You can update the results of the research in the issue.

I found a third-party library, https://github.com/elliotchance/orderedmap, but when the library is serialized into a kcl.mod file, there may be some problems, this need you to continue to research.

It's not that your method is wrong, it's just that we need some evidence to show that this method is more appropriate than other methods.

err := kclPkg.ModFile.StoreModFile()
if err != nil {
return err
Expand Down
Loading