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: Extract built-in Pandoc when running from source https://github.com/siyuan-note/siyuan/issues/19117
Daniel5cac53228b25
diff --git a/.gitignore b/.gitignoreindex 7c59267a35..0e103da463 100644--- a/.gitignore+++ b/.gitignore@@ -15,6 +15,11 @@ app/buildapp/src/types/distapp/kernel*app/SiYuan-Kernel+app/pandoc/bin/+app/pandoc/share/+app/pandoc/COPYING.rtf+app/pandoc/COPYRIGHT.txt+app/pandoc/MANUAL.htmliospprofdiff --git a/kernel/util/pandoc.go b/kernel/util/pandoc.goindex 7a4017bd03..6907f9849e 100644--- a/kernel/util/pandoc.go+++ b/kernel/util/pandoc.go@@ -120,6 +120,10 @@ func GetPandocRuntime() PandocRuntime {}func InitPandoc(customPandocBinPath string) {+ initPandoc(customPandocBinPath, getPandocVer)+}++func initPandoc(customPandocBinPath string, getVersion func(string) string) {if ContainerStd != Container {return}@@ -150,12 +154,13 @@ func InitPandoc(customPandocBinPath string) {}tempPandocDir := filepath.Join(TempDir, "pandoc")- installedPandocBinPath := pandocBinPath(filepath.Join(WorkingDir, "pandoc"))- installedPandocVer := getPandocVer(installedPandocBinPath)+ installedPandocDir := filepath.Join(WorkingDir, "pandoc")+ installedPandocBinPath := pandocBinPath(installedPandocDir)+ installedPandocVer := getVersion(installedPandocBinPath)customPandocBinPath = strings.TrimSpace(customPandocBinPath)if "" != customPandocBinPath && !isPathWithin(tempPandocDir, customPandocBinPath) {- if pandocVer := getPandocVer(customPandocBinPath); "" != pandocVer {+ if pandocVer := getVersion(customPandocBinPath); "" != pandocVer {runtimeState.BinPath = customPandocBinPathlogging.LogInfof("custom pandoc [ver=%s, bin=%s]", pandocVer, runtimeState.BinPath)removeLegacyPandocDir(tempPandocDir)@@ -171,7 +176,52 @@ func InitPandoc(customPandocBinPath string) {}removeLegacyPandocDir(tempPandocDir)- logging.LogErrorf("built-in pandoc executable [%s] is unavailable", installedPandocBinPath)+ pandocZip := pandocZipPath(installedPandocDir, runtime.GOOS, runtime.GOARCH)+ if "" == pandocZip || !gulu.File.IsExist(pandocZip) {+ logging.LogErrorf("built-in pandoc executable [%s] is unavailable", installedPandocBinPath)+ return+ }++ if err := gulu.Zip.Unzip(pandocZip, installedPandocDir); err != nil {+ logging.LogErrorf("unzip built-in pandoc [%s] failed: %s", pandocZip, err)+ return+ }+ if (gulu.OS.IsDarwin() || gulu.OS.IsLinux()) && "" != installedPandocBinPath {+ if err := os.Chmod(installedPandocBinPath, 0755); err != nil {+ logging.LogErrorf("set built-in pandoc executable [%s] permission failed: %s", installedPandocBinPath, err)+ return+ }+ }++ installedPandocVer = getVersion(installedPandocBinPath)+ if "" == installedPandocVer {+ logging.LogErrorf("initialized built-in pandoc executable [%s] is invalid", installedPandocBinPath)+ return+ }+ runtimeState.BinPath = installedPandocBinPath+ logging.LogInfof("initialized built-in pandoc [ver=%s, bin=%s]", installedPandocVer, runtimeState.BinPath)+}++func pandocZipPath(pandocDir, goos, goarch string) string {+ name := ""+ switch goos {+ case "windows":+ if "amd64" == goarch {+ name = "pandoc-windows-amd64.zip"+ }+ case "darwin":+ if "amd64" == goarch || "arm64" == goarch {+ name = "pandoc-darwin-" + goarch + ".zip"+ }+ case "linux":+ if "amd64" == goarch || "arm64" == goarch {+ name = "pandoc-linux-" + goarch + ".zip"+ }+ }+ if "" == name {+ return ""+ }+ return filepath.Join(pandocDir, name)}func pandocBinPath(pandocDir string) string {diff --git a/kernel/util/pandoc_test.go b/kernel/util/pandoc_test.goindex 39f72500d9..ed9559d453 100644--- a/kernel/util/pandoc_test.go+++ b/kernel/util/pandoc_test.go@@ -17,25 +17,90 @@package utilimport (+ "archive/zip""os""path/filepath"+ "runtime""testing")-func TestInitPandocDoesNotUseWorkspaceTemp(t *testing.T) {- originalContainer, originalTempDir, originalWorkingDir := Container, TempDir, WorkingDir- originalRuntime := GetPandocRuntime()- t.Cleanup(func() {- Container, TempDir, WorkingDir = originalContainer, originalTempDir, originalWorkingDir- pandocInitMutex.Lock()- activePandocRuntime = originalRuntime- pandocInitMutex.Unlock()+func TestPandocZipPath(t *testing.T) {+ pandocDir := filepath.Join("resources", "pandoc")+ tests := []struct {+ name string+ goos string+ goarch string+ want string+ }{+ {name: "Windows AMD64", goos: "windows", goarch: "amd64", want: "pandoc-windows-amd64.zip"},+ {name: "Windows ARM64", goos: "windows", goarch: "arm64"},+ {name: "Darwin AMD64", goos: "darwin", goarch: "amd64", want: "pandoc-darwin-amd64.zip"},+ {name: "Darwin ARM64", goos: "darwin", goarch: "arm64", want: "pandoc-darwin-arm64.zip"},+ {name: "Linux AMD64", goos: "linux", goarch: "amd64", want: "pandoc-linux-amd64.zip"},+ {name: "Linux ARM64", goos: "linux", goarch: "arm64", want: "pandoc-linux-arm64.zip"},+ {name: "Unsupported OS", goos: "freebsd", goarch: "amd64"},+ {name: "Unsupported architecture", goos: "linux", goarch: "386"},+ }+ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ got := pandocZipPath(pandocDir, test.goos, test.goarch)+ if "" == test.want {+ if "" != got {+ t.Fatalf("unexpected Pandoc archive path: %q", got)+ }+ return+ }+ want := filepath.Join(pandocDir, test.want)+ if want != got {+ t.Fatalf("Pandoc archive path: got %q, want %q", got, want)+ }+ })+ }+}++func TestInitPandocExtractsDevelopmentArchive(t *testing.T) {+ root := preparePandocTest(t)+ installedPandocDir := filepath.Join(WorkingDir, "pandoc")+ installedPandocBin := pandocBinPath(installedPandocDir)+ if "" == installedPandocBin {+ t.Skip("Pandoc is not supported on this platform")+ }+ pandocZip := pandocZipPath(installedPandocDir, runtime.GOOS, runtime.GOARCH)+ if "" == pandocZip {+ t.Skip("Pandoc archive is not available on this platform")+ }+ writePandocTestArchive(t, pandocZip, installedPandocBin, []byte("test-pandoc"))++ initPandoc("", func(binPath string) string {+ data, err := os.ReadFile(binPath)+ if nil == err && "test-pandoc" == string(data) {+ return "test"+ }+ return ""})- root := t.TempDir()- Container = ContainerStd- TempDir = filepath.Join(root, "temp")- WorkingDir = filepath.Join(root, "resources")+ if runtimeState := GetPandocRuntime(); installedPandocBin != runtimeState.BinPath {+ t.Fatalf("built-in Pandoc path: got %q, want %q", runtimeState.BinPath, installedPandocBin)+ }+ if data, err := os.ReadFile(installedPandocBin); nil != err || "test-pandoc" != string(data) {+ t.Fatalf("read extracted Pandoc executable failed: %v", err)+ }+ if "windows" != runtime.GOOS {+ info, err := os.Stat(installedPandocBin)+ if nil != err {+ t.Fatal(err)+ }+ if 0 == info.Mode().Perm()&0111 {+ t.Fatalf("extracted Pandoc executable is not executable: %v", info.Mode())+ }+ }+ if _, err := os.Stat(filepath.Join(root, "temp", "pandoc")); !os.IsNotExist(err) {+ t.Fatalf("Pandoc was extracted into the workspace temporary directory: %v", err)+ }+}++func TestInitPandocDoesNotUseWorkspaceTemp(t *testing.T) {+ preparePandocTest(t)legacyPandocDir := filepath.Join(TempDir, "pandoc")legacyPandocBin := pandocBinPath(legacyPandocDir)if "" == legacyPandocBin {@@ -57,3 +122,50 @@ func TestInitPandocDoesNotUseWorkspaceTemp(t *testing.T) {t.Fatalf("legacy Pandoc temporary directory was not removed: %v", err)}}++func preparePandocTest(t *testing.T) string {+ t.Helper()+ originalContainer, originalTempDir, originalWorkingDir := Container, TempDir, WorkingDir+ originalRuntime := GetPandocRuntime()+ t.Cleanup(func() {+ Container, TempDir, WorkingDir = originalContainer, originalTempDir, originalWorkingDir+ pandocInitMutex.Lock()+ activePandocRuntime = originalRuntime+ pandocInitMutex.Unlock()+ })++ root := t.TempDir()+ Container = ContainerStd+ TempDir = filepath.Join(root, "temp")+ WorkingDir = filepath.Join(root, "resources")+ return root+}++func writePandocTestArchive(t *testing.T, archivePath, binPath string, data []byte) {+ t.Helper()+ if err := os.MkdirAll(filepath.Dir(archivePath), 0755); nil != err {+ t.Fatal(err)+ }+ archive, err := os.Create(archivePath)+ if nil != err {+ t.Fatal(err)+ }+ zipWriter := zip.NewWriter(archive)+ relativeBinPath, err := filepath.Rel(filepath.Dir(archivePath), binPath)+ if nil != err {+ t.Fatal(err)+ }+ bin, err := zipWriter.Create(filepath.ToSlash(relativeBinPath))+ if nil != err {+ t.Fatal(err)+ }+ if _, err = bin.Write(data); nil != err {+ t.Fatal(err)+ }+ if err = zipWriter.Close(); nil != err {+ t.Fatal(err)+ }+ if err = archive.Close(); nil != err {+ t.Fatal(err)+ }+}