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

resolve paths relative to tsconfig.json #421

Open
esc-mhild opened this issue Apr 15, 2018 · 6 comments
Open

resolve paths relative to tsconfig.json #421

esc-mhild opened this issue Apr 15, 2018 · 6 comments

Comments

@esc-mhild
Copy link

esc-mhild commented Apr 15, 2018

Relative paths in tsconfig.json (such as rootDir) are resolved relative to Gruntfile.js instead of tsconfig.json.
As mentioned in issue #397, this causes problems when tsconfig.json is used by other tools, such as an IDE. There are more comments in pull #399.

I am afraid to say that I could not follow the reasoning in either thread and apologize if this is repetitious. I will offer two reasons to think that the current behaviour is a bug and not a feature.

First, the Typescript documentation seems not to be explicit on resolution of relative paths (what am I missing?) except when it discusses inheritance:

All relative paths found in the configuration file will be resolved relative to the configuration file they originated in.

However, tsc implements resolution relative to tsconfig.json. It would seem to me that grunt-ts should compile tsconfig.json correctly iff tsc -p path/to/ tsconfig.json does (when tsc is called from the parent directory of the Gruntfile). This is currently not the case.

Second, the following possible work-around is cumbersome:

  1. Author tsconfig.json with paths relative to tsconfig.json.
  2. Create an extension tsconfig.grunt.json that modifies paths for the benefit of grunt-ts.

For example, let:

projectdir/
  |-Gruntfile.js
  |-subdir/
      |-tsconfig.json
      |-tsconfig.grunt.json
      |-ts/
          |- ... ts files to compile

tsconfig.json:

{
	"compilerOptions": {
		"rootDir": "ts",
                ...
	}
}

tsconfig.json would compile correctly for use by IDE or tsc -p subdir/tsconfig.json where tsc is called from the project directory projectdir/.

tsconfig.grunt.json:

{
	"extends": "./tsconfig",
	"compilerOptions": {
		"rootDir": "subdir/ts"
	}
 }

tsconfig.grunt.json is referenced in grunt-ts via tsconfig: 'subdir/tsconfig.grunt.json'.

Many thanks for your great work!

@nycdotnet
Copy link
Contributor

Thanks for the report. The feature is intended to allow tsconfig files to work within IDEs and with tsc -p, as you said. If you’re not seeing that, then there is a bug. It’s possible I didn’t implement the transform for rootDir correctly. I recommend that you use tsconfig passThrough true unless you want the overriding functionality of grunt-ts. I will investigate rootDir.

@Nel75
Copy link

Nel75 commented Aug 30, 2018

I'm having this same issue - the output directory structure generated by grunt-ts is inconsistent with that generated by the IDE (WebStorm) and by running tsc directly against the same tsconfig.json that I feed to grunt-ts.
I have my tsconfig.json configured with a rootDir="." so that the source directory structure is recreated in the specified output directory. Grunt-ts, contrary to the IDE and tsc, is adding the parent directory of the tsconfig.json to the output directory structure. As confirmed here, I had suspected this issue was due to grunt-ts resolving the path relative to Gruntfile instead of to tsconfig.json. This to me also seems counterintuitive and inconsistent.
I attempted to use the passThrough flag, as suggested, but that yielded the same result.
I hope this is fixed so that grunt-ts resolves the paths relative to the specified tsconfig.json as the IDE and tsc do without needing any workarounds or flags, but I'd be happy to pass a flag if it worked in this case. In the meantime, I'll have to set up my grunt to use tsc directly.

@esc-mhild
Copy link
Author

As a workaround, I opted to call tsc directly using "grunt-exec": "3.0.0" as shown below. To be sure, this creates some difficulties when working cross-platform but the direct use of only tsconfig.json is appealing.

			exec : {
				ts : {
					cmd : './target/node/node node_modules/typescript/bin/tsc -p tsconfig.json'
					// for windows:
					// cmd : '.\\target\\node\\node node_modules\\typescript\\bin\\tsc -p tsconfig.json'
				},
				rollup : {
					cmd : './target/node/node node_modules/rollup/bin/rollup -c rollup.config.js'
					// for windows:
					// cmd : '.\\target\\node\\node node_modules\\rollup\\bin\\rollup -c rollup.config.js'
				}
			},

An important complication is the determination of the node path to use (cf. "./target/node/node") since this may differ from the system installation. We use the above on Linux and Windows.

@Nel75
Copy link

Nel75 commented Aug 30, 2018

@MatthiasHild Thanks! I started going that route but then decided to use the tsconfig.grunt.json workaround as it was simpler and to not lose the nice console output that grunt-ts provides.

@esc-mhild
Copy link
Author

I do not want to detract from the benefits of using a dedicated tsc wrapper!

FYI, base output from exec as below. Additional output via tsconfig.json properties:

	"diagnostics": true,
	"traceResolution": false,
	"listFiles": false,
	"listEmittedFiles": false

Sample output:

Running "exec:ts" (exec) task
Verifying property exec.ts exists in config...OK
File: [no files]

./target/node/node node_modules/typescript/bin/tsc -p tsconfig.json
buffer   : disabled
timeout  : infinite
killSig  : SIGTERM
shell    : true
command  : ./target/node/node node_modules/typescript/bin/tsc -p tsconfig.json
args     : []
stdio    : [ignore,pipe,pipe]
cwd      : /home/m/dev/web/web-standard
exitcodes: 0
pid     : 12531
Files:           269
Lines:         50469
Nodes:        233666
Identifiers:   75811
Symbols:       59992
Types:          8294
Memory used: 113657K
I/O read:      0.01s
I/O write:     0.04s
Parse time:    0.58s
Bind time:     0.27s
Check time:    0.83s
Emit time:     0.42s
Total time:    2.10s
>> Exited with code: 0.

@Nel75
Copy link

Nel75 commented Aug 30, 2018

@MatthiasHild Ah ok, great! I missed those settings in tsconfig.json. Thanks again.

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

3 participants