You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Set TypeScript compiler's base/working directory #25430
It looks like TypeScript compiler resolves files relative to the location of the tsconfig.json file (however, I couldn't find anything about paths resolution in the official documentation).
Is there a way to specify an alternative base/working directory, which will be used for relative paths resolution?
I want to use a generic tsconfig.json file to compile multiple projects in various directories (one at a time).
P/S: I've created a question on StackOverflow first, but haven't received any attention there, so I've decided to ask here directly.
But, according to the docs, the rootDir option affects only the output directory structure and the baseUrl option is for non-relative modules (like node_modules I guess?).
I will try to explain better. I have multiple projects with very similar directory structure and compilation requirements. I would like to define compilation options (including relative paths to source files from the root of the project directory) in a single place. Therefore I have a single tsconfig.json file, which I want to use to compile multiple projects. However, the compiler is trying to resolve the source files, specified in the config, not from the project root directory, but from the directory, where this generic tsconfig.json file is located.
Consider, I have the following generic tsconfig.json file in ~/common/tsconfig.json:
And I have multiple projects like ~/project1, ~/project2, etc. I want to run the compiler like this:
tsc --project ~/common/tsconfig.json --projectRoot ~/project1 and I want it to start compilation using files, located in ~/project1/src/**/*.ts and not in ~/common/src/**/*.ts.
Also, I don't want to use extends functionality of the tsconfig.json, because I want projects to be agnostic from the location of the generic config.
I hope it makes sense.
Reacted by Daniel Playfair Cal, Joe Pea, anand kumar, Chihab Otmani, Robert Benjamin, Naoko Nishimura, Federico Biccheddu, Kevin Montag, Lisiur Day, Gwenaël Hagenmuller and 2 more
Reacted by Andrew Lisowski, Mirosław Filip, Joe Pea and Jakub Zak
Thanks. I would love to see this implemented. Right now I have to manually use gulp-typescript just in order to specify a list of files to compile. It would be nice to invoke tsc directly.
I have the same use case. My team is currently building out a component library. The repo is a monorepo and each component is written in typescript. Currently I have to have a tsconfig.json in each package that looks like this
This is a lot of duplication. I need to add another tsconfig for build related things. To do this in my current setup I would have to double the amount of tiny tsconfigs in the project.
I've been playing around with moving the tsconfig to our scripts package but can't get it to work because file resolution is relative to the tsconfig. If there was some way to set the --projectRoot from the cli I could get rid of all of these tiny files. It is important that i can set it from the CLI too. A tsconfig option would not suit my use case.
Reacted by Slava, Kevin Johnson, Tim Cheung, Lisa Cook, Daniel Playfair Cal, Peter Hagen and Evan Henley
This is a big issue when using "extends": "path/to/tsconfig.json". It means the extending config can't use includes, excludes, outDir, rootDir, or any option that includes a path as the path to these files are relative to the tsconfig that is extending, instead of being relative to the tsconfig that is extended, which makes a lot more sense.
Reacted by Daniel Montesinos, Raniz Daniel Raneland, Makary Hryshkin, Bohdan Shulha, ZhetoN, Emerson Havener, Nathan Jhaveri, Jagadish K. and Evin Owen
Very surprising to not be able to compile just the src directory for exemple. I have some utils directories in my projects which I don't need to be compiled. Additionally I would like to have the src and dist dir at the same level. Looks like I need babel to do that or did I misunderstood something ?
However this ../../../.. workaround relies on two assumptions:
The @microsoft/rush-stack-compiler-3.4 package is a direct dependency of the project being built; AND
The package manager installs it in the specific subfolder ./node_modules/@microsoft/rush-stack-compiler-3.4. This is true for NPM, PNPM, and Yarn classic, but from what I read it maybe would /not/ be true for Yarn Plug'n'Play.
Really the TypeScript compiler should define tokens that make it more explicit how relative paths are resolved. For example, jest.config.json supports a <rootDir> token, and api-extractor.json supports a <projectFolder> token. That way a shared base configuration can specify paths like "mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts" without any ambiguity what it is relative to.
Also "extends" should support module resolution, so we can write "extends": "@microsoft/rush-stack-compiler-3.4/includes/tsconfig-node.json" instead of assuming a specific node_modules folder placement.
This seems pretty straightforward to implement for TypeScript's tsconfig.json.
Reacted by Stef van Wijchen, Priyajeet Hora, Userbit, Bohdan Shulha, Jonathan Weiss, Nathan Jhaveri and Andi Rosca
Slava (@slavafomin) One thing you can do is publish your tsconfig in an NPM package, then in each project just npm install in each project so that, for example, it will always be located in each projects' node_modules/your-config-package/tsconfig.json and then you can hard code ../../src/**/* inside of that tsconfig.json file and it will resolve to the files that you expect in each project.
Ryan Cavanaugh (@RyanCavanaugh) One problem is that rootDir or baseUrl have no effect on includes and exclude paths. includes and exclude seem to always be relative to the tsconfig.json file, regardless of where it is.
If there was a new top-level option (f.e. projectDir or something), then this could point to some arbitrary location, and all other options (rootDir, rootDirs, baseUrl, outDir, include, and exclude would all be relative to this projectDir setting).
This would then make it feasible that in each project's tsconfig, we could do something simple like
and at that point any paths in node_modules/my-configs/tsconfig.json will always work due to the projectDir being specified. The tsconfig file could be located anywhere, and it would just work.
As Pete Gonzalez (@octogonz) mentioned, being able to specify module identifiers in extends would be convenient too.
Reacted by Pete Gonzalez, Tomáš Szabo, kdevan, Wasuwat Limsuparhat, Michael Leonard, hdmr14, Federico Biccheddu, Abhishek Tomar, Carl Ansley, Olivier Combe and 11 more
Reacted by Michael Leonard, Sushruth Sastry, Jake Correa, Nathaniel Meierpolys and stephen
Activity
RyanCavanaugh commentedon Jul 5, 2018
You might be looking for
rootDirorbaseUrl? A concrete example of what you want to happen would be extremely useful.slavafomin commentedon Jul 5, 2018
Thank you Ryan for a response.
But, according to the docs, the
rootDiroption affects only the output directory structure and thebaseUrloption is for non-relative modules (likenode_modulesI guess?).I will try to explain better. I have multiple projects with very similar directory structure and compilation requirements. I would like to define compilation options (including relative paths to source files from the root of the project directory) in a single place. Therefore I have a single
tsconfig.jsonfile, which I want to use to compile multiple projects. However, the compiler is trying to resolve the source files, specified in the config, not from the project root directory, but from the directory, where this generictsconfig.jsonfile is located.Consider, I have the following generic
tsconfig.jsonfile in~/common/tsconfig.json:And I have multiple projects like
~/project1,~/project2, etc. I want to run the compiler like this:tsc --project ~/common/tsconfig.json --projectRoot ~/project1and I want it to start compilation using files, located in~/project1/src/**/*.tsand not in~/common/src/**/*.ts.Also, I don't want to use
extendsfunctionality of thetsconfig.json, because I want projects to be agnostic from the location of the generic config.I hope it makes sense.
RyanCavanaugh commentedon Jul 5, 2018
I see. There's currently no support for this but we could think about ways to accomplish it. The scenario makes a lot of sense IMO
slavafomin commentedon Jul 7, 2018
Thanks. I would love to see this implemented. Right now I have to manually use
gulp-typescriptjust in order to specify a list of files to compile. It would be nice to invoketscdirectly.itonics-tbeauvais commentedon Dec 13, 2018
This would be great!
siddthesciencekid commentedon Jan 11, 2019
+1 This feature would be awesome
hipstersmoothie commentedon Mar 20, 2019
I have the same use case. My team is currently building out a component library. The repo is a monorepo and each component is written in typescript. Currently I have to have a
tsconfig.jsonin each package that looks like this{ "extends": "../../tsconfig.json", "include": ["src/**/*", "../../typings/**/*"] }This is a lot of duplication. I need to add another tsconfig for build related things. To do this in my current setup I would have to double the amount of tiny tsconfigs in the project.
I've been playing around with moving the tsconfig to our
scriptspackage but can't get it to work because file resolution is relative to the tsconfig. If there was some way to set the--projectRootfrom the cli I could get rid of all of these tiny files. It is important that i can set it from the CLI too. A tsconfig option would not suit my use case.TidyIQ commentedon Jul 9, 2019
This is a big issue when using
"extends": "path/to/tsconfig.json". It means the extending config can't useincludes,excludes,outDir,rootDir, or any option that includes a path as the path to these files are relative to the tsconfig that is extending, instead of being relative to the tsconfig that is extended, which makes a lot more sense.Fr33maan commentedon Aug 16, 2019
Very surprising to not be able to compile just the src directory for exemple. I have some utils directories in my projects which I don't need to be compiled. Additionally I would like to have the
srcanddistdir at the same level. Looks like I need babel to do that or did I misunderstood something ?hipstersmoothie commentedon Aug 18, 2019
L1br3 you should be able to have those at the same level. Here is an example https://github.com/hipstersmoothie/eslint-formatter-github
octogonz commentedon Oct 9, 2019
The
@microsoft/rush-stack-compiler-3.4package works around this by using paths like this:rush-stack-compiler-3.4/includes/tsconfig-base.json
This allows it to be imported like in this example:
ts-command-line/tsconfig.json
However this
../../../..workaround relies on two assumptions:@microsoft/rush-stack-compiler-3.4package is a direct dependency of the project being built; AND./node_modules/@microsoft/rush-stack-compiler-3.4. This is true for NPM, PNPM, and Yarn classic, but from what I read it maybe would /not/ be true for Yarn Plug'n'Play.Really the TypeScript compiler should define tokens that make it more explicit how relative paths are resolved. For example, jest.config.json supports a
<rootDir>token, and api-extractor.json supports a<projectFolder>token. That way a shared base configuration can specify paths like"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts"without any ambiguity what it is relative to.Also
"extends"should support module resolution, so we can write"extends": "@microsoft/rush-stack-compiler-3.4/includes/tsconfig-node.json"instead of assuming a specificnode_modulesfolder placement.This seems pretty straightforward to implement for TypeScript's
tsconfig.json.fix: tsconfig should list its own path
fix: tsconfig should list its own path
feat: typescript infrastructure and few components conversion (#1797)
trusktr commentedon Feb 8, 2020
Slava (@slavafomin) One thing you can do is publish your tsconfig in an NPM package, then in each project just
npm installin each project so that, for example, it will always be located in each projects'node_modules/your-config-package/tsconfig.jsonand then you can hard code../../src/**/*inside of that tsconfig.json file and it will resolve to the files that you expect in each project.trusktr commentedon Feb 8, 2020
Ryan Cavanaugh (@RyanCavanaugh) One problem is that
rootDirorbaseUrlhave no effect onincludesandexcludepaths.includesandexcludeseem to always be relative to thetsconfig.jsonfile, regardless of where it is.If there was a new top-level option (f.e.
projectDiror something), then this could point to some arbitrary location, and all other options (rootDir,rootDirs,baseUrl,outDir,include, andexcludewould all be relative to thisprojectDirsetting).This would then make it feasible that in each project's tsconfig, we could do something simple like
{ "extends": "./node_modules/my-configs/tsconfig.json", "projectDir": "./" }and at that point any paths in
node_modules/my-configs/tsconfig.jsonwill always work due to theprojectDirbeing specified. Thetsconfigfile could be located anywhere, and it would just work.As Pete Gonzalez (@octogonz) mentioned, being able to specify module identifiers in
extendswould be convenient too.feat: add initial tsconfig bases
ramesaliyev commentedon Jul 23, 2024
Any updates on this?