Skip to content

Can we avoid calling fs.realpathSync(filepath) if filepath is already an absolute path? #99

@rmacklin

Description

@rmacklin

I ran into an issue trying to use this plugin in my gulp watch task.

I have this code:

const gulp = require('gulp');
const sassGrapher = require('sass-graph');
const touch = require('touch');

const sassGraph = sassGrapher.parseDir('./app/assets/stylesheets');

gulp.task(
  'watch',
  () => {
    // ... (additional watch statements)

    // Trigger recompilation of stylesheet entry points when their dependencies are modified
    gulp.watch([buildStylesTask.watchFiles], (event) => {
      sassGraph.visitAncestors(event.path, (parent) => {
        if (parent.includes('stylesheets/packs')) {
          touch.sync(parent);
        }
      });
    });
  }
);

For the most part, this works pretty well, except if event.path refers to a watched file that was deleted. In that case, the watch task crashes with:

Error: ENOENT: no such file or directory, lstat '/Users/rmacklin/src/m/app/assets/stylesheets/components/tooltip.scss'
    at Object.realpathSync (fs.js:1657:15)
    at Graph.visit (/Users/rmacklin/src/m/node_modules/sass-graph/sass-graph.js:117:17)
    at Graph.visitAncestors (/Users/rmacklin/src/m/node_modules/sass-graph/sass-graph.js:101:8)
    at gulp.watch (/Users/rmacklin/src/m/lib/tasks/assets/watch.js:18:56)
    at Gaze.<anonymous> (/Users/rmacklin/src/m/node_modules/glob-watcher/index.js:18:14)
    at emitTwo (events.js:126:13)
    at Gaze.emit (events.js:214:7)
    at Gaze.emit (/Users/rmacklin/src/m/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:129:32)
    at /Users/rmacklin/src/m/node_modules/glob-watcher/node_modules/gaze/lib/gaze.js:389:18
    at Array.forEach (<anonymous>)

which is caused by this line (on the current master branch, it's this line):

filepath = fs.realpathSync(filepath);

Since the watched file was removed, fs.realpathSync throws that error. However, the event.path value that gets passed for the filepath parameter is already an absolute path, so it seems like in that scenario it is unnecessary to call fs.realpathSync. Does that seem reasonable? If so, would it be okay to add a guard check like if (!path.isAbsolute(filepath)) before we call fs.realpathSync?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions