Skip to content

rootDir and path mapping does not work for paths outside rootDir #21967

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
rolandjitsu opened this issue Feb 15, 2018 · 9 comments
Closed

rootDir and path mapping does not work for paths outside rootDir #21967

rolandjitsu opened this issue Feb 15, 2018 · 9 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@rolandjitsu
Copy link

TypeScript Version: 2.6.2

Search Terms: rootDir, rootDir and path mapping, path mapping, path map rootdir, etc.

Code
Clone cra-workspaces.

  1. Run yarn install at root
  2. cd packages/package-b
  3. yarn build

Expected behavior:
TS compiler should compile without errors.

Actual behavior:
The compiler throws:

yarn run v1.3.2
warning package.json: No license field
$ tsc
error TS6059: File '/Users/agroza/Projects/cra-workspaces/packages/package-a/src/foo.ts' is not under 'rootDir' '/Users/agroza/Projects/cra-workspaces/packages/package-b/src'. 'rootDir' is expected to contain all source files.
error TS6059: File '/Users/agroza/Projects/cra-workspaces/packages/package-a/src/index.ts' is not under 'rootDir' '/Users/agroza/Projects/cra-workspaces/packages/package-b/src'. 'rootDir' is expected to contain all source files.
error Command failed with exit code 2.
i

I understand that this happens because I set rootDir and that the paths are outside, but I need the rootDir to generate a proper folder structure after compilation (everything that's inside of packages/package-b/src should end up compiled in packages/package-b/out-tsc/lib-es2015/ with the exact structure of the contents of packages/package-b/src).

Playground Link:

Related Issues:

@ghost
Copy link

ghost commented Feb 15, 2018

package-b is depending directly on the .ts files in package-a. Since these are .ts files we expect to emit them somewhere. It might be better to depend on the public interface of package-a from its generated declaration files, so that nothing gets emitted for package-a when compiling package-b.

@rolandjitsu
Copy link
Author

@Andy-MS I understand. Ideally, the compiler should somehow know that it should only emit the js for the ts files that are in "rootDir": "src" (and keep the folder tree structure) of package-a even though it also needs some from package-b.

I'm not sure I follow what you're saying.

I use tsc in two ways:

First:

  1. cd to package-b
  2. tsc --watch
  3. Edit some files in package-a and expect tsc to see the change and rerun the compilation of package-b

Second:

  1. cd to package-b
  2. tsc and expect the package to be compiled into js
  3. cd to package-a
  4. tsc and expect the package to be compiled into js

First case it's useful during development. It works the same for tests (with jest), rerun when there are changes in either package.

Second case it's a single run task that's supposed to compile the ts files to js and a subsequent step that takes the result and generates UMD and FESM modules (using Rollup). That's why it's essential to keep the same folder structure, otherwise I'd need to make a different script for each package that knows where the compiler outputs the files and how the folder structure looks like (even though this is trivial, it's easy to forget this when you add new packages or remove them and the output tree changes).

@ghost
Copy link

ghost commented Feb 15, 2018

First:

cd to package-b
tsc --watch
Edit some files in package-a and expect tsc to see the change and rerun the compilation of package-b

That wouldn't work at runtime if the compiled output of package-a isn't also updated -- you would need to --watch both packages.
You may be interested in #17611.

@rolandjitsu
Copy link
Author

@Andy-MS are you sure about that? Because that's not what I'm seeing.

Just cd packages/package-b and run yarn start. Ignore the errors I described and go to package-a and change some .ts files. You will see that the TS compiler picks up on that and will rerun.

@ghost
Copy link

ghost commented Feb 15, 2018

Yes, because it sees the files in package-a as part of its compilation. But what you were asking for was for it to only update the emitted files for package-b -- then you would end up with a broken program because the compiled output of package-a wouldn't be up to date. #17611 would allow you to compile everything together so this doesn't happen.

@rolandjitsu
Copy link
Author

Ok. I've already been subscribed to that issue. So what you're also saying that currently there's no way to have the rootDir and path mappings that point outside of the root dir scope and there's no plan to support this?

@rolandjitsu
Copy link
Author

rolandjitsu commented Feb 16, 2018

Quick question, could I use the programmatic API to find out how the folder structure emitted by the compiler would look like (based on the path mappings and base url)?

@rolandjitsu
Copy link
Author

Ok, I resorted to simply searching for a file that I know will be at the root of the emitted folder that I'm interested in. Not ideal, but it works.

If there are no plans to support a rootDir when path mappings are outside of the root dir path, then I guess this issue can be closed.

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 16, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants