gruntjs / grunt-contrib-requirejs

Optimize RequireJS projects using r.js.
http://gruntjs.com/
MIT License
504 stars 105 forks source link

Error When Executing with grunt-runner in Atom/Electron Enviroment #103

Closed DSpeckhals closed 9 years ago

DSpeckhals commented 9 years ago

I have started to dabble around with Github's Atom text editor, and have run into a consistent problem when attempting to execute this task with Atom's grunt-runner package on Windows 7 (problem reproduced on two different machines). The error states: EBADF: bad file descriptor, write, which seems to be a file permissions issue within node. However, I decided to test the grunt-requirejs task instead, and it ran normally. After a lot of tinkering, I found out that this exception is specifically being thrown in tasks/requirejs.js while try to require the actual requirejs module. I noticed that this action is executed outside of the multitask so node/print can be defined.

  var requirejs = require('requirejs');
  var LOG_LEVEL_TRACE = 0, LOG_LEVEL_WARN = 2;

  // TODO: extend this to send build log to grunt.log.ok / grunt.log.error
  // by overriding the r.js logger (or submit issue to r.js to expand logging support)
  requirejs.define('node/print', [], function() {
    return function print(msg) {
      if (msg.substring(0, 5) === 'Error') {
        grunt.log.errorlns(msg);
        grunt.fail.warn('RequireJS failed.');
      } else {
        grunt.log.oklns(msg);
      }
    };
  });

The interesting thing is that if I move this block of code inside of the multitask (so it is executed with the actual task, instead of during the registration of the task), everything runs as expected without errors.

I'm not sure if this is the proper long term solution for this, but my question is why are low-level file permission errors being thrown here. Just as a note, I tested this grunt task on the command line and within a different text-editor, and it was fine there too.

I am using Grunt 0.4.5, with grunt-contrib-requirejs 0.4.4.

DSpeckhals commented 9 years ago

After digging into why the require call was failing outside of the multitask, I found that the error is caused by Grunt running the requirejs task in the Atom environment. Specifically, r.js detects Atom as a browser, rather than Node. I opened an issue at jrburke/r.js#840 to track this.

Yes, I guess you could move the import of requirejs into the multitask, but this seems to go against typical Grunt task practices. For now, though, I believe that this can be closed.