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

Absolute base url calculated relative to running code, not tsconfig path #7

Closed
artaommahe opened this issue May 10, 2017 · 10 comments
Closed

Comments

@artaommahe
Copy link

artaommahe commented May 10, 2017

I have this app structure

tsconfig.json
shared/
services/
  service1/
    index.js

with

const tsConfig = require('../../tsconfig.json');
const tsConfigPaths = require('tsconfig-paths');

tsConfigPaths.register({
  baseUrl: tsConfig.compilerOptions.baseUrl,
  paths: tsConfig.compilerOptions.paths
});

inside index.js
tsconfig basUrl configured as

    "baseUrl": "./services/",
    "shared/*": [ "../shared/*" ]

to allow imports like import {} from 'service1/smth'

but tsconfig-paths makes wrong absolute path for this baseUrl

configLoaderResult { resultType: 'success',
  absoluteBaseUrl: '.../services/service1/services/',

instead of just .../services/

https://github.com/jonaskello/tsconfig-paths/blob/master/src/config-loader.ts#L35

@artaommahe
Copy link
Author

artaommahe commented May 11, 2017

Also looks like there is an issue with matching paths related to baseUrl.
I passed absolute url as baseUrl to tsConfigPaths.register() (to bypass first issue) and still have an error

import {  } from 'service1/client';
...
Error: Cannot find module 'service1/client'

(client placed right in service1 folder)
debugging led me to console.log(absolutePaths); here https://github.com/jonaskello/tsconfig-paths/blob/master/src/match-path.ts#L23 that prints only this

{ 'shared/*': [ '.../shared/*' ] }

So no one path related to baseUrl will be handled except explicitly added shared.

IDE (VSCode) works well with config above

Update
Modifying paths to this

    "paths": {
      "*": [ "./*" ],
      "shared/*": [ "../shared/*" ]
    },

solves this (second) issue, but looks ugly 😟

@Jontem
Copy link
Collaborator

Jontem commented May 23, 2017

Could you create a minimal repro that shows your problem? It's easier to debug something that is runnable.

@artaommahe
Copy link
Author

@Jontem i tried to reproduce same folders/package.json structure here
https://github.com/artaommahe/tsconfig-paths-7
clone, run

yarn
cd services/smth
yarn run start

see an error

Error: Cannot find module 'user/user'

then

// add to tsconfig.json
    "paths": {
      "*": [ "./*" ],

// uncomment in services/smth/index.js
  baseUrl: path.join(__dirname, '../../', tsConfig.compilerOptions.baseUrl),
  /*baseUrl: tsConfig.compilerOptions.baseUrl,*/

and this will work
Same error if you comment out import { user } from 'user/user';

Error: Cannot find module 'shared/shared'

fixes with passing absolute baseUrl

@Jontem
Copy link
Collaborator

Jontem commented May 23, 2017

First if you specify a relative baseUrl it will be resolved relative to where tsconfig.json is. As stated in README.

Second your path mappings will work if looking like this

  paths: {
    "*": ["./*"],
    "shared/*": ["../shared/*"]
  }

@artaommahe
Copy link
Author

First if you specify a relative baseUrl it will be resolved relative to where tsconfig.json is. As stated in README.

yep, and it's correclty mapped to ./services/ but import ... from 'user/*' does not work.

Second your path mappings will work if looking like this

yep, i wrote this at second post, but it looks ugly - typescript correctly understand paths related to baseUrl without this paths hack but tsconfig-paths not.

@Jontem
Copy link
Collaborator

Jontem commented May 23, 2017

So if i understand you correct is that you would like tsconfig-paths to resolve modules relative to baseUrl without having to specify a path mapping explicitly?

@artaommahe
Copy link
Author

@Jontem yep, imho baseUrl should be added as first path for resolving modules imports

@Jontem
Copy link
Collaborator

Jontem commented May 23, 2017

I've made an implementation of this in the commit cb18815

I have just quickly tested it and it seems to work. I'll release it when i have more time to verify. But please try it out.

@artaommahe
Copy link
Author

@Jontem it works, thx

@Jontem
Copy link
Collaborator

Jontem commented May 26, 2017

Fixed in 2.2.0

@Jontem Jontem closed this as completed May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants