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

Unable to resolve path when import uses path relative to baseUrl #68

Closed
OliverJAsh opened this issue Feb 10, 2021 · 6 comments · Fixed by #69
Closed

Unable to resolve path when import uses path relative to baseUrl #68

OliverJAsh opened this issue Feb 10, 2021 · 6 comments · Fixed by #69

Comments

@OliverJAsh
Copy link
Contributor

This is a difficult one to describe but here is a failing test: OliverJAsh@e26fed2

Essentially the issue occurs when we import using a path that is relative to the baseUrl, e.g. given:

tests/withPaths2/tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "root/",
    "paths": {
      "other/*": ["../other/*"]
    }
  },
  "files": ["root/foo.ts", "other/bar.ts"]
}

tests/withPaths2/root/foo.ts:

// import using tsconfig.json path mapping
import 'other/bar'

tests/withPaths2/other/bar.ts:

// import using tsconfig.json `baseUrl`
// This one fails ❌
import 'foo'

This fails with the following error:

/Users/oliverash/Development/eslint-import-resolver-typescript/tests/withPaths2/other/bar.ts
  2:8  error  Unable to resolve path to module 'foo'  import/no-unresolved

This is a valid import—TypeScript understands it so I think this resolver should also understand it.

@JounQin
Copy link
Collaborator

JounQin commented Feb 11, 2021

We're using tsconfig-paths inside, so maybe you can debug if it's a bug of that module first.

@OliverJAsh
Copy link
Contributor Author

I just tested it with tsconfig-paths and it seems to work fine:

const lib = require('tsconfig-paths')
const config = lib.loadConfig()
const f = lib.createMatchPath(config.absoluteBaseUrl, config.paths)
const result = f('foo', undefined, undefined, ['.ts', '.tsx', '.d.ts'])
console.log(result)

After some debugging it seems like the problem is with this condition:

https://github.com/alexgorbatchev/eslint-import-resolver-typescript/blob/82ef3573fa1258e0de8d8181de57ae7109e35ec5/src/index.ts#L234-L237

Because of this the function returns undefined—we never call matchPatch:

image

It seems that the problem happens when we have a module that lives outside of the base URL.

If I remove that early return then my test works as expected. It doesn't seem to break any of the other tests. Do you know why that condition was added?

@JounQin
Copy link
Collaborator

JounQin commented Feb 11, 2021

@teoxoy Any idea?

@teoxoy
Copy link
Contributor

teoxoy commented Feb 11, 2021

It looks like it was added as part of ad33780#diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346R125-R128. It was most likely a misunderstanding on my part on how baseUrl works.

@OliverJAsh could you put up a PR for this and remove those few lines of code?

OliverJAsh added a commit to OliverJAsh/eslint-import-resolver-typescript that referenced this issue Feb 12, 2021
@OliverJAsh
Copy link
Contributor Author

#69

JounQin pushed a commit that referenced this issue Feb 13, 2021
* Failing test

* Remove redundant condition

Fixes #68
JounQin pushed a commit that referenced this issue Feb 16, 2021
* Failing test

* Remove redundant condition

Fixes #68
@JounQin
Copy link
Collaborator

JounQin commented Feb 16, 2021

released v2.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants