Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set TypeScript compiler's base/working directory #25430

Open
slavafomin opened this issue Jul 4, 2018 · 14 comments
Open

Set TypeScript compiler's base/working directory #25430

slavafomin opened this issue Jul 4, 2018 · 14 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@slavafomin
Copy link

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.

@RyanCavanaugh
Copy link
Member

You might be looking for rootDir or baseUrl? A concrete example of what you want to happen would be extremely useful.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jul 5, 2018
@slavafomin
Copy link
Author

Thank you Ryan for a response.

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:

{
    "compilerOptions": {
    },
    "include": [
        "index.ts",
        "src/**/*.ts"
    ],
    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

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.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Question An issue which isn't directly actionable in code labels Jul 5, 2018
@RyanCavanaugh
Copy link
Member

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
Copy link
Author

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.

@itonics-tbeauvais
Copy link

This would be great!

@siddthesciencekid
Copy link

+1 This feature would be awesome

@hipstersmoothie
Copy link

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

{
  "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 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.

@TidyIQ
Copy link

TidyIQ commented Jul 9, 2019

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.

@Fr33maan
Copy link

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 ?

@hipstersmoothie
Copy link

@L1br3 you should be able to have those at the same level. Here is an example https://github.com/hipstersmoothie/eslint-formatter-github

@octogonz
Copy link

octogonz commented Oct 9, 2019

The @microsoft/rush-stack-compiler-3.4 package works around this by using paths like this:

rush-stack-compiler-3.4/includes/tsconfig-base.json

{
  "$schema": "http://json.schemastore.org/tsconfig",

  "compilerOptions": {
    "outDir": "../../../../lib",
    "rootDirs": ["../../../../src/"],

This allows it to be imported like in this example:

ts-command-line/tsconfig.json

{
  "extends": "./node_modules/@microsoft/rush-stack-compiler-3.4/includes/tsconfig-node.json",

  "compilerOptions": {
    "types": [
      "jest",
      "node"
    ]
  }
}

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.

priyajeet pushed a commit to priyajeet/box-ui-elements that referenced this issue Jan 16, 2020
priyajeet pushed a commit to priyajeet/box-ui-elements that referenced this issue Jan 16, 2020
mergify bot pushed a commit to box/box-ui-elements that referenced this issue Jan 16, 2020
* feat: typescript

* fix: prettier

* fix: copy ts migrated .js.flow files to es

so that parent projects get tricked

* chore(avatar): typescript conversion

* feat: typescript support for prettier and eslint

BREAKING CHANGE: Peer deps have been updated and added.

* chore: upgrading to new @box/frontend

* fix: merge conflicts

* fix: tsconfig should list its own path

relative to this current project. See microsoft/TypeScript#25430
@trusktr
Copy link
Contributor

trusktr commented Feb 8, 2020

@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.

@trusktr
Copy link
Contributor

trusktr commented Feb 8, 2020

@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

{
  "extends": "./node_modules/my-configs/tsconfig.json",
  "projectDir": "./"
}

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 @octogonz mentioned, being able to specify module identifiers in extends would be convenient too.

@r002 r002 mentioned this issue Feb 5, 2021
3 tasks
agilgur5 added a commit to agilgur5/tsconfig that referenced this issue May 20, 2022
- base, library, and library-build, same as the package exports
  - initially started with library, but if I wanted to use this repo
    for apps as well, I wouldn't want some configurations like
    declarations or declaration maps as they're unused by apps
    - so split off library and base
  - library-build doesn't add much and is for a particular use-case, so
    I also may end up removing that at some point

- NOTE: `extends` resolves relative paths based on the location of the
  _extended_ config file, not the one doing the extension
  - https://www.typescriptlang.org/tsconfig#extends
  - This effectively means that all "path-based compiler options" like
    `outDir`, `outFile`, `rootDir`, `include`, `exclude`, and `files`
    have to be repeated in all tsconfigs
    - c.f. microsoft/TypeScript#29172,
      microsoft/TypeScript#25430
    - so the usage of them here is more of as an "example" of sorts of
      what should be repeated, as installing from NPM would result in
      the paths here being _inside_ `node_modules` (or elsewhere pending
      the NPM client you use)
@ramesaliyev
Copy link

Any updates on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

10 participants