microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99.86k stars 12.36k forks source link

tsconfig extends with bare path ".." works when it should not #51782

Open IlyaSemenov opened 1 year ago

IlyaSemenov commented 1 year ago

Bug Report

🔎 Search Terms

tsconfig extends parent

🕗 Version & Regression Information

⏯ Playground Link

Playground links with relevant code: A. https://stackblitz.com/edit/node-adwy3r B. https://stackblitz.com/edit/node-aiwgcb

💻 Code

Playground A

tsconfig.json:

{}

foo/tsconfig.json:

{"extends": ".."}

Playground B

tsconfig.json:

{"extends": "./foo"}

foo/tsconfig.json:

{}

🙁 Actual behavior

In playground A, running tsc --showConfig in directory foo works.

In playground B, running tsc --showConfig in project root fails:

tsconfig.json:2:14 - error TS6053: File './foo' not found.

2   "extends": "./foo"
               ~~~~~~~

🙂 Expected behavior

Both playgrounds are kind of symmetrical. I expect either both A and B to work, or both of them to fail.

I expect the "extends" specification to be either followed: "The value of extends is a string which contains a path to another configuration file to inherit from. The path may use Node.js style resolution.", or adjusted to reflect the actual behaviour.

I expected the Typescript 3.2 changelog to be either followed: _"TypeScript 3.2 now resolves tsconfig.jsons from node_modules. When using a bare path for the extends field in tsconfig.json, TypeScript will dive into node_modules packages for us."_ or adjusted to reflect the actual behaviour.

Neither of .. and ./foo are located inside node_modules. Both of them do have package.json and node_modules. I don't see what causes the different behaviour.

rotu commented 3 months ago

This is surely a bug:

https://github.com/microsoft/TypeScript/blob/79a851426c514a12a75b342e8dd2460ee6615f73/src/compiler/commandLineParser.ts#L3502-L3512

There is special logic to treat this as a filesystem path when it starts with ../ or ./, but not when the path is exactly ...

It's also odd that, if given a path-like string, the logic appends .json but if given a module-like string, it appends /tsconfig.json.