Skip to content

Commit

Permalink
Fix: Do not cache .eslintrc.js
Browse files Browse the repository at this point in the history
Read `.eslintrc.js` on every config load by not using `require`
Related: eslint#4610
  • Loading branch information
MethodGrab committed Jan 25, 2016
1 parent 8c870e4 commit 555bddd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ var debug = require("debug"),
validator = require("./config-validator"),
Plugins = require("./plugins"),
stripComments = require("strip-json-comments"),
isAbsolutePath = require("path-is-absolute");
isAbsolutePath = require("path-is-absolute"),
requireUncached = require("require-uncached");

//------------------------------------------------------------------------------
// Private
Expand Down Expand Up @@ -131,7 +132,7 @@ function loadLegacyConfigFile(filePath) {
function loadJSConfigFile(filePath) {
debug("Loading JS config file: " + filePath);
try {
return require(filePath);
return requireUncached(filePath);
} catch (e) {
debug("Error reading JavaScript file: " + filePath);
e.message = "Cannot read config file: " + filePath + "\nError: " + e.message;
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"optionator": "^0.8.1",
"path-is-absolute": "^1.0.0",
"path-is-inside": "^1.0.1",
"require-uncached": "^1.0.2",
"shelljs": "^0.5.3",
"strip-json-comments": "~1.0.1",
"text-table": "~0.2.0",
Expand Down
42 changes: 42 additions & 0 deletions tests/lib/config/config-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ function writeTempConfigFile(config, filename, existingTmpDir) {
return tmpFilePath;
}

/**
* Helper function to write JS configs to temp file.
* @param {object} config Config to write out to temp file.
* @param {string} filename Name of file to write in temp dir.
* @param {string} existingTmpDir Optional dir path if temp file exists.
* @returns {string} Full path to the temp file.
* @private
*/
function writeTempJsConfigFile(config, filename, existingTmpDir) {
var tmpFileDir = existingTmpDir || tmp.dirSync({prefix: "eslint-tests-"}).name,
tmpFilePath = path.join(tmpFileDir, filename),
tmpFileContents = 'module.exports = ' + JSON.stringify(config);
fs.writeFileSync(tmpFilePath, tmpFileContents);
return tmpFilePath;
}

//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -349,6 +365,32 @@ describe("ConfigFile", function() {
assert.deepEqual(config, updatedConfig.eslintConfig);
});

it("should load fresh information from a .eslintrc.js file", function() {
var initialConfig = {
parserOptions: {},
env: {},
globals: {},
rules: {
quotes: [2, "double"]
}
},
updatedConfig = {
parserOptions: {},
env: {},
globals: {},
rules: {
quotes: 0
}
},
tmpFilename = ".eslintrc.js",
tmpFilePath = writeTempJsConfigFile(initialConfig, tmpFilename),
config = ConfigFile.load(tmpFilePath);
assert.deepEqual(config, initialConfig);
writeTempJsConfigFile(updatedConfig, tmpFilename, path.dirname(tmpFilePath));
config = ConfigFile.load(tmpFilePath);
assert.deepEqual(config, updatedConfig);
});

it("should load information from a YAML file", function() {
var config = ConfigFile.load(getFixturePath("yaml/.eslintrc.yaml"));
assert.deepEqual(config, {
Expand Down

0 comments on commit 555bddd

Please sign in to comment.