Skip to content

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

:bug: Report failed CLI exports and repository operations https://github.com/siyuan-note/siyuan/issues/19459

Danielacc5751cef66
diff --git a/kernel/cli/cmd/block.go b/kernel/cli/cmd/block.go
index 78dab0893d..3f70b867db 100644
--- a/kernel/cli/cmd/block.go
+++ b/kernel/cli/cmd/block.go
@@ -379,13 +379,15 @@ var blockDeleteCmd = &cobra.Command{
return fmt.Errorf("--id is required")
}
+ bt := treenode.GetBlockTree(id)
+ if bt == nil {
+ return fmt.Errorf("block not found: %s", id)
+ }
if dryRun {
fmt.Printf("[dry-run] Would delete block %s\n", id)
return nil
}
- bt := treenode.GetBlockTree(id)
-
transactions := []*model.Transaction{{
DoOperations: []*model.Operation{{
Action: "delete",
diff --git a/kernel/cli/cmd/block_test.go b/kernel/cli/cmd/block_test.go
index 3da8bcf7e2..b6d3a2b411 100644
--- a/kernel/cli/cmd/block_test.go
+++ b/kernel/cli/cmd/block_test.go
@@ -13,6 +13,8 @@ import (
"encoding/json"
"strings"
"testing"
+
+ "github.com/spf13/cobra"
)
func TestWriteBlockWriteResult(t *testing.T) {
@@ -50,3 +52,16 @@ func TestWriteBlockWriteResultRejectsEmptyID(t *testing.T) {
t.Fatal("expected empty block ID to fail")
}
}
+
+func TestBlockDeleteRejectsMissingBlock(t *testing.T) {
+ previous := dryRun
+ t.Cleanup(func() { dryRun = previous })
+ for _, dry := range []bool{false, true} {
+ dryRun = dry
+ command := &cobra.Command{}
+ command.Flags().String("id", "20260913000000-missing", "")
+ if err := blockDeleteCmd.RunE(command, nil); err == nil || !strings.Contains(err.Error(), "block not found") {
+ t.Fatalf("missing block accepted (dry-run=%v): %v", dry, err)
+ }
+ }
+}
diff --git a/kernel/cli/cmd/export.go b/kernel/cli/cmd/export.go
index e2d251aede..1d8c8cde9e 100644
--- a/kernel/cli/cmd/export.go
+++ b/kernel/cli/cmd/export.go
@@ -41,17 +41,16 @@ var exportMdCmd = &cobra.Command{
}
output, _ := cmd.Flags().GetString("output")
- if dryRun && output != "" {
+ if dryRun {
+ if output == "" {
+ output = "stdout"
+ }
fmt.Printf("[dry-run] Would export markdown for document %s to %s\n", id, output)
return nil
}
_, content := model.ExportMarkdownContent(id, 4, 0, true, false, false, false, false)
- if output != "" {
- return os.WriteFile(output, []byte(content), 0644)
- }
- fmt.Print(content)
- return nil
+ return writeExportContent(content, output)
},
}
@@ -65,17 +64,16 @@ var exportHTMLCmd = &cobra.Command{
}
output, _ := cmd.Flags().GetString("output")
- if dryRun && output != "" {
+ if dryRun {
+ if output == "" {
+ output = "stdout"
+ }
fmt.Printf("[dry-run] Would export HTML for document %s to %s\n", id, output)
return nil
}
_, dom, _ := model.ExportHTML(id, "", false, false, false)
- if output != "" {
- return os.WriteFile(output, []byte(dom), 0644)
- }
- fmt.Print(dom)
- return nil
+ return writeExportContent(dom, output)
},
}
@@ -89,17 +87,16 @@ var exportPreviewCmd = &cobra.Command{
}
output, _ := cmd.Flags().GetString("output")
- if dryRun && output != "" {
+ if dryRun {
+ if output == "" {
+ output = "stdout"
+ }
fmt.Printf("[dry-run] Would export preview HTML for document %s to %s\n", id, output)
return nil
}
html := model.ExportPreview(id, false)
- if output != "" {
- return os.WriteFile(output, []byte(html), 0644)
- }
- fmt.Print(html)
- return nil
+ return writeExportContent(html, output)
},
}
@@ -270,3 +267,14 @@ func init() {
exportCmd.AddCommand(exportMdZipCmd)
exportCmd.AddCommand(exportDataCmd)
}
+
+func writeExportContent(content, output string) error {
+ if content == "" {
+ return fmt.Errorf("export failed: empty content")
+ }
+ if output != "" {
+ return os.WriteFile(output, []byte(content), 0644)
+ }
+ fmt.Print(content)
+ return nil
+}
diff --git a/kernel/cli/cmd/export_test.go b/kernel/cli/cmd/export_test.go
index 73554f11aa..1b0405b263 100644
--- a/kernel/cli/cmd/export_test.go
+++ b/kernel/cli/cmd/export_test.go
@@ -15,6 +15,7 @@ import (
"testing"
"github.com/siyuan-note/siyuan/kernel/util"
+ "github.com/spf13/cobra"
)
func TestMaterializeExportArtifact(t *testing.T) {
@@ -67,3 +68,38 @@ func TestMaterializeExportArtifactRejectsEmptyPath(t *testing.T) {
t.Fatal("expected empty export path to fail")
}
}
+
+func TestWriteExportContentPreservesExistingOutputOnFailure(t *testing.T) {
+ output := filepath.Join(t.TempDir(), "existing.md")
+ if err := os.WriteFile(output, []byte("keep"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if err := writeExportContent("", output); err == nil {
+ t.Fatal("empty export was accepted")
+ }
+ if data, err := os.ReadFile(output); err != nil || string(data) != "keep" {
+ t.Fatalf("output changed: %q %v", data, err)
+ }
+ if err := writeExportContent("exported", output); err != nil {
+ t.Fatal(err)
+ }
+ if data, err := os.ReadFile(output); err != nil || string(data) != "exported" {
+ t.Fatalf("unexpected output: %q %v", data, err)
+ }
+}
+
+func TestTextExportDryRunWithoutOutput(t *testing.T) {
+ previous := dryRun
+ dryRun = true
+ t.Cleanup(func() { dryRun = previous })
+ for _, command := range []*cobra.Command{exportMdCmd, exportHTMLCmd, exportPreviewCmd} {
+ t.Run(command.Name(), func(t *testing.T) {
+ input := &cobra.Command{}
+ input.Flags().String("id", "20260913000000-missing", "")
+ input.Flags().String("output", "", "")
+ if err := command.RunE(input, nil); err != nil {
+ t.Fatal(err)
+ }
+ })
+ }
+}
diff --git a/kernel/cli/cmd/repo.go b/kernel/cli/cmd/repo.go
index 52ca48625c..b6da4f32b1 100644
--- a/kernel/cli/cmd/repo.go
+++ b/kernel/cli/cmd/repo.go
@@ -162,7 +162,9 @@ var repoCheckoutCmd = &cobra.Command{
return nil
}
- model.CheckoutRepoDirect(id)
+ if err := model.CheckoutRepoDirect(id); err != nil {
+ return err
+ }
model.AppendPushReloadFiletreeEntry()
model.AppendPushReloadUIEntry()
fmt.Println("ok")
diff --git a/kernel/cli/cmd/repo_failure_test.go b/kernel/cli/cmd/repo_failure_test.go
new file mode 100644
index 0000000000..e9020f3cb4
--- /dev/null
+++ b/kernel/cli/cmd/repo_failure_test.go
@@ -0,0 +1,26 @@
+package cmd
+
+import (
+ "testing"
+
+ "github.com/siyuan-note/siyuan/kernel/conf"
+ "github.com/siyuan-note/siyuan/kernel/model"
+ "github.com/spf13/cobra"
+)
+
+func TestRepoAndSyncCommandsReportUnavailableOperations(t *testing.T) {
+ previousConf, previousDryRun := model.Conf, dryRun
+ model.Conf = &model.AppConf{Lang: "en", Repo: conf.NewRepo(), Sync: conf.NewSync()}
+ model.Conf.Sync.Enabled = false
+ dryRun = false
+ t.Cleanup(func() { model.Conf, dryRun = previousConf, previousDryRun })
+ for _, command := range []*cobra.Command{repoCheckoutCmd, syncPushCmd, syncPullCmd} {
+ t.Run(command.Name(), func(t *testing.T) {
+ input := &cobra.Command{}
+ input.Flags().String("id", "missing", "")
+ if err := command.RunE(input, nil); err == nil {
+ t.Fatal("unavailable operation reported success")
+ }
+ })
+ }
+}
diff --git a/kernel/cli/cmd/sync.go b/kernel/cli/cmd/sync.go
index d97c8b3891..fecb91424f 100644
--- a/kernel/cli/cmd/sync.go
+++ b/kernel/cli/cmd/sync.go
@@ -55,7 +55,9 @@ var syncPushCmd = &cobra.Command{
return nil
}
- model.SyncDataUpload()
+ if err := model.SyncDataUpload(); err != nil {
+ return err
+ }
fmt.Println("ok")
return nil
},
@@ -70,7 +72,9 @@ var syncPullCmd = &cobra.Command{
return nil
}
- model.SyncDataDownload()
+ if err := model.SyncDataDownload(); err != nil {
+ return err
+ }
fmt.Println("ok")
return nil
},
diff --git a/kernel/model/repository.go b/kernel/model/repository.go
index f65072b078..3a46c7fa55 100644
--- a/kernel/model/repository.go
+++ b/kernel/model/repository.go
@@ -1320,14 +1320,14 @@ func CheckoutRepo(id string) {
task.AppendTask(task.RepoCheckout, checkoutRepo, id)
}
-func CheckoutRepoDirect(id string) {
- checkoutRepo(id)
+func CheckoutRepoDirect(id string) error {
+ return checkoutRepo(id)
}
-func checkoutRepo(id string) {
- var err error
+func checkoutRepo(id string) (err error) {
if 1 > len(Conf.Repo.Key) {
- util.PushErrMsg(Conf.Language(26), 7000)
+ err = errors.New(Conf.Language(26))
+ util.PushErrMsg(err.Error(), 7000)
return
}
FlushTxQueue()
diff --git a/kernel/model/sync.go b/kernel/model/sync.go
index 1e1fed3d8c..08d7f77ce6 100644
--- a/kernel/model/sync.go
+++ b/kernel/model/sync.go
@@ -44,7 +44,8 @@ import (
"github.com/siyuan-note/siyuan/kernel/util"
)
-func SyncDataDownload() {
+func SyncDataDownload() (err error) {
+ err = errors.New("sync download did not complete")
defer logging.Recover()
if !checkSync(false, false, true) {
@@ -54,7 +55,7 @@ func SyncDataDownload() {
scope := lanSyncScope()
latestID := getSyncCloudLatestID()
if "" != latestID {
- _, _ = syncRemoteRequests.do(scope, latestID, func() error {
+ _, err = syncRemoteRequests.do(scope, latestID, func() error {
lockSync()
defer unlockSync()
if syncRemoteRequests.isCompleted(scope, latestID) {
@@ -74,9 +75,10 @@ func SyncDataDownload() {
return
}
defer unlock()
- if err := syncDataDownloadLocked(); nil == err {
+ if err = syncDataDownloadLocked(); nil == err {
completeCurrentSyncRemoteRequest(scope)
}
+ return
}
func syncDataDownloadLocked() (err error) {
@@ -135,7 +137,8 @@ func completeCurrentSyncRemoteRequest(scope string) {
syncRemoteRequests.complete(scope, latest.ID)
}
-func SyncDataUpload() {
+func SyncDataUpload() (err error) {
+ err = errors.New("sync upload did not complete")
defer logging.Recover()
if !checkSync(false, false, true) {
@@ -152,7 +155,7 @@ func SyncDataUpload() {
now := util.CurrentTimeMillis()
Conf.Sync.Synced = now
- err := syncRepoUploadWithDNSRetry()
+ err = syncRepoUploadWithDNSRetry()
code := 1
if err != nil {
code = 2