Skip to content

Commit

Permalink
fix(debugger): Always filewatch .map and .js from sourceMapRoot, but …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
chmeyer-ms authored Oct 30, 2024
1 parent e63585a commit 20f66f4
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,54 +1024,51 @@ export class Session extends DebugSession {

const config = workspace.getConfiguration('minecraft-debugger');
const reloadOnSourceChangesEnabled = config.get<boolean>('reloadOnSourceChanges.enabled');
if (!reloadOnSourceChangesEnabled) {
return;
}

const reloadOnSourceChangesDelay = Math.max(config.get<number>('reloadOnSourceChanges.delay') ?? 0, 0);
const reloadOnSourceChangesGlobPattern = config.get<string>('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');
}

if (globPattern) {
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);
}
}

Expand Down

0 comments on commit 20f66f4

Please sign in to comment.