Skip to content

Commit

Permalink
Make cache directory be relative to cwd (#582)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
spence-s and sindresorhus committed Aug 3, 2021
1 parent f038f9c commit 512291b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 6 additions & 6 deletions lib/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ resolveFrom.silent = (moduleId, fromDirectory) => {
const resolveLocalConfig = name => resolveModule(normalizePackageName(name, 'eslint-config'), import.meta.url);

const nodeVersion = process && process.version;
const cacheLocation = findCacheDir({name: CACHE_DIR_NAME}) || path.join(os.homedir() || os.tmpdir(), '.xo-cache/');
const cacheLocation = cwd => findCacheDir({name: CACHE_DIR_NAME, cwd}) || path.join(os.homedir() || os.tmpdir(), '.xo-cache/');

const DEFAULT_CONFIG = {
useEslintrc: false,
cache: true,
cacheLocation: path.join(cacheLocation, 'xo-cache.json'),
cacheLocation: path.join(cacheLocation(), 'xo-cache.json'),
globInputPaths: false,
baseConfig: {
extends: [
Expand Down Expand Up @@ -131,7 +131,7 @@ const mergeWithFileConfig = options => {
const tsConfigExplorer = cosmiconfigSync([], {searchPlaces: ['tsconfig.json'], loaders: {'.json': (_, content) => JSON5.parse(content)}});
const {config: tsConfig, filepath: tsConfigPath} = tsConfigExplorer.search(options.filePath) || {};

options.tsConfigPath = getTsConfigCachePath([options.filePath], options.tsConfigPath);
options.tsConfigPath = getTsConfigCachePath([options.filePath], options.tsConfigPath, options.cwd);
options.ts = true;
outputJsonSync(options.tsConfigPath, makeTSConfig(tsConfig, tsConfigPath, [options.filePath]));
}
Expand Down Expand Up @@ -187,7 +187,7 @@ const mergeWithFileConfigs = async (files, options, configFiles) => {
await Promise.all(Object.entries(groupBy(groups.filter(({options}) => Boolean(options.ts)), group => group.options.tsConfigPath || '')).map(
([tsConfigPath, groups]) => {
const files = groups.flatMap(group => group.files);
const cachePath = getTsConfigCachePath(files, tsConfigPath);
const cachePath = getTsConfigCachePath(files, tsConfigPath, options.cwd);

for (const group of groups) {
group.options.tsConfigPath = cachePath;
Expand All @@ -206,8 +206,8 @@ const findApplicableConfig = (file, configFiles) => configFiles.find(({filepath}
Generate a unique and consistent path for the temporary `tsconfig.json`.
Hashing based on https://github.com/eslint/eslint/blob/cf38d0d939b62f3670cdd59f0143fd896fccd771/lib/cli-engine/lint-result-cache.js#L30
*/
const getTsConfigCachePath = (files, tsConfigPath) => path.join(
cacheLocation,
const getTsConfigCachePath = (files, tsConfigPath, cwd) => path.join(
cacheLocation(cwd),
`tsconfig.${murmur(`${pkg.version}_${nodeVersion}_${stringify({files: files.sort(), tsConfigPath})}`).result().toString(36)}.json`,
);

Expand Down
4 changes: 4 additions & 0 deletions test/options-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ test('mergeWithFileConfig: typescript files', async t => {
semicolon: false,
ts: true,
};
const expectedConfigPath = new RegExp(`${slash(cwd)}/node_modules/.cache/xo-linter/tsconfig\\..*\\.json[\\/]?$`, 'u');
t.regex(slash(options.tsConfigPath), expectedConfigPath);
t.deepEqual(omit(options, 'tsConfigPath'), expected);
t.deepEqual(await readJson(options.tsConfigPath), {
extends: path.resolve(cwd, 'tsconfig.json'),
Expand All @@ -574,6 +576,8 @@ test('mergeWithFileConfig: tsx files', async t => {
semicolon: false,
ts: true,
};
const expectedConfigPath = new RegExp(`${slash(cwd)}/node_modules/.cache/xo-linter/tsconfig\\..*\\.json[\\/]?$`, 'u');
t.regex(slash(options.tsConfigPath), expectedConfigPath);
t.deepEqual(omit(options, 'tsConfigPath'), expected);
t.deepEqual(await readJson(options.tsConfigPath), {
extends: path.resolve(cwd, 'tsconfig.json'),
Expand Down

0 comments on commit 512291b

Please sign in to comment.