less / less.js

Less. The dynamic stylesheet language.
http://lesscss.org
Apache License 2.0
17.02k stars 3.41k forks source link

Error thrown on compiling using a sourcemap and autoprefix in Node v14 #3646

Closed coreyoss closed 2 years ago

coreyoss commented 3 years ago

Hello,

I recently upgraded to Node v4.17.5 and it seems a newly added exception there is breaking my Less build.

I'm using less v4.1.1 and less-plugin-autoprefix v2.0.0. To recreate the issue I've -

  1. Installed the above packages globally
  2. Created a less file (test.less) with the following content -
    p {
    color: blue;
    }
  3. On the command line, run - lessc test.less test.css --source-map=test.css.map --autoprefix='defaults'

On running this, I get the errors -

internal/fs/utils.js:793
  throw new ERR_INVALID_ARG_TYPE(
  ^

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of SourceMapGenerator
    at writeFile (fs.js:1487:5)
    at go$writeFile (/usr/lib/node_modules/less/node_modules/graceful-fs/graceful-fs.js:138:14)
    at Object.writeFile (/usr/lib/node_modules/less/node_modules/graceful-fs/graceful-fs.js:135:12)
    at writeSourceMap (/usr/lib/node_modules/less/bin/lessc:183:10)
    at writeSourceMapIfNeeded (/usr/lib/node_modules/less/bin/lessc:207:7)
    at /usr/lib/node_modules/less/bin/lessc:299:11
    at /usr/lib/node_modules/less/bin/lessc:237:11
    at /usr/lib/node_modules/less/node_modules/graceful-fs/graceful-fs.js:143:16
    at /usr/lib/node_modules/less/node_modules/graceful-fs/graceful-fs.js:61:14
    at FSReqCallback.oncomplete (fs.js:180:23) {
  code: 'ERR_INVALID_ARG_TYPE'
}

Interestingly, I don't get the error if I exclude the sourcemap or the autoprefix. Eg. lessc test.less test.css --source-map=test.css.map works and so does lessc test.less test.css --autoprefix='defaults'.

I downgraded to Node v12.22.5 and this works fine.

Any help in resolving this would be appreciated!

Thanks, Corey

iChenLei commented 3 years ago

Thanks for bug report.

iChenLei commented 3 years ago

https://nodejs.org/dist/latest-v14.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback

Version Changes
v14.0.0 The data parameter won't coerce unsupported input to strings anymore.

When you run command line ⬇️

lessc test.less test.css --source-map=test.css.map --autoprefix='defaults'

https://github.com/less/less.js/blob/dc3a3105300930e2df14062fcf3de97d43db61b0/packages/less/bin/lessc#L180-L185

The second parameter is a instance of SourceMapGenerator and not string, so crashed when you use node v14.

bf362c528cbd254ba39d5a6a7c5209b5d4b78388

only add source-map args

lessc test.less test.css --source-map=test.css.map

258a158c898ed9d60c3f7269139d983ec4d6d8a6

Why node v12 works fine ?

Node 12 fs module's wirteFile will auto coerce unsupported input to strings and SourceMapGenerator self has a toString func, so work as expected.

So the reason is clear. @coreyoss Are you interested in this bugfix ? It'a very easy bug to fix, only need add toString for source-map generation, of course unit test is required ! source-map and plugin test code is here: https://github.com/less/less.js/blob/3173fddd64dbbdca3a9615289573da95bf4581f5/packages/less/Gruntfile.js#L259-L276

Good first issue for you, thanks ! PR welcome, I will help you to be less contributor !

cc @matthew-dean

gzb1128 commented 3 years ago

@iChenLei hiii, i am new to github, i am interested in this issue. when i finish, i will open a PR, including bug fix and unit tests mentioned above or more. is it enough? thx

iChenLei commented 3 years ago

@gzb1128 Thank you.