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

[TIMOB-24642] - Correct require implementation #9030

Merged
merged 5 commits into from May 10, 2017

Conversation

ewanharris
Copy link
Collaborator

JIRA: https://jira.appcelerator.org/browse/TIMOB-24642

Note iOS does not like a leading slash on paths, so '/node_modules' in the below is 'node_modules' on iOS

  • Decrement i before continuing
  • If startDir/path === '/' return early with "/node_modules" to prevent unnecessary loop
  • Always appends '/node_modules'
  • On iOS correct the path builder to split the array rather than perform a substring on the string

Test case:

  1. Download the test app linked in the ticket description
  2. Build the app, it should load to a yellow screen and in the console you should see.
[INFO]  boz index.js loaded
[INFO]  boo boo.js loaded
[INFO]  baz index.js loaded
[INFO]  bar bar.js loaded
[INFO]  foo lib/foo.js loaded
[INFO]  foo index.js loaded

@hansemannn hansemannn requested a review from sgtcoolguy May 8, 2017 13:12
@hansemannn hansemannn added this to the 6.2.0 milestone May 8, 2017
@cb1kenobi
Copy link
Contributor

When a path begins with /, it implies a resource, not module. require('/path/to/file.js') is a resource. require('path/to/file') is a module. Module identifiers do not have an extension. Resources require an extension. The initial require() implementation handled / wrong. If you require('/foo'), it should NEVER search node_modules. Not even Node does that.

@@ -385,6 +385,16 @@ Module.prototype.loadNodeModules = function (moduleId, startDir, context) {
*/
Module.prototype.nodeModulesPaths = function (startDir) {
// 1. let PARTS = path split(START)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this comment back down to the line it's referencing below.

@sgtcoolguy
Copy link
Contributor

@ewanharris It'd be useful to add some tests around this in our test suite so we know the issue is fixed, and to verify some of the things we're discussing here.

Copy https://github.com/appcelerator/titanium-mobile-mocha-suite/blob/master/Resources/ti.require.test.js in titanium_mobile/tests/Resources and add some unit tests that verify we don't have an infinite loop. You may need to also override titanium_mobile/tests/Resources/node_modules to include some additional node modules that trigger the bug (right now there's just the abbrev module there: https://github.com/appcelerator/titanium-mobile-mocha-suite/tree/master/Resources/node_modules/abbrev).

As for what @cb1kenobi is talking about, I don't believe we do search for node_modules when the require path is prefixed by ./, ../ or /. This fix is related to gathering the list of possible paths to search for node_modules folders up the directory tree from our current location (in the cases where we should be looking for them).

Copy link
Contributor

@sgtcoolguy sgtcoolguy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting unit tests be added to verify the bug is fixed.

@ewanharris
Copy link
Collaborator Author

@mukherjee2 mukherjee2 self-requested a review May 10, 2017 15:26
Copy link
Contributor

@mukherjee2 mukherjee2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passed FR!
Node Version: 6.10.1
NPM Version: 3.10.10
Mac OS: 10.12.4
Appc CLI: 6.2.0
Appc CLI NPM: 4.2.9
Titanium SDK version: 6.2.0.v20170510080439
Appcelerator Studio, build: 4.8.1.201612050850
Xcode 8.3.2

First tested with 6.0.4.GA. The app hangs. I retried with the fix in this PR, and the app launched and displayed a yellow window. FR passed.

@mukherjee2 mukherjee2 merged commit 92a60d9 into tidev:master May 10, 2017
@ewanharris ewanharris deleted the TIMOB-24642 branch August 31, 2021 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants