bun icon indicating copy to clipboard operation
bun copied to clipboard

Implement `fs.watch`

Open robogeek opened this issue 2 years ago • 2 comments

Version

Canary 0.1.5

Platform

Linux davidpc 5.15.0-41-generic #44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

What steps will reproduce the bug?

Earlier I reported that running Chokidar throws lots of errors about stats.isSymbolicLink not existing. That is fixed as of the Canary build of 0.1.5, thank you.

With 0.1.5 there is still an error in Chokidar - fs.watch does not exist, and if you Import the fs module it is indeed undefined.

The following code was written primarily to be a performance test, but it will demonstrate the error.

import { inspect } from 'util';
import { default as chokidar } from 'chokidar';

const dirs = [
// Put a list of directory names here
];

let watcher;

const start = new Date();
let count = 0;

try {
    await new Promise((resolve, reject) => {
        try {
            watcher = chokidar.watch(
                typeof process.argv[2] !== 'undefined'
                ? process.argv[2]
                : dirs
            );
            watcher
            .on('error', async (error) => {
                console.error(error);
                reject(error);
            })
            .on('add', (fpath, stats) => {
                // console.log(`add ${fpath} ${inspect(stats)}`);
                count++;
            })
            .on('change', (fpath, stats) => {
                // console.log(`change ${fpath} ${inspect(stats)}`);
            })
           .on('ready', async () => {
                // console.log(`ready`);
                await close();

                const finish = new Date();

                console.log(`time ${(finish - start) / 1000} seconds - ${count} files`);

                resolve();
            });
        } catch (err) { reject(err); }
    });

} catch (errr) { console.error(errr); }

async function close() {
    await watcher.close();
    watcher = undefined;
}

Another piece of source is this:

import * as fs from 'fs';

console.log(fs.watch);

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior?

With choke.mjs the output for Node is:

$ node choke.mjs
time 1.45 seconds - 6426 files

And for fsw.mjs it is:

$ node fsw.mjs 
[Function: watch]

What do you see instead?

These are run using the Canary build of 0.1.5

$ ~/Projects/bun/bun-linux-x64/bun fsw.mjs 
undefined

And for choke.mjs there is one instance of this error for each file:

115 |       );
116 |     }
117 |   };
118 |   try {
119 | // console.log(fs.watch);
120 |     return fs.watch(path, options, handleEvent);
               ^
TypeError: fs.watch is not a function. (In 'fs.watch(path, options, handleEvent)', 'fs.watch' is undefined)
      at createFsWatchInstance (/home/david/Projects/akasharender/stacked-directories/node_modules/chokidar/lib/nodefs-handler.js:120:11)
      at /home/david/Projects/akasharender/stacked-directories/node_modules/chokidar/lib/nodefs-handler.js:167:14
      at _watchWithNodeFs (/home/david/Projects/akasharender/stacked-directories/node_modules/chokidar/lib/nodefs-handler.js:332:13)
      at _handleFile (/home/david/Projects/akasharender/stacked-directories/node_modules/chokidar/lib/nodefs-handler.js:396:17)
      at /home/david/Projects/akasharender/stacked-directories/node_modules/chokidar/lib/nodefs-handler.js:638:15

Additional information

No response

robogeek avatar Jul 22 '22 19:07 robogeek

fs.watch isn't implemented yet

Jarred-Sumner avatar Jul 22 '22 19:07 Jarred-Sumner

I know watch is problematic even in Node as there are no guarantees it works reliably cross platform but when it comes to Bun core features, being unable to watch a sqlite-wal or any generic database file is kinda a blocker for any logic reacting around changes on it.

This is also a blocker for anyone liking to indirectly communicate back-forward with Python libraries for IoT purpose on Bun capable targets and if this is of any any help, filebus based on inotifywait-spawn provides all primitives to have watch reliable wherever inotify-tools can run.

P.S. inotify-tools are available in both all Linux and Macs via home brew

WebReflection avatar Feb 23 '23 13:02 WebReflection

when it comes to Bun core features, being unable to watch a file is kinda a blocker

Yes important feature needs to be a priority. If not node:fs.watch, at least do Bun.watch()

e3dio avatar May 23 '23 20:05 e3dio

Implemented in #3249 (will be available in release 0.6.10)

Electroid avatar Jun 26 '23 21:06 Electroid