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

pathMapping doesn't use rootDirs for absolute or mapped paths #7837

Closed
alexeagle opened this issue Apr 5, 2016 · 3 comments
Closed

pathMapping doesn't use rootDirs for absolute or mapped paths #7837

alexeagle opened this issue Apr 5, 2016 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@alexeagle
Copy link
Contributor

Here's a mockup of our directory structure:

$ cat binary/some/other/project/bin-a.ts
export let a;
$ cat generated/path/to/my/project/gen-b.ts
export let b;
$ cat generated/spot/for/node/modules/angular2/core.d.ts
export let bootstrap;
$ cat path/to/my/project/c.ts
// NOTE: we don't use a relative path here because it's a walk up to the top of the source tree and back
import {a} from 'some/other/project/bin-a';
import {b} from './gen-b';
import {bootstrap} from 'angular2/core';

And tried to compile with

"compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": false,
        "baseUrl": ".",
        "rootDirs": [
          "generated",
          "binary",
          "."
        ],
        "paths": {
          // NOTE: this assumes we'll resolve it under the "generated" root, to find the generated .d.ts file
          "angular2/*": ["spot/for/node/modules/angular2/*"]
        },
        "noEmit": true
    },

and I get

$ ./node_modules/.bin/tsc
path/to/my/project/c.ts(1,17): error TS2307: Cannot find module 'some/other/project/bin-a'.
path/to/my/project/c.ts(3,25): error TS2307: Cannot find module 'angular2/core'.

In Vlad's comment we see why:
#5039 (comment)

baseUrl / paths are used to resolve non-relative module names and rootDirs are applied only for relative names

Sorry we didn't notice this earlier. I believe @mprobst had verified our use cases against #2338 and then maybe the design changes before merge made this no longer work. I'm just now working to update google-internal to use this (we are also blocked on #7573 )

@mprobst please check if I've accurately represented the shape of imports that we want. @vladima I know this is close to release, so I wonder what our options are now. Could rootDirs be consulted for all resolutions, or did you eliminate that option in the design process?

@mhegazy
Copy link
Contributor

mhegazy commented Apr 5, 2016

so why not specify these in path as well:

"compilerOptions": {
        "baseUrl": ".",
        "rootDirs": [
          "generated",
          "binary",
          "."
        ],
        "paths": {
            "*" : [ "generated/*", "binary/*", "./*" ],
           "angular2/*": ["spot/for/node/modules/angular2/*"]
        }
    },

@alexeagle
Copy link
Contributor Author

Ah, thanks Mohamed, that's a good point. It gets verbose since I also need

"paths": {
          "angular2/*": ["spot/for/node/modules/angular2/*",
          "generated/spot/for/node/modules/angular2/*"]
        },

but we codegen the tsconfig.json so I don't really care how it looks.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 5, 2016

The idea is that rootDirs are for relative imports, as you have no other way of changing how these are resolved. but for non-relative names you have paths and baseUrl. We have more documentation on using baseUrl, paths and rootDirs at https://github.com/Microsoft/TypeScript-Handbook/blob/release-2.0/pages/Module%20Resolution.md#additional-module-resolution-flags

@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Apr 5, 2016
@mhegazy mhegazy closed this as completed Apr 5, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants