From 20f66f495e212725c6780f417410c056513ca20d Mon Sep 17 00:00:00 2001 From: Chad Meyers Date: Wed, 30 Oct 2024 12:05:06 -0700 Subject: [PATCH] fix(debugger): Always filewatch .map and .js from sourceMapRoot, but optionally /reload minecraft. (#249) Enable file watch on `sourceMapRoot` even if /reload is not enabled. This ensures the source map cache is always cleared after build. There are users (editor) that prefer manual /reload in some cases and would need to have updated source maps. Summary: - if no source maps, only enable file watch if /reload is enabled (catches .js changes and reloads MC) - if `globPattern` override is set, enable file watch only if /reload is enabled (this feature was to watch a special pattern) - if `sourceMapRoot` is set, always enable file watch for .map and .js files but don't send /reload unless enabled. From the user's perspective nothing has changed. Source maps are simply cleared more aggressively. --- src/Session.ts | 53 ++++++++++++++++++++++++-------------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/src/Session.ts b/src/Session.ts index dbc9095..e484d37 100644 --- a/src/Session.ts +++ b/src/Session.ts @@ -1024,27 +1024,26 @@ export class Session extends DebugSession { const config = workspace.getConfiguration('minecraft-debugger'); const reloadOnSourceChangesEnabled = config.get('reloadOnSourceChanges.enabled'); - if (!reloadOnSourceChangesEnabled) { - return; - } - const reloadOnSourceChangesDelay = Math.max(config.get('reloadOnSourceChanges.delay') ?? 0, 0); const reloadOnSourceChangesGlobPattern = config.get('reloadOnSourceChanges.globPattern'); - - // Either monitor the build output (TS->JS) by looking at .map and .js files in sourceMapRoot, - // or monitor .js files directly if not using TS or source maps by looking at localRoot, - // or monitor a specific glob pattern for all files within the workspace. + + // watch all files within the workspace matching custom glob pattern. + // only active if Minecraft /reload is enabled let globPattern: RelativePattern | undefined = undefined; - if (reloadOnSourceChangesGlobPattern) { + if (reloadOnSourceChangesGlobPattern && reloadOnSourceChangesEnabled) { const workspaceFolders = workspace.workspaceFolders; if (workspaceFolders && workspaceFolders.length > 0) { globPattern = new RelativePattern(workspaceFolders[0].uri.fsPath ?? '', reloadOnSourceChangesGlobPattern); } } + // watch source map files and reload cache if changed. + // always needed if source maps are used. else if (sourceMapRoot) { globPattern = new RelativePattern(sourceMapRoot, '**/*.{map,js}'); } - else if (localRoot) { + // watch localRoot for .js file changes. + // only needed if Minecraft /reload is enabled + else if (localRoot && reloadOnSourceChangesEnabled) { globPattern = new RelativePattern(localRoot, '**/*.js'); } @@ -1052,26 +1051,24 @@ export class Session extends DebugSession { this._sourceFileWatcher = workspace.createFileSystemWatcher(globPattern, false, false, false); } - const doReload = (): void => { - this._sourceMaps.clearCache(); - this.onRunMinecraftCommand('say §aPerforming Auto-Reload'); - this.onRunMinecraftCommand('reload'); - }; - - const debounce = (func: () => void, wait: number): (() => void) => { - let timeout: NodeJS.Timeout; - return () => { - clearTimeout(timeout); - timeout = setTimeout(func, wait); - }; + let timeout: NodeJS.Timeout; + const onSourceChanged = (): void => { + clearTimeout(timeout); + timeout = setTimeout(() => { + // always clear source maps + this._sourceMaps.clearCache(); + // and optionally reload Minecraft + if (reloadOnSourceChangesEnabled) { + this.onRunMinecraftCommand('say §aPerforming Auto-Reload'); + this.onRunMinecraftCommand('reload'); + } + }, reloadOnSourceChangesDelay); }; - - const debouncedReload = debounce(doReload, reloadOnSourceChangesDelay); - + if (this._sourceFileWatcher) { - this._sourceFileWatcher.onDidChange(debouncedReload); - this._sourceFileWatcher.onDidCreate(debouncedReload); - this._sourceFileWatcher.onDidDelete(debouncedReload); + this._sourceFileWatcher.onDidChange(onSourceChanged); + this._sourceFileWatcher.onDidCreate(onSourceChanged); + this._sourceFileWatcher.onDidDelete(onSourceChanged); } }