grunt-ts icon indicating copy to clipboard operation
grunt-ts copied to clipboard

resolve paths relative to tsconfig.json

Open esc-mhild opened this issue 7 years ago • 6 comments
trafficstars

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!

esc-mhild avatar Apr 15 '18 17:04 esc-mhild

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.

nycdotnet avatar Apr 15 '18 22:04 nycdotnet

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.

Nel75 avatar Aug 30 '18 09:08 Nel75

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.

esc-mhild avatar Aug 30 '18 09:08 esc-mhild

@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.

Nel75 avatar Aug 30 '18 10:08 Nel75

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.

esc-mhild avatar Aug 30 '18 10:08 esc-mhild

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

Nel75 avatar Aug 30 '18 16:08 Nel75