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

baseUrl: "./" confuses VSCode and Jest (and me) #133

Closed
mikl opened this issue Jun 7, 2019 · 2 comments · Fixed by #707
Closed

baseUrl: "./" confuses VSCode and Jest (and me) #133

mikl opened this issue Jun 7, 2019 · 2 comments · Fixed by #707
Labels
kind: support Asking for support with something or a specific use case solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths

Comments

@mikl
Copy link

mikl commented Jun 7, 2019

Current Behavior

When editing a file like src/middleware/healthCheck.ts, and having VS Code automatically import something from another file (src/util/checkHttpMethod.ts) based on autocomplete, VS Code will generate a statement like:

import { checkHttpMethod } from 'util/checkHttpMethod'

However, this appears to be incorrect.

When the compiler runs (from npm start, ie. tsdx watch), it will say the following:

No name was provided for external module 'util/checkHttpMethod' in output.globals – guessing 'checkHttpMethod$1'

And worse yet, when I run npm test with a test that tries loading src/middleware/healthCheck.ts, it will fail with this error message.

 FAIL  test/middleware/healthCheck.test.ts
  ● Test suite failed to run

    Cannot find module 'util/checkHttpMethod' from 'healthCheck.ts'

    However, Jest was able to find:
    	'../../src/middleware/healthCheck.ts'

    You might want to include a file extension in your import, or update your 'moduleFileExtensions', which is currently ['ts', 'tsx', 'js', 'jsx', 'json', 'node'].

    See https://jestjs.io/docs/en/configuration#modulefileextensions-array-string

Everything works fine if I switch the import statement to use a relative path like this:

import { checkHttpMethod } from '../util/checkHttpMethod'

Expected behavior

I would have expected things to "just work" - ie. VS Code reads the tsconfig.json and is able to figure out the right path when importing files.

Suggested solution(s)

I'm not sure I understand what the baseUrl: "./" setting is supposed to do, so I don't have any immediate suggestions. Maybe I'm just doing it wrong?

Additional context

The test in question lives in test/middleware/healthCheck.test.ts and has this import statement:

import { HEALTH_CHECK_PATH } from '../../src/middleware/healthCheck'

Your environment

Software Version(s)
TSDX 0.6.1
TypeScript 3.5.1
Browser N/A
npm/Yarn npm 6.9.0
Operating System macOS 10.14.5
@swyxio
Copy link
Collaborator

swyxio commented Jun 7, 2019

vscode and typescript do read baseUrl fine, but this is actually more of a rollup issue. see #91

@agilgur5
Copy link
Collaborator

agilgur5 commented Mar 20, 2020

So actually this happens because of paths being set weirdly, not baseUrl (although paths are relative to baseUrl), so pretty much a duplicate of #91.
(Edit: actually baseUrl also makes it possible to do src/thing.ts instead of ./src/thing.ts, but the specific problem here seemed to be from paths containing src/*)

Will be looking to handle both paths and baseUrl with a Babel plugin, but will also probably remove the current tsconfig.json defaults that add src/* to paths because that's been very confusing to most users (and I don't think src/* is the path most aliases normally use either). paths inform VS Code's auto-complete as far as I can tell, so that creates even more confusion when it doesn't work.

@agilgur5 agilgur5 added topic: TS Paths Aliases Related to using aliases with TypeScript paths solution: duplicate This issue or pull request already exists labels Mar 20, 2020
@agilgur5 agilgur5 added this to the v0.13.x milestone May 3, 2020
@agilgur5 agilgur5 linked a pull request May 3, 2020 that will close this issue
@agilgur5 agilgur5 added kind: support Asking for support with something or a specific use case solution: workaround available There is a workaround available for this issue labels Aug 25, 2020
Repository owner locked as resolved and limited conversation to collaborators Aug 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: support Asking for support with something or a specific use case solution: duplicate This issue or pull request already exists solution: workaround available There is a workaround available for this issue topic: TS Paths Aliases Related to using aliases with TypeScript paths
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants