xojs/xo

Error: Cannot find module '<tsconfig from node_modules>'

jonahsnider opened this issue · 2 comments

Bug description

When I run xo I get this error:

Error: Cannot find module '@namespace/package'
Require stack:
- /Users/jonah/programming/xo-bug/tsconfig.json
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:956:15)
    at Function.resolve (node:internal/modules/cjs/helpers:108:19)
    at recursiveBuildTsConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:649:27)
    at handleTSConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:180:28)
    at async mergeWithFileConfig (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:139:13)
    at async parseOptions (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:586:51)
    at async Promise.all (index 0)
    at async getOptionGroups (file:///Users/jonah/programming/xo-bug/node_modules/xo/lib/options-manager.js:602:21)
    at async Object.lintFiles (file:///Users/jonah/programming/xo-bug/node_modules/xo/index.js:78:17)
    at async file:///Users/jonah/programming/xo-bug/node_modules/xo/cli.js:211:18

This is my tsconfig.json:

{
	"compilerOptions": {
		"declaration": true,
		"inlineSourceMap": true,
		"moduleResolution": "node",
		"outDir": "dist"
	},
	"extends": "@tsconfig/node16",
	"include": ["src"]
}

Bug reproduction

Here's a fully reproducible project:
xo-bug.zip

  1. Download & extract
  2. yarn
  3. yarn test
  4. Error occurs

Fix

I believe this is a bug introduced in #677.

The fix is probably replacing this:

xo/lib/options-manager.js

Lines 642 to 644 in c8ec522

if (!tsConfig.extends || (typeof tsConfig.extends === 'string' && tsConfig.extends.includes('node_modules'))) {
return tsConfig;
}

with something like this:

 if (!tsConfig.extends || (typeof tsConfig.extends === 'string' && tsConfig.extends.includes('node_modules') || tsConfig.startsWith('@'))) { 
 	return tsConfig; 
 } 

@jonahsnider thanks for the report and the repro!

I think we've nearly got the tsconfig changes sorted out now, hate that it introduced so many regressions...

Unfortunately @tsconfig doesn't add a main field to the package.json which is why require.resolve can't find the config, but ultimately we should support whatever TS does as close as possible...

I don't think the check for startsWith('@') will fix this entirely, since it would be possible to install a tsconfig from npm that isn't prefixed with @ that also doesn't have the main field of package.json listed as the tsconfig.

I will make a PR for a different patch that will support this syntax and resolution logic to assume that tsconfig.json is always the name of the file it is looking for.