An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Resolve mobile workspace deletion failure https://github.com/siyuan-note/siyuan/issues/18848
Daniel26ca7716fa65
diff --git a/app/src/config/tabs/appTab.ts b/app/src/config/tabs/appTab.tsindex 57ff158e94..98f94ae733 100644--- a/app/src/config/tabs/appTab.ts+++ b/app/src/config/tabs/appTab.ts@@ -146,14 +146,21 @@ const mountAppWorkspaceSlot = (root: HTMLElement) => {if (!removePath) {break;}- fetchPost("/api/system/removeWorkspaceDir", {path: removePath}, () => {- renderWorkspaceList(workspaceDirElement);- confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.removeWorkspacePhysically.replace("${x}", removePath), () => {- fetchPost("/api/system/removeWorkspaceDirPhysically", {path: removePath});- }, undefined, true);- });event.preventDefault();event.stopPropagation();+ if (removePath === window.siyuan.config.system.workspaceDir) {+ fetchPost("/api/system/removeWorkspaceDir", {path: removePath});+ break;+ }+ confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.removeWorkspacePhysically.replace("${x}", removePath), () => {+ fetchPost("/api/system/removeWorkspaceDirPhysically", {path: removePath}, () => {+ renderWorkspaceList(workspaceDirElement);+ });+ }, () => {+ fetchPost("/api/system/removeWorkspaceDir", {path: removePath}, () => {+ renderWorkspaceList(workspaceDirElement);+ });+ }, true);break;} else if (target.classList.contains("b3-list-item") && !target.classList.contains("b3-list-item--focus")) {const workspacePath = target.getAttribute("data-path");diff --git a/app/src/menus/workspace.ts b/app/src/menus/workspace.tsindex 7b429e9da2..5145baee22 100644--- a/app/src/menus/workspace.ts+++ b/app/src/menus/workspace.ts@@ -302,11 +302,15 @@ export const workspaceMenu = (app: App, rect: DOMRect) => {if (hasClosestByClassName(event.target as Element, "b3-menu__action")) {event.preventDefault();event.stopPropagation();- fetchPost("/api/system/removeWorkspaceDir", {path: item.path}, () => {- confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.removeWorkspacePhysically.replace("${x}", item.path), () => {- fetchPost("/api/system/removeWorkspaceDirPhysically", {path: item.path});- }, undefined, true);- });+ if (item.path === window.siyuan.config.system.workspaceDir) {+ fetchPost("/api/system/removeWorkspaceDir", {path: item.path});+ return;+ }+ confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.removeWorkspacePhysically.replace("${x}", item.path), () => {+ fetchPost("/api/system/removeWorkspaceDirPhysically", {path: item.path});+ }, () => {+ fetchPost("/api/system/removeWorkspaceDir", {path: item.path});+ }, true);return;}confirmDialog(window.siyuan.languages.confirm, `${pathPosix().basename(window.siyuan.config.system.workspaceDir)} -> ${pathPosix().basename(item.path)}?`, () => {diff --git a/kernel/api/workspace.go b/kernel/api/workspace.goindex 8c2ed59949..634f960868 100644--- a/kernel/api/workspace.go+++ b/kernel/api/workspace.go@@ -21,7 +21,6 @@ import ("net/http""os""path/filepath"- "slices""sort""strings""time"@@ -189,9 +188,9 @@ func removeWorkspaceDirPhysically(c *gin.Context) {if rejectMobileWorkspaceBaseDir(ret, cleanPath) {return}- if cleanPath == util.WorkspaceDir {+ if util.IsWorkspaceLocked(cleanPath) || cleanPath == util.WorkspaceDir {ret.Code = -1- ret.Msg = "cannot remove current workspace"+ ret.Msg = "cannot remove opened workspace"return}if !util.IsWorkspaceDir(cleanPath) {@@ -205,8 +204,8 @@ func removeWorkspaceDirPhysically(c *gin.Context) {ret.Msg = err.Error()return}- isKnown := slices.Contains(knownPaths, cleanPath)- if !isKnown {+ remainingPaths := util.RemoveWorkspacePath(knownPaths, cleanPath)+ if len(remainingPaths) == len(knownPaths) {ret.Code = -1ret.Msg = "path is not a registered workspace"return@@ -217,11 +216,13 @@ func removeWorkspaceDirPhysically(c *gin.Context) {ret.Msg = err.Error()return}+ if err = util.WriteWorkspacePaths(remainingPaths); err != nil {+ ret.Code = -1+ ret.Msg = err.Error()+ return+ }logging.LogInfof("removed workspace [%s] physically", path)- if util.WorkspaceDir == path {- os.Exit(logging.ExitCodeOk)- }}type Workspace struct {diff --git a/kernel/api/workspace_test.go b/kernel/api/workspace_test.goindex b01e18201c..4e4047f98f 100644--- a/kernel/api/workspace_test.go+++ b/kernel/api/workspace_test.go@@ -111,6 +111,67 @@ func TestRemoveWorkspaceDirPhysicallyProtectsBaseDir(t *testing.T) {}}+func TestRemoveWorkspaceDirPhysicallyRemovesRegisteredWorkspace(t *testing.T) {+ rootDir := t.TempDir()+ homeDir := filepath.Join(rootDir, "home")+ workspaceConfDir := filepath.Join(homeDir, ".config", "siyuan")+ currentWorkspaceDir := filepath.Join(rootDir, "current")+ targetWorkspaceDir := filepath.Join(rootDir, "target")+ for _, dir := range []string{workspaceConfDir, filepath.Join(targetWorkspaceDir, "conf")} {+ if err := os.MkdirAll(dir, 0755); err != nil {+ t.Fatal(err)+ }+ }+ if err := os.WriteFile(filepath.Join(targetWorkspaceDir, "conf", "conf.json"), []byte(`{"kernelVersion":"test"}`), 0644); err != nil {+ t.Fatal(err)+ }++ originalContainer, originalHomeDir, originalWorkspaceDir := util.Container, util.HomeDir, util.WorkspaceDir+ util.Container = util.ContainerStd+ util.HomeDir = homeDir+ util.WorkspaceDir = currentWorkspaceDir+ t.Cleanup(func() {+ util.Container, util.HomeDir, util.WorkspaceDir = originalContainer, originalHomeDir, originalWorkspaceDir+ })+ if err := util.WriteWorkspacePaths([]string{targetWorkspaceDir}); err != nil {+ t.Fatal(err)+ }++ gin.SetMode(gin.TestMode)+ engine := gin.New()+ engine.POST("/api/system/removeWorkspaceDirPhysically", removeWorkspaceDirPhysically)+ recorder := httptest.NewRecorder()+ request := httptest.NewRequest(http.MethodPost, "http://127.0.0.1/api/system/removeWorkspaceDirPhysically",+ strings.NewReader(`{"path":"`+filepath.ToSlash(targetWorkspaceDir)+`"}`))+ request.Header.Set("Content-Type", "application/json")+ engine.ServeHTTP(recorder, request)++ response := &struct {+ Code int `json:"code"`+ }{}+ if err := json.Unmarshal(recorder.Body.Bytes(), response); err != nil {+ t.Fatalf("unmarshal removeWorkspaceDirPhysically response failed: %s", err)+ }+ if 0 != response.Code {+ t.Fatalf("removeWorkspaceDirPhysically failed: %s", recorder.Body.String())+ }+ if _, err := os.Stat(targetWorkspaceDir); !os.IsNotExist(err) {+ t.Fatalf("workspace directory should be removed: %v", err)+ }++ workspacePathsData, err := os.ReadFile(filepath.Join(workspaceConfDir, "workspace.json"))+ if err != nil {+ t.Fatal(err)+ }+ var workspacePaths []string+ if err = json.Unmarshal(workspacePathsData, &workspacePaths); err != nil {+ t.Fatal(err)+ }+ if slices.Contains(workspacePaths, targetWorkspaceDir) {+ t.Fatalf("removed workspace should be unregistered: %v", workspacePaths)+ }+}+func prepareMobileWorkspaceBaseDirTest(t *testing.T) (packageDir, workspaceBaseDir string) {t.Helper()packageDir = t.TempDir()