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

Fix external relative paths on Windows #1704

Closed
wants to merge 8 commits into from

Conversation

Shingyx
Copy link
Contributor

@Shingyx Shingyx commented Mar 20, 2017

Replace backslashes with forward slashes when adding relative paths for external references.

Replace backslashes with forward slashes when adding relative paths for
external references.
@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 20, 2017

Fixes the following scenario:

  • Set up bundle A which includes a src file './lib/foo.js'.
  • Set up bundle B which includes a src file './test/bar.js' and marks './lib/foo.js' as an external dependency.
  • If './test/bar.js' has the statement require('../lib/foo'), it'll throw the error "Cannot find module '/lib\foo'".

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 20, 2017

Build is failing for the same reason that the master branch is failing https://travis-ci.org/substack/node-browserify/builds/207173302

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 21, 2017

grunt-test.zip
Attached is a small test project which reproduces this problem.

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 21, 2017

...I may have just had a misunderstanding of how Browserify works. I will have to verify tomorrow, but it seems like my change just gave me the illusion of fixing the problem, when in reality it was just including ./lib/foo.js in the second bundle, so my change probably breaks the functionality of the external flag and I was misusing Browserify the whole time. Will close if this is the case.

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 21, 2017

Can confirm. All my change seemed to do was invalidate the external option, making the second bundle just include the "external" files. However, I think my scenario suggests that there could be an issue somewhere in Browserify's external option, given the error Cannot find module '/lib\foo' was thrown, but my change does nothing to address this issue (if there is one at all).

@Shingyx Shingyx closed this Mar 21, 2017
@dwelle
Copy link

dwelle commented Mar 24, 2017

I can confirm the backslashes on windows are causing problems. Why did you conclude your proposed change is incorrect?

Related #1584, which was not resolved either.
Also #1613, #1511 and numerous other issues

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 28, 2017

Firstly, ignore my previous grunt-test.zip. It was more complicated than necessary.

grunt-test.zip
This is a much simpler project which demonstrates the problem. For context, I want to bundle a bunch of production code inside lib-bundle.js and the test code inside test-bundle.js. For those tests, I want the dependencies to come from lib-bundle.js, AKA exactly what will be used in production. I use index.html to run those tests (usually would be using Mocha or something).

dist-before.zip
This zip contains those two bundles before my changes. This is everything I wanted, except the last line of test-bundle.js has {"../lib/library":"/lib\\library.js"}]}, saying 'to import "../lib/library", look up "/lib\library.js"'. As a result, opening index.html will give "Uncaught Error: Cannot find module '/lib\library.js'". Simply replacing the \\ on this line with / gives the expected behaviour since "/lib/library.js" is declared in lib-bundle.js, which is what I was aiming to do.

dist-after.zip
This zip contains the bundles after my changes. lib-bundle.js is unchanged, but test-bundle.js just decided to import the files I was trying to mark as external in itself, suggesting that all my changes did was invalidate the external option.

So while I think there is a problem in this use case, my changes didn't fix it.

@dwelle
Copy link

dwelle commented Mar 28, 2017

There's no doubt there is a problem (as the numerous issues attest). What I'm afraid of that there's not any incentive to fix it (there were multiple PRs somewhat addressing windows backslash issues, some still open).

/cc @substack should we attempt to issue a PR to fix this at all?

@Shingyx Shingyx reopened this Apr 1, 2017
@Shingyx
Copy link
Contributor Author

Shingyx commented Apr 1, 2017

Fixed it properly this time by replacing all \ with / in relative paths. Now it works as expected.

Fun fact: my change decreased the number of failing tests on my machine from 61 to 53.

Again, Travis check is failing for the same reason as the master branch: ArrayBuffer is not defined in Node 0.10.x.

Use the same arguments as the function exported by cached-path-relative,
rather than calling .apply
@Shingyx
Copy link
Contributor Author

Shingyx commented Apr 22, 2017

Is there anything else I can do to help get this merged in?

@kaushikvijay
Copy link

I am also facing same issue. Will this get merged?

@Shingyx
Copy link
Contributor Author

Shingyx commented Oct 10, 2017

@kauvj I haven't heard anything, but I merged in the current master branch to get this to build.

@@ -118,14 +119,12 @@ Browserify.prototype.require = function (file, opts) {
var expose = opts.expose;
if (file === expose && /^[\.]/.test(expose)) {
expose = '/' + relativePath(basedir, expose);
expose = expose.replace(/\\/g, '/');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Couldn't you use your helper function from below here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure what you mean. My changes basically extended the existing relativePath method to replace backslashes with forward slashes, which means that adding the .replace(/\\/g, '/') after a relativePath call is no longer necessary.

I don't quite remember, but I think the problem was that .replace(/\\/g, '/') was missing after some of the other relativePath calls and so the path behaviour was inconsistent on Windows.

}
if (expose === undefined && this._options.exposeAll) {
expose = true;
}
if (expose === true) {
expose = '/' + relativePath(basedir, file);
expose = expose.replace(/\\/g, '/');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same as above

Copy link
Member

@goto-bus-stop goto-bus-stop left a comment

Choose a reason for hiding this comment

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

Thanks! this fixes some tests on windows too 🎉

@goto-bus-stop
Copy link
Member

Will merge this in #1819. Thanks!

@Shingyx
Copy link
Contributor Author

Shingyx commented Mar 23, 2018

Awesome. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants