Skip to content
This repository has been archived by the owner on Sep 6, 2021. It is now read-only.

fix loading of less imports on win, linux and mac #7522

Merged
merged 1 commit into from
Apr 17, 2014

Conversation

zaggino
Copy link
Contributor

@zaggino zaggino commented Apr 15, 2014

maybe the if should be more sophisticated, but this fixes #7513 tested on both win & linux ... need to test on mac too to be 100% sure

if (PathUtils.isAbsoluteUrl(url)) {
// PathUtils.isAbsoluteUrl is required for windows platform
// FileSystem.isAbsolutePath is required for linux/mac platform
if (PathUtils.isAbsoluteUrl(url) || FileSystem.isAbsolutePath(url)) {
Copy link
Member

Choose a reason for hiding this comment

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

I think the issue here is that, somehow, the path you're getting passed is sometimes a URL and sometimes an OS path. Each of these APIs works correctly across all platforms when given the type of string it's designed to be used for, but since the type of string varies by platform we need to call both. Might want to tweak the comment a bit -- as is it sort of suggests one or both of these APIs is unreliable.

@zaggino
Copy link
Contributor Author

zaggino commented Apr 16, 2014

fixed as suggested @peterflynn
i still need to test this at home on mac to be 100% sure

*/
function isAbsolutePathOrUrl(str) {
return PathUtils.isAbsoluteUrl(str) || FileSystem.isAbsolutePath(str);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@zaggino I don't like the idea of this solution because I think it sets a bad precedent -- pretty soon we'll have to do this everywhere.

The FileSystem.isAbsolutePath() check does not seem to be safe. All it does is check to see if first char is a forward slash (fullPath[0] === "/") which will cause problems if someone passes in a web site root-relative path of something like "/styles/my.less".

What code is failing? Maybe that code should convert the local file path to a url instead.

cc @peterflynn

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, you're right this is a bad solution ... here's the problematic code (line 140):

function getModuleUrl(module, path) {
        var url = encodeURI(getModulePath(module, path));

        // On Windows, $.get() fails if the url is a full pathname. To work around this,
        // prepend "file:///". On the Mac, $.get() works fine if the url is a full pathname,
        // but *doesn't* work if it is prepended with "file://". Go figure.
        // However, the prefix "file://localhost" does work.
        if (brackets.platform === "win" && url.indexOf(":") !== -1) {
            url = "file:///" + url;
        }

        return url;
    }
``

Copy link
Member

Choose a reason for hiding this comment

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

@redmunds The FileSystem APIs all only work with paths -- if someone passes in a URL instead, they will all fail. I don't think we should change this one API to accept URLs when none of the other related APIs do.

Where is the path originating from in this case? It seems like it's part of the Require module object. If Require has a feature/bug where it sometimes passes file://... URLs and sometimes passes absolute Linux/Mac system paths, then it seems appropriate to me to place a workaround specific to that issue localized in this one bit of code that actually interacts with those Require APIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This results in url being different for win vs others so different check is also needed depending on the platform...

Copy link
Member

Choose a reason for hiding this comment

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

Oops, @zaggino sorry, your replies didn't show up until I reloaded the page. So tt seems like our own getModuleUrl() fully explains the funkiness both cases -- not a Require bug.

In the loadFile() case we could just do the absolute check before converting getModulePath() to a URL... but in the loadStyleSheet() -> parseLessCode() case we're going off of the URL fed into $.get() (this.url), which has to have the funkiness... so it does seem like the cleanest solution overall is to just have an absolute-checking utility that handles both cases...

Copy link
Contributor

Choose a reason for hiding this comment

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

@peterflynn

I don't think we should change this one API...

I wasn't suggesting that API should be changed, just not used here.

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 changed the check a bit according to getModuleUrl() and fixed the comment, should be much more clear now.

@redmunds
Copy link
Contributor

Done with review. Looks good -- just need to update comments.

@zaggino
Copy link
Contributor Author

zaggino commented Apr 17, 2014

Updated + Squashed old commits

@redmunds
Copy link
Contributor

Looks good. Merging.

redmunds added a commit that referenced this pull request Apr 17, 2014
fix loading of less imports on win, linux and mac
@redmunds redmunds merged commit 8ab46c2 into master Apr 17, 2014
@redmunds redmunds deleted the zaggino/fix-less-loading branch April 17, 2014 16:41
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

less imports loading is still broken on linux and mac
3 participants