license-checker icon indicating copy to clipboard operation
license-checker copied to clipboard

Bug: license-checker searches LICENSE file in wrong directory

Open rogierschouten opened this issue 7 years ago • 4 comments

I have a module which uses license-checker to check other modules. It has a directory structure like this:

/node_modules
/src
/src/lib
/src/test
/out/lib
/out/test
/out/test/test-module
/out/test/test-module/node_modules

There is a unittest which creates a fake test module inside the out/test/test-module/ directory, installs a dependency, and then runs the license checker functionality in it.

The funny thing is this: when you start the license checker in the /out/test/test-module/ directory, it will pick up the package.json from there. However, it will try and fetch the dependencies from the top-level node_modules directory (which is what CWD points to at that time).

	licenseChecker.init({
		start: "C:/my-module/out/test/test-module",
		unknown: true,
		production: true, 
		relativeLicensePath: true
	}, (err, ret) => {
		...
	})       

This will eventually give the error:

Uncaught Error: ENOENT: no such file or directory, open 'C:\my-module\node_modules\kitchen\LICENSE'

Here 'kitchen' is a dependency that is present in the test-module/node_modules directory and referenced in the test-module/package.json file.

So it seems that the license checker sometimes looks in CWD instead of using the start directory

rogierschouten avatar Jun 29 '17 12:06 rogierschouten

Here's some code that reproduces the issue.

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const licenseChecker = require("license-checker");
const path = require("path");
const start = "C:/my-module/out/test/test-module/";

// UN-COMMENT THIS STATEMENT TO WORK AROUND THE BUG
// process.chdir(start);



licenseChecker.init({
    start,
    unknown: true,
    production: true,
    relativeLicensePath: true
}, (err, ret) => {
    if (err) {
        process.stderr.write(err.message);
        process.exit(1);
    }
    else {
        process.stdout.write(JSON.stringify(ret, undefined, 2));
        process.exit(0);
    }
});

Package.json file for in the /out/test/test-module/ directory:

{
  "name": "test-module",
  "version": "1.0.0",
  "dependencies": {
    "kitchen": "0.0.3"
  }
}

rogierschouten avatar Jun 29 '17 12:06 rogierschouten

That's odd, the only place that it sets the start is like this:

parsed.start = parsed.start || process.cwd()

So if start is passed, it should work off that dir..

Can you run your command with the DEBUG=* environment variable and see what it says? There should be a line like: scanning <start variable> That might show where and why it's moving around.

davglass avatar Jun 29 '17 13:06 davglass

I suspect that your start variable is set correctly but that you use another mechanism like require.resolve which goes from a source file location or cwd.

On June 29, 2017 3:56:41 PM GMT+02:00, Dav Glass [email protected] wrote:

That's odd, the only place that it sets the start is like this:

parsed.start = parsed.start || process.cwd()

So if start is passed, it should work off that dir..

Can you run your command with the DEBUG=* environment variable and see what it says? There should be a line like: scanning <start variable> That might show where and why it's moving around.

-- You are receiving this because you authored the thread. Reply to this email directly or view it on GitHub: https://github.com/davglass/license-checker/issues/104#issuecomment-311974228

rogierschouten avatar Jun 29 '17 16:06 rogierschouten

Looking through the code, you use the read-installed module which defaults to searching dependencies in parent directories. Looks like the culprit may be in there

rogierschouten avatar Jun 29 '17 17:06 rogierschouten