less / less.js

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

import fails with unhelpful error message #3621

Closed gomedia-adi closed 3 years ago

gomedia-adi commented 3 years ago

I'm using the CodeKit app on MacOS, which in turn is using LESS v4.1.1

The following code:

@import 'less';

fails, when the file less.less doesn't exist, with:

Compiling failed with this error: ParseError: Unrecognised input in /Applications/CodeKit.app/Contents/Resources/engines/node_modules/less/index.js on line 1, column 16: 1 module.exports = require('./lib/less-node').default; 2

However, this code:

@import 'less.less';

fails (when the file less.less doesn't exist) with:

Compiling failed with this error: FileError: 'less.less' wasn't found. Tried - ...blah blah... on line 1, column 1: 1 @import 'less.less'; 2

The developer of CodeKit assures me that these errors are coming from the LESS compiler.

The first error message is not very helpful. The second is. Not a big deal in the grand scheme of things but I thought I'd mention it anyway!

iChenLei commented 3 years ago

This is a very funny case !

@import "less";

If your ImportNode's path is not endwith less(color.less) or relative file path(../bootstrap), it means you want import a npm less lib. In current case, you want import less npm package, but you know less is a css preprocessor and not a less lib which supoort a style port (in package.json). So you will encount a ParseError !

Why?

Because the less's file-manager lookup read file logic !

less.js/packages/less/src/less-node/file-manager.js

https://github.com/less/less.js/blob/dc3a3105300930e2df14062fcf3de97d43db61b0/packages/less/src/less-node/file-manager.js#L62-L137

When less try to lookup file, it will try to read file which path is:

  1. ./less.less
  2. npm://less <- You will found less self, it exist but not a valid less file nor less npm library.
  3. npm://less.less

So you will get a ParseError rather than FileError.

Conclusion

It's expected behavior, I think we don't need change anything. /cc @matthew-dean