Skip to content

Commit 93b5fa8

Browse files
committed
chore(compiler): add debug statements for file watching
this commit is a follow up to #4146. when debugging the file watcher, it's valuable to know what files have been perceived as added/updated/etc. for #4146, we created dev-builds with additional logging statements to help diagnose the issue. in this commit, we add more refined debugging statements that attempt to balance providing useful information while not spamming the output with logging statements. when starting a dev server, the version of stencil has been added to the `[LOCAL DEV]`. starting with #4098, stencil has useful information in both dev and prod builds of the project that can is used here. this was also added to avoid a call to stencil's info task - most of the information from the info task is already provided in the output of starting of the dev server. by adding only the version here, we avoid redundant logging statements. the watch build task has been updated to log the files changed (added, updated, deleted, written). these statements can get slightly verbose, with at least one line per category per rebuild. however, this information was immensely helpful in #4146, and was deemed worth the extra output. the output is only provided when the logging level of the stencil logger is set to "debug", and won't be enabled for normal runs of the compiler. similar to the watch build task, additional debug-only logging was added to the node-sys watch file-related functions. debug statements in this file are not guarded with a conditional statement, as we do not have any guarantees/information around the logging level that has been set when a node-sys instance is created. in order to log correctly, an optional logger (optional as to not break the contract of this public api) has been added to the creation of node-sys. should one not be provided, debug logging will not apply.
1 parent b34e83a commit 93b5fa8

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/cli/logs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const startupLogVersion = (logger: Logger, task: TaskCommand, coreCompile
4040
let startupMsg: string;
4141

4242
if (isDevBuild) {
43-
startupMsg = logger.yellow('[LOCAL DEV]');
43+
startupMsg = logger.yellow(`[LOCAL DEV] v${coreCompiler.version}`);
4444
} else {
4545
startupMsg = logger.cyan(`v${coreCompiler.version}`);
4646
}

src/compiler/build/watch-build.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,19 @@ export const createWatchBuild = async (
5353
buildCtx.hasHtmlChanges = hasHtmlChanges(config, buildCtx);
5454
buildCtx.hasServiceWorkerChanges = hasServiceWorkerChanges(config, buildCtx);
5555

56+
if (config.flags.debug) {
57+
config.logger.debug(`WATCH_BUILD::watchBuild::onBuild filesAdded: ${formatFilesForDebug(buildCtx.filesAdded)}`);
58+
config.logger.debug(
59+
`WATCH_BUILD::watchBuild::onBuild filesDeleted: ${formatFilesForDebug(buildCtx.filesDeleted)}`
60+
);
61+
config.logger.debug(
62+
`WATCH_BUILD::watchBuild::onBuild filesUpdated: ${formatFilesForDebug(buildCtx.filesUpdated)}`
63+
);
64+
config.logger.debug(
65+
`WATCH_BUILD::watchBuild::onBuild filesWritten: ${formatFilesForDebug(buildCtx.filesWritten)}`
66+
);
67+
}
68+
5669
dirsAdded.clear();
5770
dirsDeleted.clear();
5871
filesAdded.clear();
@@ -70,6 +83,22 @@ export const createWatchBuild = async (
7083
}
7184
};
7285

86+
/**
87+
* Utility method for formatting a debug message that must either list a number of files, or the word 'none' if the
88+
* provided list is empty
89+
* @param files a list of files, the list may be empty
90+
* @returns the provided list if it is not empty. otherwise, return the word 'none'
91+
*/
92+
const formatFilesForDebug = (files: ReadonlyArray<string>): string => {
93+
/**
94+
* In the created message, it's important that there's no whitespace prior to the file name.
95+
* Stencil's logger will split messages by whitespace according to the width of the terminal window.
96+
* Since file names can be fully qualified paths (and therefore quite long), putting whitespace between a '-' and
97+
* the path can lead to formatted messages where the '-' is on its own line
98+
*/
99+
return files.length > 0 ? files.map((filename: string) => `-${filename}`).join('\n') : 'none';
100+
};
101+
73102
const start = async () => {
74103
const srcRead = watchSrcDirectory(config, compilerCtx);
75104
const otherRead = watchRootFiles(config, compilerCtx);
@@ -104,7 +133,7 @@ export const createWatchBuild = async (
104133
break;
105134
}
106135

107-
config.logger.debug(`onFsChange ${eventKind}: ${p}`);
136+
config.logger.debug(`WATCH_BUILD::fs_event_change - type=${eventKind}, path=${p}, time=${new Date().getTime()}`);
108137
tsWatchProgram.rebuild();
109138
}
110139
};

src/sys/node/node-sys.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
CompilerSystemRealpathResults,
1818
CompilerSystemRemoveFileResults,
1919
CompilerSystemWriteFileResults,
20+
Logger,
2021
} from '../../declarations';
2122
import { asyncGlob, nodeCopyTasks } from './node-copy-tasks';
2223
import { NodeLazyRequire } from './node-lazy-require';
@@ -30,11 +31,12 @@ import { NodeWorkerController } from './node-worker-controller';
3031
*
3132
* This takes an optional param supplying a `process` object to be used.
3233
*
33-
* @param c an optional object wrapping a `process` object
34+
* @param c an optional object wrapping `process` and `logger` objects
3435
* @returns a node.js `CompilerSystem` object
3536
*/
36-
export function createNodeSys(c: { process?: any } = {}): CompilerSystem {
37+
export function createNodeSys(c: { process?: any; logger?: Logger } = {}): CompilerSystem {
3738
const prcs: NodeJS.Process = c?.process ?? global.process;
39+
const logger: Logger = c?.logger;
3840
const destroys = new Set<() => Promise<void> | void>();
3941
const onInterruptsCallbacks: (() => void)[] = [];
4042

@@ -457,9 +459,12 @@ export function createNodeSys(c: { process?: any } = {}): CompilerSystem {
457459
sys.events = buildEvents();
458460

459461
sys.watchDirectory = (p, callback, recursive) => {
462+
logger?.debug(`NODE_SYS_DEBUG::watchDir ${p}`);
463+
460464
const tsFileWatcher = tsSysWatchDirectory(
461465
p,
462466
(fileName) => {
467+
logger?.debug(`NODE_SYS_DEBUG::watchDir:callback dir=${p} changedPath=${fileName}`);
463468
callback(normalizePath(fileName), 'fileUpdate');
464469
},
465470
recursive
@@ -496,6 +501,8 @@ export function createNodeSys(c: { process?: any } = {}): CompilerSystem {
496501
* @returns an object with a method for unhooking the file watcher from the system
497502
*/
498503
sys.watchFile = (path: string, callback: CompilerFileWatcherCallback): CompilerFileWatcher => {
504+
logger?.debug(`NODE_SYS_DEBUG::watchFile ${path}`);
505+
499506
const tsFileWatcher = tsSysWatchFile(
500507
path,
501508
(fileName: string, tsEventKind: TypeScript.FileWatcherEventKind) => {

0 commit comments

Comments
 (0)