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

Nested @import cause undefined variable or something like that #2672

Closed
iby opened this issue Aug 21, 2015 · 4 comments
Closed

Nested @import cause undefined variable or something like that #2672

iby opened this issue Aug 21, 2015 · 4 comments

Comments

@iby
Copy link

iby commented Aug 21, 2015

Trying to separate style into more manageable blocks, and hitting a rather unexpected "undefined variable" error. I have four files: color variables, component style, and two theme styles. Component imports theme styles, theme styles import color variables.

// colors.less

@black: #000000;
@red: #000000;

// button.less

button {
    //
    @import "fooTheme";
    @import "barTheme";
}

// fooTheme.less

&.theme-foo {
    @import (reference) "colors";
    color: @red;
}

// barTheme.less

&.theme-bar {
    @import (reference) "colors";
    color: @black;
}

When compiling all this I get an exception 'variable @black is undefined in file …/barTheme.less'. Bar gets imported after foo, meaning the exact same imports work in foo, but when it gets to bar, it fails. If I leave only one theme import, it works as expected.

It almost seems like less checks if that file is already imported and ignores it, but when it gets to pulling variable values, they are not really in that scope, causing it to fail. Am I doing something silly or is there a bug?

@matthew-dean
Copy link
Member

By default, imports only import once. I haven't tested your code, but you probably will need to do something like:

@import (reference, multiple) "colors";

That should work, according to docs.

@iby
Copy link
Author

iby commented Aug 21, 2015

Thanks Matt, I've figured this out earlier in the day, but this behaviour is confusing at very least.

Auto-applied once makes sense only in the same context, but in different contexts like in my example it makes no good. Especially with reference already in there.

I'm sure there's a reason / use case for such behaviour, could you explain to the stupid why and when this is useful in different contexts?

On 21 Aug 2015, at 22:08, Matthew Dean notifications@github.com wrote:

By default, imports only import once. I haven't tested your code, but you probably will need to do something like:

@import (reference, multiple) "colors";

That should work, according to docs.


Reply to this email directly or view it on GitHub.

@matthew-dean
Copy link
Member

@ianbytchek Less used to import multiples by default, but a common complaint was that this sometimes caused unexpected behavior. (See #622.)

Auto-applied once makes sense only in the same context, but in different contexts like in my example it makes no good. Especially with reference already in there.

You are probably correct.

I'm sure there's a reason / use case for such behaviour, could you explain to the stupid why and when this is useful in different contexts?

It's more the reverse. It wasn't designed that way because of a specific use case. It wasn't designed the way you're suggesting because there WASN'T a specific use case suggested in order to add "different behavior per context" or "different behavior for reference".

Basically, imports simply evaluate once by default, and since you can specify when you want to evaluate more than once, it keeps the behavior consistent (albeit perhaps not useful in your case).

There are many other ways of "importing" variables into that context as well, so I'm not sure another feature is needed.

@iby
Copy link
Author

iby commented Aug 25, 2015

Thanks @matthew-dean, makes sense overall.

@iby iby closed this as completed Aug 25, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants