prevent conflic between obsidian sync and headless sync.

This commit is contained in:
Nystik
2026-03-30 22:15:10 +02:00
parent 300e251734
commit 0d7f36ca9b
8 changed files with 212 additions and 109 deletions

View File

@@ -1,9 +1,14 @@
const { Notice } = require("obsidian");
const fs = require("fs"); // Using fs shim
const fs = require("fs");
const CORE_PLUGINS_PATH = ".obsidian/core-plugins.json";
// Reads core-plugins.json via the fs shim. When headless sync is active,
// the shim patches sync: false, so this returns false. When the flag is
// cleared (user action), this returns the real value.
function isCoreSyncEnabled() {
try {
const data = fs.readFileSync(".obsidian/core-plugins.json", "utf-8");
const data = fs.readFileSync(CORE_PLUGINS_PATH, "utf-8");
const config = JSON.parse(data);
return config.sync === true;
} catch {
@@ -27,11 +32,31 @@ function showConflictWarning(title, message) {
});
}
function startCoreSyncWatcher(plugin, api, wsListener) {
function startCoreSyncGuard(plugin, api, wsListener) {
const app = plugin.app;
const vaultId = app.vault.getName();
// Monkey-patch syncPlugin.enable() to clear the shim flag before
// Obsidian writes core-plugins.json. This ensures the read transform
// doesn't block a user-initiated core sync enable.
const syncPlugin = app.internalPlugins.getPluginById("sync");
let origEnable = null;
if (syncPlugin) {
origEnable = syncPlugin.enable.bind(syncPlugin);
syncPlugin.enable = function (...args) {
window.__ignisHeadlessSyncActive = false;
api.stopSync(vaultId).catch(() => {});
return origEnable(...args);
};
}
// Watch for core-plugins.json changes via WebSocket.
let wasEnabled = isCoreSyncEnabled();
const rawHandler = (msg) => {
if (msg.type === "modified" && msg.path === ".obsidian/core-plugins.json") {
if (msg.type === "modified" && msg.path === CORE_PLUGINS_PATH) {
handleCoreSyncChange();
}
};
@@ -42,9 +67,6 @@ function startCoreSyncWatcher(plugin, api, wsListener) {
const enabled = isCoreSyncEnabled();
if (enabled && !wasEnabled) {
const vaultId = plugin.app.vault.getName();
api.stopSync(vaultId).catch(() => {});
showConflictWarning(
"Headless Sync Stopped",
"Obsidian Sync has been enabled. Headless Sync has been automatically " +
@@ -59,11 +81,15 @@ function startCoreSyncWatcher(plugin, api, wsListener) {
return {
cleanup() {
wsListener.offRaw();
if (syncPlugin && origEnable) {
syncPlugin.enable = origEnable;
}
},
};
}
module.exports = {
isCoreSyncEnabled,
startCoreSyncWatcher,
startCoreSyncGuard,
};

View File

@@ -2,7 +2,7 @@ const { Plugin } = require("obsidian");
const { HeadlessSyncSettingTab } = require("./settings-tab");
const { WsListener } = require("./ws-listener");
const { initSyncStatusBar } = require("./sync-status-bar");
const { startCoreSyncWatcher } = require("./core-sync-guard");
const { startCoreSyncGuard } = require("./core-sync-guard");
const api = require("./api");
class IgnisHeadlessSyncPlugin extends Plugin {
@@ -14,7 +14,7 @@ class IgnisHeadlessSyncPlugin extends Plugin {
this.addSettingTab(new HeadlessSyncSettingTab(this.app, this));
this._coreSyncWatcher = startCoreSyncWatcher(this, api, this.wsListener);
this._coreSyncGuard = startCoreSyncGuard(this, api, this.wsListener);
this.addCommand({
id: "start-sync",
@@ -53,9 +53,11 @@ class IgnisHeadlessSyncPlugin extends Plugin {
}
onunload() {
if (this._coreSyncWatcher) {
this._coreSyncWatcher.cleanup();
this._coreSyncWatcher = null;
window.__ignisHeadlessSyncActive = false;
if (this._coreSyncGuard) {
this._coreSyncGuard.cleanup();
this._coreSyncGuard = null;
}
if (this._syncStatusBarCleanup) {

View File

@@ -124,12 +124,6 @@ class SyncManager {
throw new Error(`No sync configuration for vault: ${vaultId}`);
}
if (this.isCoreSyncEnabled(state.vaultPath)) {
const msg = `Cannot start sync for ${vaultId}: Obsidian Sync core plugin is enabled`;
this.ctx.log(msg);
throw new Error(msg);
}
if (state.status === "running") {
this.ctx.log(`Sync already running for ${vaultId}`);
return this.getState(vaultId);
@@ -316,31 +310,11 @@ class SyncManager {
}
}
isCoreSyncEnabled(vaultPath) {
try {
const configPath = path.join(vaultPath, ".obsidian", "core-plugins.json");
const data = fs.readFileSync(configPath, "utf-8");
const config = JSON.parse(data);
return config.sync === true;
} catch {
return false;
}
}
autoStartAll() {
let started = 0;
let skipped = 0;
for (const [vaultId, state] of this.states) {
if (state.autoStart && state.status === "stopped") {
if (this.isCoreSyncEnabled(state.vaultPath)) {
this.ctx.log(
`Skipping auto-start for ${vaultId}: Obsidian Sync core plugin is enabled`,
);
skipped++;
continue;
}
try {
this.startSync(vaultId);
started++;
@@ -353,12 +327,6 @@ class SyncManager {
if (started > 0) {
this.ctx.log(`Auto-started sync for ${started} vault(s)`);
}
if (skipped > 0) {
this.ctx.log(
`Skipped ${skipped} vault(s) due to Obsidian Sync being enabled`,
);
}
}
async shutdown() {