less / less/less.js

Find less file path match error in npm package

Open
#4,220 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
17k
Forks
3.4k
Avg merge
7h 42m
Merged PRs (30d)
26

Description

To reproduce:

The code structure is like this:

  • node_modules
    • lessPkg1@1.0.0
    • lessPkg2
      • node_modules
      • lessPkg1@2.0.0

my project depends on lessPkg1@1.0.0 and lessPkg2
lessPkg2 depends on lessPkg1@2.0.0

When I try to use webpack to build my project, I found that when the lessPkg2 looks for lessPkg1@2.0.0, it will match lessPkg1@1.0.0

Breakpoint debugging webpack, I found the loadFile function in this file(node_modules/less/lib/less-node/file-manager.js),It will only match two paths:

  • currentDirectory: The path of lessPkg2
  • '·': The node_modules directory of my project

As a result, lessPkg2 cannot find lessPkg1@2.0.0 under its own node_modules, but can only find lessPkg1@1.0.0 under node_modules of my project。

Expected behavior:
I hope that when a less file in npm package is looking for a less file in another npm package, it will first search in its own node_modules directory. If it cannot be found,it will search in the parent directory until it matches up to the root path of the project.

When I debug vite@4.2.1 source code, I found the same error, But it has been fixed in this issue

When vite is looking for module dependencies, it will recurse upwards to the node_modules directory under the directory, see the code here

I think there should be a consistent ability here.

Environment information:

  • less version: v4.2.0
  • nodejs version: v16.14.2
  • operating system: macos

other
English is not good, use translation software to write, forgive me.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the nested npm dependency case with lessPkg1 at two versions and inspect node_modules/less/lib/less-node/file-manager.js, especially loadFile. Compare its lookup behavior with the linked Vite package-resolution implementation. Done means a package first resolves Less files from its own node_modules, then walks parent node_modules directories without selecting the wrong version.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, vite, webpack
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.