Skip to content

Resolving manually provided config file #106

@ec-milan

Description

@ec-milan

Hi! This is a follow-up on previous PR/issue on resolving manually provided config file. Let's try to get to the bottom of this...

The problem I initially faced happens in the following scenario:

OS: Windows 10 Pro.
Shell: PowerShell 5.0.10586.122

Here is the relevant file structure on the filesystem:

/
|__ node_modules/
|   |
|   |__ ...
|
|__ testing/
|   |
|   |__ src/
|   |   |__ script1.js
|   |   |__ script2.js
|   |
|   |__ app/
|
|__ test_package.json
|__ gulpfile.js
|
|__ ...

Here is the content of simplified 'test_package.json' file:

{
  "name": "testcase",
  "version": "1.0.0",
  "devDependencies": {
    "gulp": "^3.9.1",
    "gulp-load-plugins": "latest",
    "gulp-concat": "^2.6.0"
  }
}

Here is the content of simplified 'testing/gulpfile.js' file:

var gulp = require("gulp");
var gulpLoadPlugins = require("gulp-load-plugins");

var loadPluginsOptions = {
    "config": "./test_package.json"
};

var plugins = gulpLoadPlugins(loadPluginsOptions);

gulp.task("default", function () {
    gulp.src("./testing/src/*.js")
        .pipe(plugins.concat("all.js"))
        .pipe(gulp.dest("./testing/app/"));
});

When I try to run the Gulp script from within the root directory I get the 'Cannot find module...' error, like so:

image

Changing the 'configObject' declaration solved the particular issue, but it was not a valid solution! I did not go deep enough into the problem... The case is: if our config file is named 'package.json' (which is the default value) but we do provide config path as './package.json' (which should be legit) - it resolves to 'package.json' of gulp-load-modules package:

image

and it consequently results in an error shown bellow:

image

I guess, this behavior is the result of search path starting from CWD. If our config is defined in a file named anything other than 'package.json', the error shown first above arises (in my case the config file was named 'test_package.json').

So, here is another simple test case (as noticed by @goyney) which highlights the real problem:

Here is the relevant file structure on the filesystem:

/
|__ node_modules/
|   |
|   |__ ...
|
|__ testing/
|   |
|   |__ src/
|   |   |__ script1.js
|   |   |__ script2.js
|   |
|   |__ app/
|   |
|   |__ gulpfile.js   // Changed from the first example!
|
|__ test_package.json
|
|__ ...

Content of 'test_package.json' file remains the same.

Here is the simplified content of 'testing/gulpfile.js' file:

var gulp = require("gulp");
var gulpLoadPlugins = require("gulp-load-plugins");

var loadPluginsOptions = {
    "config": "./../test_package.json"   // Changed from the first example!
};

var plugins = gulpLoadPlugins(loadPluginsOptions);

gulp.task("default", function () {
    gulp.src("./testing/src/*.js")
        .pipe(plugins.concat("all.js"))
        .pipe(gulp.dest("./testing/app/"));
});

When I try to run the Gulp script from within 'testing' directory I get the 'Cannot find module...' error, like so:

image

And when I change the 'configObject' declaration to call 'configureFn' I get the following error:

image

After putting some more effort to it I realized it was a problem of config file path, not the invoking function. So, here is proposed solution - adding the following block of code after options initialization:

// If config file path is provided as string value it should be normalized
// and converted to fixed path in order to resolve the file correctly on
// all platforms (Win, *nix, OSX, ...).
if (typeof config === 'string') {
    config = path.normalize(config);
    config = path.resolve(config);
}

This solves it for me - 'configObject' declaration stays the same ('require()' is used), but it resolves correctly for both './configfile' and './../configfile'.

Sidenote: It is hard for me to believe this problem did not surface earlier. I was testing this on Windows platform only, so it might be that on other platforms filepaths are resolved correctly even though no explicit 'path.normalize()' and 'path.resolve()' are utilized.

Can you please look into it...

P. S.
I am working on more than a few things at the moment, so I might not be able to respond promptly. :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions