Closed Bug 907184 Opened 11 years ago Closed 10 years ago

require does not honor multiline comments

Categories

(Add-on SDK Graveyard :: General, defect, P3)

x86
Linux
defect

Tracking

(Not tracked)

RESOLVED DUPLICATE of bug 915376

People

(Reporter: johannes, Unassigned)

References

Details

User Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:23.0) Gecko/20100101 Firefox/23.0 (Beta/Release)
Build ID: 20130803193142

Steps to reproduce:

// lib/main.js
"use strict";
exports.main = function() {
  /*
  var x = require('./doesnotexist');
  */

  console.log("after commented out require");
}



Actual results:

ModuleNotFoundError: unable to satisfy: require(./doesnotexist) from
  /home/jonas/workspace/shufu/client-side/inject/lib/main.js:4:
Looked for it in:
  /home/jonas/workspace/shufu/client-side/inject/lib/doesnotexist.js



Expected results:

info: inject: after commented out require
OS: All → Linux
Hardware: All → x86
The same happens in conditional blocks:

"""
if (false) {
  var x = require('./doesnotexist');
}
"""

which kind of defeats the purpose of conditional blocks ;)
I am running Ubuntu quantal (Linux obelisk 3.5.0-23-generic #35-Ubuntu SMP Thu Jan 24 13:05:29 UTC 2013 i686 i686 i686 GNU/Linux).
I downloaded the SDK from the website as a zip-ball (https://ftp.mozilla.org/pub/mozilla.org/labs/jetpack/jetpack-sdk-latest.zip
), version addon-sdk-1.14.

Johannes
I think that `var x = require('./doesn'+'otexist');` will work around the issue.
The issue here is that the cfx tool that scans for require usage is written in python and doesn't interpret Javascript syntax. 

It might be possible to fix this once we rewrite cfx in javascript (bug 746124).

Single-line comments using // should be ignored, though, as far as I can tell.
(In reply to Erik Vold [:erikvold] [:ztatic] from comment #3)
> I think that `var x = require('./doesn'+'otexist');` will work around the
> issue.

Or another option is:


var x = './doesnotexist';
require(x);
Thank you for your suggestions.
My issue is less with the behavior now knowing it, but it was a debugging nightmare to find problems caused by commented out code.

I am using a multi-target module that tries to find out the environment it runs in.
I.e. "if (typeof window === 'undefined') {/*node.js*/ require('crypto'); ...}"

Because your parser does not honor the conditional, the require is executed even though it shouldn't be. As "require" is a well-established idiom in javascript, the current semantics make for very tedious debugging.

- "Hmm, seems like the "node.js" path is taken."
- "Let me add a console.log to check if it is taken. Hmm. No."
- "Let me uncomment it to make sure."
- "<Long pause> Hmm. Weird" 

BTW, is there any decent way to debug Jetpack code?

Johannes
So we won't be updating cfx any longer, and after bug 915376 people will use JPM https://github.com/mozilla/jpm which will not have this issue.
Status: UNCONFIRMED → RESOLVED
Closed: 10 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.