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

declaration maps (*.d.ts.map) have incorrect relative paths in sources unless declarationDir is set #204

Closed
agilgur5 opened this issue Feb 12, 2020 · 2 comments · Fixed by #221
Labels
kind: bug Something isn't working properly

Comments

@agilgur5
Copy link
Collaborator

agilgur5 commented Feb 12, 2020

What happens and why it is wrong

This comes from downstream TSDX at jaredpalmer/tsdx#479 (and jaredpalmer/tsdx#135), which I fixed in jaredpalmer/tsdx#488 by adding a declarationDir, but I'm not sure if this isn't just a bug here. I believe the potential bug is either in

overrides.declarationDir = undefined;
or in
const relativePath = relative(pluginOptions.cwd, fileName);
but I'm not sure. Also not sure if this is related to #136 or not.

The tl;dr on those related issues is that TSDX takes ./src/index.ts as input and outputs ./dist/index.js, ./dist/index.d.ts, and if declarationMap is true, ./dist/index.d.ts.map, which has a sources attribute.

  • The problem is that when declarationDir is not set, sources will incorrectly point to index.ts
  • When declarationDir is set to ./dist, sources will correctly point to ../src/index.ts.
    • (with useTsconfigDeclarationDir: true ofc)

But in both cases, whether declarationDir is set to ./dist or not, everything gets output to ./dist via Rollup config.

If I'm understanding the code correctly, this is because the TS Language Service isn't creating the files when declarationDir isn't set, and so the relative paths are generated incorrectly before emitFile is called.
So that seems like a bug here, but maybe I'm missing something or its not fixable without setting declarationDir, sorry if that's the case 😅

Environment

TSDX @ master

Versions

  • typescript: 3.7.5
  • rollup: 1.27.8
  • rollup-plugin-typescript2: 0.26.0

rollup.config.js

The only difference is what's listed above (useTsconfigDeclarationDir and declarationDir), but current, non-trivial config is here

tsconfig.json

declarationDir is the only relevant one but here's a fixture

package.json

Probably not relevant but here

plugin output with verbosity 3

No errors, so I don't think it's relevant? Can run this on a fixture if needed.

@ezolenko
Copy link
Owner

I think it relates to this line:

if (declaration && !useTsconfigDeclarationDir)
overrides.declarationDir = cwd;

The line above is when declarations are not requested at all.

That cwd should arguably be rollup output folder. Problem with that is rollup can have multiple output folders configured, and when not using useTsconfigDeclarationDir, plugin will emit declarations and their maps and hand them over to rollup, which then decides where to write them. This has one important effect -- it lets other plugins see and potentially process them.

By setting useTsconfigDeclarationDir, user decides to control location from tsconfig and write them out of band into a single folder (which is probably a correct thing to do in most cases).

@ezolenko
Copy link
Owner

We can probably set declarationDir to first rollup output folder in this case and get a significant number of cases right instead of none...

@ezolenko ezolenko added the kind: bug Something isn't working properly label Mar 4, 2020
sisisin added a commit to sisisin/react-hook-form that referenced this issue Mar 20, 2020
Note: If it is not set both tsconfig's `declarationDir` and rollup.config's `useTsconfigDeclarationDir`,
distribution d.ts.map is incorrect
related issue: ezolenko/rollup-plugin-typescript2#204
sisisin added a commit to sisisin/react-hook-form that referenced this issue Mar 20, 2020
Note: If it is not set both tsconfig's `declarationDir` and rollup.config's `useTsconfigDeclarationDir`,
distribution d.ts.map is incorrect
related issue: ezolenko/rollup-plugin-typescript2#204
Repository owner locked as resolved and limited conversation to collaborators May 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
kind: bug Something isn't working properly
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants