Skip to content

Is it possible to configure baseUrl and still use relative path when doing autocomplete? #36181

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

Closed
lgraziani2712 opened this issue Jan 14, 2020 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@lgraziani2712
Copy link

lgraziani2712 commented Jan 14, 2020

EDIT: since TS v4.1 baseUrl is not longer required to use with the paths rule, fixing this issue!

TypeScript Version: 3.7.4

Search Terms: baseUrl, autocomplete, relative path, lib, aliases, monorepo, sibling libs

Code

tsconfig.json

{
  "compilerOptions": {
    // ...

    "baseUrl": ".",
    "paths": {
      "sibling-a/lib/*": ["../sibling-a/src/*"],
      "sibling-b/lib/*": ["../sibling-b/src/*"]
    },
  },
  "references": [{ "path": "../sibling-a" }, { "path": "../sibling-b" }]
}

Expected behavior: I have a monorepo with some libs and some of them uses code from sibling libs. I achieved three things I wanted: (1) to not need to build them just for TS to correctly check the imports (thanks to baseUrl/paths config), (2) to jump into the actual definition instead of the built file (also thanks to baseUrl/paths), (3) to TS understand the relationship between these libs (although I don't know if I configured everything correctly).

The thing is, since I'm using the baseUrl config, any module I try to import from within the same library, but at least two folders up above the actual module will use the alias. E.g.:

// ./packages/sibling-c/src/some/path/to/file.ts
// I try to import
// ./packages/siblin-c/src/some/anotherFile.ts
// And instead of VSCode offering me
import anotherFile from '../../anotherFile';
// It will suggest something like this:
import anotherFile from 'src/anotherFile';

The problem with the alias for files withing the same lib is exactly that: they are libraries, so what I build with TS is what I publish to my private repository manager. When some service import the file with the alises it will crash because it won't understand it.

Is correct what I'm configuring/want to have?

Thank you in advance!

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jan 14, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

@Ore4444
Copy link

Ore4444 commented Apr 20, 2020

i second that. having the same issue. this is a bug

@andrewbranch
Copy link
Member

andrewbranch commented Aug 6, 2020

You can suppress auto imports from using the unrooted path with a VS Code setting:

image

But TypeScript will still believe that src/anotherFile is valid. The proposal to “fix” this (it’s not actually a bug per se; my understanding is it used to be a feature designed for AMD modules and now that it’s being used differently by many people, certain aspects of the feature don’t make sense in the current context) is here: #31869.

@lgraziani2712
Copy link
Author

Uhmmm, you really cannot use the relative value in a monorepo project. If that relativity only worked up to the nearest tsconfig file, maybe, but it isn't feasible to use it if it adds an import of a sibling lib's module as a relative import.

@andrewbranch
Copy link
Member

FWIW, monorepos with project references work really well with lerna and/or yarn workspaces, and don’t require path mappings / baseUrl that way. Out of curiosity, how do you test/run the projects in your monorepo? That is, during development, what mechanism makes "sibling-a" resolve to ../sibling-a?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants