Skip to content

Compilation fails when using project references with tsc -b #26867

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
pragatisureka opened this issue Sep 4, 2018 · 5 comments
Closed

Compilation fails when using project references with tsc -b #26867

pragatisureka opened this issue Sep 4, 2018 · 5 comments

Comments

@pragatisureka
Copy link

TypeScript Version: 3.1.0-dev.20180901

Typescript build fails when using tsc -b with project references. The generated .d.ts files in outDir fails to resolve relative imports imports

Another(consequence) issue is that extending an interface does not recognizes properties of super interface. As can be seen in the output below a.ts imports interface IT2 from which it extends with another property prop3 but the compiler fails to resolve relative reference in generated d.ts file and throws error.

Search Terms:
tsc build
project references

Code
https://github.com/pragatisureka/typescript-build-test

This repo is similar to https://github.com/RyanCavanaugh/project-references-outfile but without outFile

Expected behavior:
Build succeeds

Actual behavior:
Build fails

mainpkg/dist/a.d.ts:1:21 - error TS2307: Cannot find module './b'.

1 import { IT2 } from './b';
                      ~~~~~

pkg2/c.ts:6:9 - error TS2322: Type '{ prop1: string; prop2: string[]; prop3: number; }' is not assignable to type 'IT1'.
  Object literal may only specify known properties, but 'prop1' does not exist in type 'IT1'. Did you mean to write 'prop3'?

6         prop1: 'text',

Related Issues:
#26863
#26689

@ajafff
Copy link
Contributor

ajafff commented Sep 4, 2018

For relative imports to work correctly with project references, you need to make sure that the output structure in outDir is the same as the source directory. That's because the compiler doesn't rewrite the import paths for referenced projects.

Your current setup outputs the following structure:

dist
  ... output files of mainpkg
  ... output files of pkg2
src
  mainpkg
    ... files of mainpkg
  pkg2
    ... files of pkg2

But you actually need the following structure to make it work.

dist
  mainpkg
    ... output files of mainpkg
  pkg2
    ... output files of pkg2
src
  mainpkg
    ... files of mainpkg
  pkg2
    ... files of pkg2

@pragatisureka
Copy link
Author

Structure is exactly same. I have commited the the dist folder as well in the provided repository

src
  mainpkg
     dist
          ... output files of mainpkg
    ... files of mainpkg
  pkg2
     dist
          ... output files of pkg2
     ... files of pkg2

in my example the generated output is

mainpkg
    dist
        a.d.ts -  import { IT2 } from './b';
        b.d.ts - exports interface IT2
        ....other files
    a.ts - import { IT2 } from './b';
    b.ts
pkg2
    c.ts - import {IT1} from '../mainpkg/a';

when tsc is building pkg2 it fails with following message - with --verbose flag

message TS6355: Projects in this build:
    * mainpkg/tsconfig.json
    * pkg2/tsconfig.json
    * tsconfig.json

message TS6352: Project 'mainpkg/tsconfig.json' is out of date because output file 'mainpkg/dist/a.js' does not exist

message TS6358: Building project '/Users/pragatisureka/software/typescript-test/mainpkg/tsconfig.json'...

message TS6352: Project 'pkg2/tsconfig.json' is out of date because output file 'pkg2/dist/c.js' does not exist

message TS6358: Building project '/Users/pragatisureka/software/typescript-test/pkg2/tsconfig.json'...

mainpkg/dist/a.d.ts:1:21 - error TS2307: Cannot find module './b'.

1 import { IT2 } from './b';
                      ~~~~~

pkg2/c.ts:6:9 - error TS2322: Type '{ prop1: string; prop2: string[]; prop3: number; }' is not assignable to type 'IT1'.
  Object literal may only specify known properties, but 'prop1' does not exist in type 'IT1'. Did you mean to write 'prop3'?

6         prop1: 'text',
          ~~~~~~~~~~~~~

@ajafff
Copy link
Contributor

ajafff commented Sep 4, 2018

Thanks for clarifying. I misread the outDir config.

with this setup you would actually need import {IT1} from '../../mainpkg/dist/a' in c.ts to make the compiled output work but compilation might still fail.

I guess the issue importing ./b in a.d.ts is caused by the fact that outDir is nested inside rootDir. Switching to a directory structure like the one I described above should fix that.

@pragatisureka
Copy link
Author

pragatisureka commented Sep 5, 2018

Thanks for the reply I will try it out, if this works then maybe we can mark it as workaround available and close this issue

@pragatisureka
Copy link
Author

@ajafff moving the outDiroutside rootDir solves this problem. Closing this issue.

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