Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rmacklin opened this issue Jan 3, 2019 · 3 comments

Comments

@rmacklin
Copy link

rmacklin commented Jan 3, 2019

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?

@rmacklin
Copy link
Author

rmacklin commented Jan 3, 2019

If so, would it be okay to add a guard check like if (!path.isAbsolute(filepath)) before we call fs.realpathSync?

I've opened a PR with this change: #100

@xzyfer
Copy link
Owner

xzyfer commented Jan 3, 2019 via email

@rmacklin
Copy link
Author

rmacklin commented Jan 3, 2019

Thanks for your response!

If I remove the path from the index, it won't trigger recompilation of the ancestors. But I want that to happen so that the watch task will display an error if you remove a file that is still imported somewhere. If it's removed from the index, the watch task will just silently carry on despite there being broken code that the developer should address immediately.

Is there another solution you'd be open to?

macropygia pushed a commit to macropygia/sass-graph that referenced this issue Aug 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants