Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Correct kernel CLI human-readable path resolution https://github.com/siyuan-note/siyuan/issues/18999

Danielc738f1aaa563
diff --git a/kernel/cli/cmd/display_test.go b/kernel/cli/cmd/display_test.go
index f81f6eb389..5f9dd93667 100644
--- a/kernel/cli/cmd/display_test.go
+++ b/kernel/cli/cmd/display_test.go
@@ -10,6 +10,56 @@ package cmd
import "testing"
+func TestResolvePath(t *testing.T) {
+ paths := map[string]string{
+ "/parent": "/20260827000000-parent1.sy",
+ "/parent/child": "/20260827000000-parent1/20260827000001-child01.sy",
+ }
+ lookup := func(hPath string) (string, bool) {
+ p, found := paths[hPath]
+ return p, found
+ }
+ tests := []struct {
+ name string
+ userPath string
+ hPath string
+ want string
+ wantErr bool
+ }{
+ {name: "explicit internal path", userPath: "/explicit.sy", hPath: "/missing", want: "/explicit.sy"},
+ {name: "default root", want: "/"},
+ {name: "root hpath", hPath: "/", want: "/"},
+ {name: "relative root hpath", hPath: ".", want: "/"},
+ {name: "existing hpath", hPath: "/parent", want: "/20260827000000-parent1.sy"},
+ {name: "relative hpath", hPath: "parent", want: "/20260827000000-parent1.sy"},
+ {name: "hpath with trailing slash", hPath: "/parent/", want: "/20260827000000-parent1.sy"},
+ {
+ name: "nested hpath",
+ hPath: "/parent/child",
+ want: "/20260827000000-parent1/20260827000001-child01.sy",
+ },
+ {name: "missing hpath", hPath: "/missing", wantErr: true},
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ got, err := resolvePathWithLookup(test.userPath, test.hPath, lookup)
+ if test.wantErr {
+ if nil == err {
+ t.Fatalf("expected an error, got path %q", got)
+ }
+ return
+ }
+ if nil != err {
+ t.Fatalf("resolve path failed: %v", err)
+ }
+ if got != test.want {
+ t.Fatalf("unexpected path: got %q, want %q", got, test.want)
+ }
+ })
+ }
+}
+
func TestResolveDocumentMovePath(t *testing.T) {
paths := map[string]string{
"/parent": "/20260726000000-parent1.sy",
diff --git a/kernel/cli/cmd/document.go b/kernel/cli/cmd/document.go
index 42fca286e1..f956abdeb6 100644
--- a/kernel/cli/cmd/document.go
+++ b/kernel/cli/cmd/document.go
@@ -46,7 +46,11 @@ var documentListCmd = &cobra.Command{
if notebook == "" {
return fmt.Errorf("--notebook is required")
}
- files, _, err := model.ListDocTree(notebook, resolvePath(notebook, docPath, hpath), 0, false, false, 128)
+ listPath, err := resolvePath(notebook, docPath, hpath)
+ if err != nil {
+ return err
+ }
+ files, _, err := model.ListDocTree(notebook, listPath, 0, false, false, 128)
if err != nil {
return err
}
@@ -292,16 +296,32 @@ var documentInfoCmd = &cobra.Command{
},
}
-func resolvePath(boxID, userPath, hpath string) string {
- if userPath != "" {
- return userPath
- }
- if hpath != "" {
- if bt := treenode.GetBlockTreeRootByHPath(boxID, hpath); bt != nil {
- return strings.TrimSuffix(bt.Path, ".sy") + "/"
+func resolvePath(boxID, userPath, hpath string) (string, error) {
+ return resolvePathWithLookup(userPath, hpath, func(targetHPath string) (string, bool) {
+ bt := treenode.GetBlockTreeRootByHPath(boxID, targetHPath)
+ if nil == bt {
+ return "", false
}
+ return bt.Path, true
+ })
+}
+
+func resolvePathWithLookup(userPath, hpath string, lookup func(string) (string, bool)) (string, error) {
+ if "" != userPath {
+ return userPath, nil
}
- return "/"
+ if "" == hpath {
+ return "/", nil
+ }
+
+ targetHPath := path.Clean("/" + strings.TrimPrefix(hpath, "/"))
+ if "/" == targetHPath {
+ return "/", nil
+ }
+ if targetPath, found := lookup(targetHPath); found {
+ return targetPath, nil
+ }
+ return "", fmt.Errorf("target human-readable path not found: %s", targetHPath)
}
func resolveDocumentMovePath(boxID, userPath, hpath, sourceHPath string) (string, error) {
diff --git a/kernel/cli/cmd/import.go b/kernel/cli/cmd/import.go
index 87cadb98dd..bd5116b573 100644
--- a/kernel/cli/cmd/import.go
+++ b/kernel/cli/cmd/import.go
@@ -49,13 +49,17 @@ var importMdCmd = &cobra.Command{
if err != nil {
return err
}
+ targetPath, err := resolvePath(notebook, toPath, hpath)
+ if err != nil {
+ return err
+ }
if dryRun {
fmt.Printf("[dry-run] Would import Markdown from \"%s\" to notebook %s\n", filePath, notebook)
return nil
}
- if err := model.ImportFromLocalPath(notebook, absPath, resolvePath(notebook, toPath, hpath)); err != nil {
+ if err := model.ImportFromLocalPath(notebook, absPath, targetPath); err != nil {
return formatNotebookWriteError(notebook, err)
}
model.AppendPushReloadFiletreeEntry()
@@ -83,13 +87,17 @@ var importSYCmd = &cobra.Command{
if err != nil {
return err
}
+ targetPath, err := resolvePath(notebook, toPath, hpath)
+ if err != nil {
+ return err
+ }
if dryRun {
fmt.Printf("[dry-run] Would import .sy.zip from \"%s\" to notebook %s\n", filePath, notebook)
return nil
}
- if err := model.ImportSY(absPath, notebook, resolvePath(notebook, toPath, hpath)); err != nil {
+ if err := model.ImportSY(absPath, notebook, targetPath); err != nil {
return err
}
model.AppendPushReloadFiletreeEntry()