Skip to content

Commit

Permalink
Fix tsconfig resolution quirks (#683)
Browse files Browse the repository at this point in the history
  • Loading branch information
spence-s committed Aug 26, 2022
1 parent c8ec522 commit 4cf8b05
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,5 +4,7 @@ yarn.lock
test/fixtures/project/node_modules/.cache
!test/fixtures/typescript/extends-module/node_modules
test/fixtures/typescript/extends-module/node_modules/.cache
!test/fixtures/typescript/extends-tsconfig-bases/node_modules
test/fixtures/typescript/extends-tsconfig-bases/node_modules/.cache
.nyc_output
coverage
21 changes: 17 additions & 4 deletions lib/options-manager.js
Expand Up @@ -28,7 +28,7 @@ import {
CACHE_DIR_NAME,
} from './constants.js';

const {__dirname, readJson, require} = createEsmUtils(import.meta);
const {__dirname, require} = createEsmUtils(import.meta);
const {normalizePackageName} = Legacy.naming;
const resolveModule = Legacy.ModuleResolver.resolve;

Expand Down Expand Up @@ -162,7 +162,7 @@ const handleTSConfig = async options => {

if (tsConfigProjectPath) {
options.tsConfigPath = path.resolve(options.cwd, tsConfigProjectPath);
options.tsConfig = await readJson(options.tsConfigPath);
options.tsConfig = JSON5.parse(await fs.readFile(options.tsConfigPath));
} else {
const tsConfigExplorer = cosmiconfig([], {
searchPlaces: ['tsconfig.json'],
Expand Down Expand Up @@ -644,9 +644,22 @@ async function recursiveBuildTsConfig(tsConfig, tsConfigPath) {
}

// If any of the following are missing, then we need to look up the base config as it could apply
// Use node resolution
const require = createRequire(tsConfigPath);
const basePath = require.resolve(tsConfig.extends);

let basePath;
try {
basePath = require.resolve(tsConfig.extends);
} catch (error) {
// Tsconfig resolution is odd, It allows behavior that is not exactly like node resolution
// therefore we attempt to smooth this out here with this hack
try {
basePath = require.resolve(path.join(tsConfig.extends, 'tsconfig.json'));
} catch {
// Throw the orginal resolution error to let the user know their extends block is invalid
throw error;
}
}

const baseTsConfig = JSON5.parse(await fs.readFile(basePath));

delete tsConfig.extends;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions test/fixtures/typescript/extends-tsconfig-bases/package.json
@@ -0,0 +1,3 @@
{
"xo": {}
}
3 changes: 3 additions & 0 deletions test/fixtures/typescript/extends-tsconfig-bases/tsconfig.json
@@ -0,0 +1,3 @@
{
"extends": "@tsconfig/node16"
}
9 changes: 9 additions & 0 deletions test/options-manager.js
Expand Up @@ -680,6 +680,15 @@ test('mergeWithFileConfig: tsconfig can properly extend configs in node_modules'
t.is(options.tsConfigPath, expectedConfigPath);
});

test('mergeWithFileConfig: tsconfig can properly extend tsconfig base node_modules', async t => {
const cwd = path.resolve('fixtures', 'typescript', 'extends-tsconfig-bases');
const expectedConfigPath = path.join(cwd, 'tsconfig.json');
const filePath = path.resolve(cwd, 'does-not-matter.ts');
await t.notThrowsAsync(manager.mergeWithFileConfig({cwd, filePath}));
const {options} = await manager.mergeWithFileConfig({cwd, filePath});
t.is(options.tsConfigPath, expectedConfigPath);
});

test('applyOverrides', t => {
t.deepEqual(
manager.applyOverrides(
Expand Down

0 comments on commit 4cf8b05

Please sign in to comment.