Search code examples
javascriptgulparchlinuxgulp-watch

Gulp watch /tmp permission denied


I'm using gulp in one of my projects. When running gulp watch from OS (Arch Linux)

fs.js:921
  return binding.readdir(pathModule.toNamespacedPath(path), options.encoding);
             ^

Error: EACCES: permission denied, scandir '/tmp/systemd-private-c33e4391b18f4b24af3055190fb15730-systemd-hostnamed.service-zrxyaX/'
at Object.fs.readdirSync (fs.js:921:18)
at Gaze._addToWatched (/home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:274:22)
at Gaze._internalAdd (/home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:191:10)
at /home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:400:16
at Array.forEach (<anonymous>)
at /home/majmun/code/wp/wp-content/plugins/project/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:396:12
at FSReqWrap.oncomplete (fs.js:153:20)

I'm running gulp from host OS instead from a virtual machine because it's much faster.

I understand that problem is that gulp is doing something in /tmp folder of host OS, and some files have root privileges.

If I run sudo chown -R majmun:users /tmp, after while a new file with root privileges appears which break gulp.

Why gulp watch need /tmp folder. Did someone resolve this problem?

Here is code of gulp watch task // gulp watch

 Gulp.task('watch', ['dev'], function() {
        console.log('Initializing assets watcher:')

    let stream = new Stream();
    let filter = '*.{css,scss,sass,less,js,png,jpg,jpeg,svg}';
    let tmp = Tmp.fileSync();
    let glob = [
        path.src.assets + '**/' + filter,
        path.src.elements + '**/' + filter,
        path.src.properties + '**/' + filter,
    ];

    for (let i = 0; i < glob.length; i++) {
        console.log('- ' + glob[i]);
    }

    let watcher = Gulp.watch(glob, ['dev']);
    watcher.add(tmp.name);

    process.on('SIGINT', function() {
        stream.emit('end');
        process.exit(0);
    });

    KeyPress(process.stdin);
    process.stdin.on('keypress', function(char, key) {
        if (key && key.ctrl && key.name == 'c') {
            process.emit('SIGINT');
        }
        else if (key && key.ctrl && key.name == 'r') {
            Fs.utimes(tmp.name, new Date(), new Date(), function() {});
        }
    });
    process.stdin.setRawMode(true);
    process.stdin.resume();

    console.log('Watcher up and running, Ctrl+R to refresh, Ctrl+C to exit.');
    return stream;
});

Solution

  • I fix this by adding some option when using tmp library. Instead of default options I specify where temporary folder will be and it's permission.

    So instead of this:

    let tmp = Tmp.fileSync();
    

    I did this:

    let tmp = Tmp.fileSync({ 
        // Use settings from .env if defined 
        dir: process.env.MY_TMP_DIR || ''
        mode: process.env.MY_TMP_MODE || '', 
        prefix: process.env.MY_TMP_PREFIX || '', 
    });