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
Add ability to configure TypeScript Node from tsconfig.json #4
Comments
Hi, in the source code I see, that the tsconfig.json is already being read. This leads to some issues with my project. My project looks like this: /src/my/typescript/**/files.ts When running tsc here everythings works as expected. But when I run mocha --compilers ts:ts-node/register it also loads the tsconfig.json and gets confused with the paths, because the tsconfig says, all ts files would be located in src. But obviously the tests aren't. Also it builds the paths wrong, e.g. it searches in: /tsconfig.json/src/../.. So the tsconfig.json filename is in the path. If I temporarily rename the tsconfig.json to something different and run mocha again, everything works just fine. So I guess, having the ability to ignore the tsconfig for ts-node would be nice, although I am not sure, how this could be done in conjunction with mocha. Do you have any suggestion? |
@ghost23 Can you please elaborate on the issue you're having. It's likely you're having a different issue, but I don't have enough details to understand what it is. I'm using it with tests all the time, and I absolutely need to load from You say that |
Hi, when I run the mocha command, I get the following error, that's why I said, it is in the path: mocha --compilers ts:ts-node/register ⨯ Unable to compile TypeScript File 'C:/Users/me/projects/ts-test/src/analysis/body_traverser.ts' is not under 'rootDir' 'C:/Users/me/projects/ts-test/tsconfig.json/src'. 'rootDir' is expected to contain all source files. (6059) npm ERR! Test failed. See above for more details. You see, the first three files really are in the src directory, whereas the last one is the test.ts itself, which is not in the src directory, but the test directory. Anyway, the errors state, that the path for rootDir is expected to be ..../projects/ts-test/tsconfig.json/src My tsconfig.json file looks like this: { |
@ghost23 Thanks for the response, looks like It also might be worth it, from |
As far as I can see, |
Yes, when I compile with tsc, everything works as expected. Of course, I am not compiling the tests then, which is the way I want it. I only want the tests to compile on the fly during the mocha test. |
@ghost23 Weird, and you don't have a |
The way I understand it is, when you run tsc only with rootDir, it takes everything and ONLY everything in that directory and compiles it. So no need for a files array. Quite comfortable in my view :) |
@ghost23 Right, I understand. It's just last time I tried that, it wasn't working and I'm trying to recall why. Most likely, it was the usage of the |
Oh, I forgot to mention that I also have a typings directory in root. Sorry. Now that you say that, I begin to understand your point. It is indeed weird, that tsc is OK with looking for typings outside of rootDir. But it works. |
Just in case the question comes up, I am using tsc 1.6.2 |
Awesome, thanks. It seems if you're using definition files only outside of root, it's ok. Not sure if that's new, or I had some other issue, but it makes sense. How is
|
As I said, I am not compiling the test files with tsc. tsc is ignoring the test folder, because it is outside rootDir. I only want to compile the test files during the mocha run. And when I simply remove or rename tsconfig.json and run mocha --compilers ts:ts-node/register in the root folder, it just magically works. But not, if the tsconfig.json file is present. |
@ghost23 I understand, but how are they ignored? Mine aren't ignored by default. Are you using Edit: Basically, I'm replicating what you have told me above and running Edit 2:
|
Gee, I am ... something. I actually also get the error, but since I have some more files, the error quickly disappeard just behind the window. So yes, I do have the error, but tsc still compiles all the other files just fine. I guess, with an exclude the problem could be fixed. |
yes,
fixes the error |
Awesome, I just pushed |
Great, thanks for your quick responses. How would I use that in conjunction with mocha? |
You'd have to use the executable - |
ah, ok. I see. will try that. Thank you. |
@ghost23 It should have "just worked" for you too, since it overrides the |
Ok, I tried it with ts-node --noProject node_modules/mocha/bin/mocha, but it didn't work. I got weird error messages, which indicate, that typescript had not been transpiled first and so it gave errors for "import ..." statements and the like. Perhaps mocha is programmatically running normal node unless i pass the compilers statement?! |
@ghost23 Replace |
I don't know if this still belongs in this issue, but i have the following tscconfig:
when I run I've tried:
|
@willm Sounds like you're doing something else, |
I am having The script i am running is `"test": "mocha -r ts-node/register test/test.ts",`` Any solution? |
Why don't you just make your |
I'm working on a PR that loads config flags from tsconfig.json. In the process, I'm generating a JSON schema from the TS types via typescript-json-schema. So far the experience is nice: The JSON schema generator can extract arbitrary @-tags into the schema, which means it could hypothetically drive the CLI parser or render the README, too. Not sure it's worth the effort, but it's cool that it's possible. |
@cspotcode just FYI schemastore has a schema for That schema tends to be the default used by IDEs & other tools these days, and schemastore will be happy to include the definition of the |
@G-Rath very cool, that sounds great. Right now the schema is extending https://schemastore.azurewebsites.net/schemas/json/tsconfig.json via a I found something suggesting that VSCode used that schema by default. Is that a Microsoft mirror of schemastore or something? |
@cspotcode that is schemastore 😄 They host all their schemas, so you don't have to download them; instead you just point to the URL in https://schemastore.azurewebsites.net/json/ (MS actually use that schemastore as the official schema, which is one of the reasons why it's such hig quality) |
Ok, I wasn't sure if it was the same as http://json.schemastore.org/tsconfig. We should still bundle the schema with ts-node in case there are changes, and users want to use the version of the schema built for a specific version of ts-node. But it'll be sweet having a default that "just works" for everyone. |
A few notes about the implementation: Config order of precedenceWe're adding a third source of config flags: env vars, argv flags, and now tsconfig. They override each other in this order: Question: Should tsconfig be allowed to specify
|
Quirk: Different compiler used to read tsconfig than to compile
This could impose a performance hit, since we're loading 2x compilers at startup. But again, if |
This isn't currently true, is something changing here? Currently it passes
Possibly. To be fair, the
Let's not support it today, it does seem confusing. At least we could always add it later. |
@cspotcode We might want to rename it to the |
Awesome, I like that better. I'll go with "ts-node".
Currently ts-node is able to read the |
Right, that makes sense. Maybe we can omit that functionality until it’s requested? It doesn’t sound ideal to load twice. |
Random drive-by:
Is there a particular reason for this? I feel like it might be better for env variables to override This could be a moot point b/c technically the configuration for |
@blakeembrey To make sure we're on the same page, I think we agree on the following implementation: If The above implementation assumes that the custom compiler does not change the behavior of config file discovery or parsing. I'm pretty sure this is true for every custom compiler I know about. ( Also, this implementation remains fast for the common case: using the default compiler. If users of custom compilers complain about a slight startup cost, we can explain this chicken-and-egg problem and recommend that they use the We could also add a flag One other quirk: users won't be able to specify |
@G-Rath I'm not opposed to that. My goal with this feature is to avoid using environment variables, so I admittedly don't think about the UX of env vars much. I'd rather avoid them. How do people typically use the env vars? Do they set them in their bash profile? Do they set them via package.json "scripts"? The former would be very problematic if it override tsconfig; the latter seems more in line with what you want. Today, Can we find a situations where we'd want to override options via env vars, and we could not easily use |
I’m wondering if we can just remove compiler overrides from the tsconfig to start with altogether. Do you have a use-case for it already? If not, let’s delay supporting it in the initial version to keep the implementation simpler. Also agree that extended config options aren’t an issue for this version. I can’t see anyone making a request for it either. |
On the env var debate, I don’t have too much of an opinion. I think if the goal is to get this in without too many changes, the easiest place to apply these options before flags and environment variables. |
...and everything would "just work." The user can put |
When we're using a tool that uses In my mind being able to do In saying all that....
You make a very good point - I think it might be better to have Personally, my two wishes are: the I've not followed this issue a lot, so you might have already answered this but where does |
@G-Rath Thanks, I think your use-case will be easy regardless of which way the env-var debate goes.
For your example,
EDIT: forgot to include |
Since the module could end up being used in a lot of different environments (E.g.
gulp
), there should be a way to configure it via config file or environment. Sincetsconfig.json
already exists, I'd say we piggy back off it and add atypescript-node
property to the object.The text was updated successfully, but these errors were encountered: