import-js icon indicating copy to clipboard operation
import-js copied to clipboard

Confusing behaviour when having nested package.json

Open janpaul123 opened this issue 6 years ago • 2 comments

I struggled with setting up import-js for a repo that had (mistakenly) two package.json files. This was the directory structure:

- import.js
- package.json
- client/
  - package.json
  - components/
    - some_file.js

I was trying to figure out why some_file.js was not picking up the configuration. So I tried to set minimumVersion to something large, but then it errored out. So it looked like it was picking up the configuration. But then I tried to use other config options, but it wouldn't pick them up.

The reason for this is that in this case Configuration is initialised multiple times but with different workingDirectory params. The first time it is initialised correctly, but the second and third time the working directory was pointing at the client/ directory.

This was particularly confusing since there was no error message and it even seemed like configuration was somewhat picked up (because of the minimumVersion thing working properly).

janpaul123 avatar Jul 09 '18 22:07 janpaul123

The code that finds the project root is pretty naive, it simply looks for a package.json file: https://github.com/Galooshi/import-js/blob/01e65693e0fda2b2a9e1c774c805ffecc10c48a1/lib/findProjectRoot.js#L13

There have been an (unmerged) attempt at resolving this: https://github.com/Galooshi/import-js/pull/462. One possible (different) way of solving this could be to let the presence of an .importjs.js file have precedence. So instead of just stopping at the first package.json we find we could first scan for .importjs.js, then make a fallback scan for package.json.

trotzig avatar Jul 11 '18 19:07 trotzig

Yet another approach might be to put the highest precedence on .importjs-root file and keep the existing behavior the same. Very few people are using that file due to it being undocumented so we risk less breaking changes.

Could you let me know if this is still something that needs to be implemented? I could give it a go.

mikabytes avatar Feb 05 '24 07:02 mikabytes