Less.js: Imports are not updated when recompiling via the same (?) less object

Created on 7 Mar 2018  ·  19Comments  ·  Source: less/less.js

bug report
I write a compile tool for my project, it depend less.

my project style folder and files likes

- styles
   _utils.less
   index.less

and index.less content is

@import "./utils"

I use less api to compile the less files like

let less=require('less');
//....
less.render(indexContent,options,(err,result)=>{
       console.log(result.css)
});

when i change _utils.less content , the above code will automatically execute

but since 3.0.x , when i change _utils.less content ,the result.css is the first time result, whatever change how many times.

but 2.7.x is ok, when i change _utils.less content ,the result.css also changed.

bug high priority research needed up-for-grabs

Most helpful comment

In Node environment, manual clear less compile cached file:

const less = require('less');
const fileManagers = less.environment && less.environment.fileManagers || [];
fileManagers.forEach(fileManager => {
     if (fileManager.contents) {
               fileManager.contents = {};
     }
});

All 19 comments

Sounds similar to https://github.com/stevelacy/gulp-less/issues/283.
I guess in a these "auto-watch" cases it's the same Less object used to compile each update, right? Then I guess it's something with not reseting some imports cache between different render calls or somewhat like that.

@seven-phases-max yes,use the same Less object to compile each update.
i am not use gulp-less tool, i use less api directly .
it seems less cache the result and not update correctly

So the quick workaround (until the bug is fied) in your case wil be in:

require('less').render(indexContent,options,(err,result)=>{
       console.log(result.css)
});

Yes, it seems the issue lies in this commit where the calltree is manipulated based on arguments https://github.com/less/less.js/commit/a48c24c4dd3c13e00a20ece80323768496a96b36#diff-a6b31db4e862654ae597161e441689bb

it seems the issue lies in this commit ...

Hmm, honestly there I can't see anything that could affect imports...

In Node environment, manual clear less compile cached file:

const less = require('less');
const fileManagers = less.environment && less.environment.fileManagers || [];
fileManagers.forEach(fileManager => {
     if (fileManager.contents) {
               fileManager.contents = {};
     }
});

Thanks @tiodot it seems to be working so far... But hope we'll get a "cleaner" way soon!

It might have something to do with these lines added for 3.0. https://github.com/less/less.js/blob/master/lib/less/tree/mixin-definition.js#L9

Try removing the specific if/else to just the else statement and see if the problem still happens. If it doesn't, this optimization may need to be removed. However, what would be more ideal is clearing the file manager or any internal cache per new parse/render call.

Just checking if there are any updates for this issue?
gulp-less is getting quite a few developers hitting this.

Just gave another try a few hours ago and it seemed to be working fine (without patch). Anyone else?

  "dependencies": {
    "browser-sync": "^2.24.4",
    "gulp": "^4.0.0",
    "gulp-autoprefixer": "^5.0.0",
    "gulp-clean-css": "^3.9.4",
    "gulp-hb": "^7.0.1",
    "gulp-less": "^4.0.0",
    "gulp-load-plugins": "^1.5.0",
    "gulp-rename": "^1.2.2"
  },
  "optionalDependencies": {
    "less": "^3.0.4"
  }

I have the same issue descibed by @xinglie upgrading from [email protected].

The workaround provided by @tiodot did not seem to work for me (tried: [email protected] and [email protected]).
Also using the require('less').render() workaround provided by @seven-phases-max didn't do the trick :(
Are there maybe more suggestions? :)

Also affected with this :(

Hi, though I am not sure what I did differently the workaround @tiodot provided DOES work for me now!
I tried it again and I must have made a mistake somewhere in the code.
(using [email protected]).
Thanks @tiodot !

Thanks @tiodot

@tiodot @hawkerboy7 @TigersWay Does this branch resolve the issue? If so, I'll make a PR - please check this out - https://github.com/matthew-dean/less.js/commits/cache-modified

Just did checks in 2 clearly different projects, commenting out @tiodot great patch inside package.json, and it seems to be working perfectly!!

  "optionalDependencies": {
    "less": "github:matthew-dean/less.js#cache-modified"
  },

@matthew-dean works for me!

@matthew-dean Yes it has resolved the issue! PR to the rescue!! :)

@tiodot @hawkerboy7 @TigersWay Cool, the nice thing is, you should get slightly faster Less compiling with a warm cache (a 3.x feature), so it's preferable to have it fixed; otherwise I would have suggested taking it out. Will do a PR.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukeapage picture lukeapage  ·  37Comments

lukeapage picture lukeapage  ·  102Comments

zigomir picture zigomir  ·  41Comments

Soviut picture Soviut  ·  64Comments

influsion picture influsion  ·  50Comments