-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
tsc should not care and fail with ts files outside of rootDir #9858
Comments
To get around, I add "exclude" back to my |
the rootDir is used to build the output folder structure. all files have to be under rootDir for the compiler to know where to write the output. without this constraint, if there are files that are not under rootDir, the compiler has two options 1. either they will be emitted in a totally unexpected place, or 2. not emitted at all. i would say an error message is much better than a silent failure. |
@mhegazy Thanks for clarification. By name |
Here is my tsconfig.json:
My folder structure looks like this:
I want my .ts files in the src folder to be compiled and output with the same folder structures to the build folder. I do this so that I can set my document root to the build folder and keep my src out of the doc root. I'm getting error messages about .ts files in the node_modules folder, same error messages as OP. Note above, I have node_modules excluded. Why is tsc complaining about those? |
@HillTravis your issue seems to be the same as #9552. |
@mhegazy I'm not sure it's the same. Yes, my structure includes a symlink, but anyplace where that symlink exists should be ignored. In #9552, there is no src folder, meaning the node_modules folder exists in the path that is being built. In my scenario, the root directory that tsc should be looking at is src, which does not contain node_modules. So it shouldn't be looking at that folder at all. On top of that, I have the node_modules folder explicitly excluded via my tsconfig.json. So it should be doubly ignored. I also have the Also, in #9552, there was no mention of the error messages or failed compilation. I should also mention that I'm using TypeScript 1.8.10. Specifically, the error messages read:
|
@HillTravis you can trust me on this one :) it is our inconsistent handling of symlinks that throws the compiler off. we follow symlinks only for node module resolution, then use that name as the file name; then the we perform a check to make sure all files are under the source directory using the real path, instead of the user specified path. and that is what generates the error. |
As a test, I tried removing the symlink. Actually, I deleted the whole During more testing, I tried commenting out the in-code import and uses of that particular node module (ng2-datetime) and the error went away. So it's only when I attempt to import that node module that I see the error, regardless of symlink. If you look at the source for that package on GitHub, you'll see that the package.json file has the "main" parameter set to "ng2-datetime.js", although the author also published the .ts file with the same name. This seems to be causing the issue. If I create .d.ts files and delete the .ts files, the issue goes away. It compiled with no issues even after adding the symlink back. So with all respect, I don't see how this issue could be related to the symlink specifically. It seems to just generally cause issues when the author includes the .ts files in the package. Do you know if there is any workaround for this? The OP above said the worked around it by adding "exclude" back to the tsconfig.json. I already have the directory excluded, but it's still trying to compile it. |
This may be related to another, but I can't find it right now. When resolving for a type, |
"tsc should not care and fail with ts files outside of rootDir" I agree. I cannot understand why typescript would be looking at JS files outside the rootDir, since that's where all the source should be. If this is "Working as Intended", then what exactly is the intent or motivation of this behavior? Is there a case where TS would want to compile source files located outside the rootDir? On the other hand, adding an "include" section in tsconfig solves the problem. When provided with files or directories to include, TS does not go looking at other files outside that. |
with typescript 1.8.10 I had a node_modules folder in my homedir
very confusing - although not sure why I had a node modules folder in my home dir to begin with. |
@iwllyu can you check your project on |
Still have the issue with 2.1.4 although it complains about a different set of files
Well I tried with a fresh project and it wasn't happening. Since this is an older project and I already fixed it by removing the extra node_modules folder, I'm not gonna spend any more time trying to recreate it. Thanks for the help! |
About this case. If all you import are types (like I'm really surfing on this "front-typescript, back-typescript" wave thing) from outside - maybe tsc can distinguish what I'm doing? |
Had this as well. |
guys, any resolution on this now? using types from client code inside backend code and otherwise. what's the best option? copy-paste there and there and modify if I change types? making one root dir for 2 projects is not an option, since why backend tsconfig.json will have info on jsx and compile into commonjs when I can use es2015 |
Does |
@unional won't changing baseUrl break all of node_modules imports - requiring to write not import * as request from "superagent"; but import * as request from "node_modules/superagent/..something"; ? |
Bleh, this is really working as intended? I just ran into this, I told typescript that my source was all in a particular folder (via This really feels like an error, if I tell the compiler where my source is via For anyone coming after me, the following will do what you actually want: {
"compilerOptions": {
"outDir": "./output",
"rootDir": "./source",
},
"include": [
"source"
]
} |
This is not what |
Even if I accept that, the current behavior is still in error IMO. If I have any TS files outside of the specified directory then I think hard failing would be much better because the compiler effectively thinks I have given an invalid configuration. Ignoring configuration when it is invalid is (IMO) not the right way to handle this class of failure. Example error message that would be far more clear (along with hard failure):
Note: A big part of the reason the current behavior of rootDir feels very wrong is precisely because it doesn't make sense to have files outside rootDir. When one asks oneself (or the compiler) what does it mean to compile files outside |
The evil is in the naming. It really means |
I have also noticed that |
IMO this comment should be especially taken into account to confirm that this is indeed a bug and should be re-opened as such:
Indeed, it does not make sense. "Source files outside |
+100 on this being a bug (a nasty one actually). Consider following directory structure:
I would like In fact, no combination of And I don't really think my use case is unique, so it's worth reconsidering the "work as intended" label at least from UX point of view. |
The |
I found this issue because I was googling why typescript was trying to compile my webpack.config.js file after I set
This is the answer! The docs for rootDir say
Which I took to mean "input source files". I recommend that @RyanCavanaugh's comment replace the current explanation for this option. |
@sebelga I'd probably consider that a bug - allowing |
Thanks not true.. We have some special rules for emitting .json files... If the file is going to be emitted at same location at itself then and then only it is not emitted. In all cases it is emitted (eg when --outDir is specified, it will be emitted into that folder) |
@RyanCavanaugh I have a similar use case to @sebelga where I want to |
So my solution was to change my "include": [
"src",
"typings"
] And as the #!/bin/bash
rm lib/package.json
mv $(pwd)/lib/src/* $(pwd)/lib
rm -rf lib/src It does work, but feels quite hacky to me. |
@sebelga Do you have |
@sebelga Instead of import { sync as loadJsonFileSync } from 'load-json-file';
const { version } = loadJsonFileSync('../../package.json'); TypeScript won't care about it being outside of the https://github.com/sindresorhus/load-json-file If you want to avoid hardcoding all the |
@RyanCavanaugh I can't set it to "src" as mentioned above. I am importing "package.json" outside our src ( Thanks for sharing this @dylang ! I will try it. |
@RyanCavanaugh But it is what |
I'm not that good at maintaining my composure when I see issues that have been open since 2016 that could probably be very fixed without introducing new issues for existing users by introducing a new "compilerOption" to let the compiler know it can ignore the error in a given use case. For this, I apologize. I can't help it, because I'm relied upon to provide a good service, yet I occasionally encounter issue threads like these. Why is it so difficult for the authors of TypeScript to implement one simple configuration flag? I don't think it's because the developers are lazy, or because they're unwilling to help. I don't think anyone here is a bad person, except for myself. It's this conflict, this debate, which has gone on for years over what appears to be the definition of the property "rootDir". And what to do about this issue. People get thrown into this debate because they believe that they are right. Yes, "rootDir" serves as the base directory for the source code. Yes, the error is valid. Yet, the underlying issue still persists. My tooling compiles the code perfectly, yet my IDE displays this error on my screen. It's a perversion. I want those red lines gone. My files don't have errors in them. And if they do, I should focus on those rather than being distracted by these non-issues. All I want is to suppress this error message. I think that's what most of us want. I don't know if there are other cases here and maybe there are people who would like to speak up about that. Can we please work together and solve this problem? It would make me so happy to see those red lines gone... |
@nullquery If |
@nullquery file a bug that shows how to reproduce the problem and we will either fix it or tell you what's wrong with your project configuration |
Following on from @sebelga workaround, here is is a variation that is suitable to go inside your npm scripts tsc --module commonjs --target ESNext --outDir ./edition-esnext --project tsconfig.json && test -d edition-esnext/source && ( mv edition-esnext/source edition-temp && rm -Rf edition-esnext && mv edition-temp edition-esnext ) || true |
The solution is Project References. To use this, edit your "tsconfig.json" file and add this to the end of the file (outside of the "compilerOptions"):
This will allow you to reference files in the "../common/" folder (and all sub-folders). My earlier confusion was stemming from the fact that IntelliJ and my untainted It turns out that for some reason, "gulp-typescript" is trying a best-effort approach to compilation. An error in TypeScript (such as So, if you're a "gulp-typescript" user, you may experience such an issue depending on your version of "gulp-typescript" and your configuration. (I'm using the latest version at the time of writing, but I'm assuming someone from the future might be reading this. I hope times are better for you.) I know the use of Project References is buried somewhere in this avalanche of responses, but it would have been better if this were documented better. The top 3 results for "TS6059" on Google all lead to GitHub issues. It would have been a lot clearer to me if there was a page documenting these common errors and their solutions. @MicahZoltu Is this something that can be addressed? Should I make a new issue, or is this something that would go faster if it were discussed internally between the main contributors? |
(FWIW I'm not part of the TypeScript team) Generally, I recommend against using As for gulp-typescript, it sounds like perhaps the version of TSC being used by gulp-typescript is different from the version of TSC being used by IntelliJ. When you see symptoms like you describe, that is usually the problem (the other being that they are not using the same |
The version used by "gulp-typescript" should be identical; both versions are derived from It's fine though. As long as IntelliJ gives me errors, I'm happy to accept that the Gulp task will ignore them. I'm not a fan of overhauling my method in favour of the preferred method of the week. The project structure I have makes sense to me. It keeps everything contained yet gives me the flexibility to split things up into multiple components. I don't believe in "allowing compatibility for legacy reasons," and especially when it comes to project references. It must be possible to reference files outside of the When I first began working with TypeScript, I ran into numerous issues. Among them:
I ended up having to develop my own Gulpfile to handle everything. It does the following:
There may be more elegant ways out there, but after jumping from one open-source project to the next, I had quite enough of juggling between the solutions each of them was offering. The approach I've chosen works, it's simple enough to understand (especially now that I understand that "gulp-typescript" does something different) and I'll probably continue using this for years to come. The best solution is one that you understand yourself. While this doesn't help anybody who tries to help me (which I appreciate very much! the help I mean, not the confusion) I can speak from experience that there will always be something wrong with any solution so it's better to stick with something you own yourself than something that is designed, developed and produced by a multicultural team of various beliefs, sexual orientations and gender identities. tl;dr please don't break project references. I need that for my tooling to work. thank you |
I gave up setting up multi-project TS setup proerly and just do But today in one project I changed target from File structure:
Why If |
@alexeyPetrushin you can take a look at some of my projects. It seems to be working fine. e.g.: |
NOPE. And there's no way to turn this error off I can find. |
@SephReed: I've spent 200 rep points on StackOverflow and someone contributed this very simple solution for importing declare module '*.json'; I have no idea how this works, but honestly, I don't care. At that stage, TS was forking for itself, not for me. |
I surprised this is such a heated debate. But I understand the reasoning. For me, I have of course the source folder, but I also have build scripts outside of that folder that isn't meant to be compiled but definitely include type support, because let's be honest your best IntelliSense is TypeScript.
So I use |
I finally got what this flag exactly means without several comments that don't make it any clearer. Just cheers with this. The options that sets folders and files for tsc to take account into compliation: "files", "include", "exclude". Then what you use "rootDir" for?
When you set "include" and "rootDir" as down below, tsc will generate the output as:
When you also set "outDir" as down below, tsc will generate the output as:
Probably you wanted to have this by setting "rootDir" option and changing "outDir" option as down below.
So when you have set "rootDir" option that doesn't cover the scopes of files except from "exclude" option, tsc fails to build because it doesn't make sense with the purpose of "rootDir" option. Yeah it's completely a bad naming practice. It should've been just "rootOfStructureOfInputs" or something. And also, "outDir" should've been just "rootOfOutputs". |
What you see on the SS is not a WebStorm problem - it comes from TS DevServer. I also tried: "exclude": ["lib", "node_modules",
"..", "../..", "../../..", "../../../..", "../../../../..", "../../../../../.."
] Didn't solve the problem either |
@mhegazy You labeled this issue with "Working as Intended" but thats not true. As you can see in my screenshot, tsc-server provides files outside of my rootDir (in this case mobiservice/src) as import suggestion. To me this looks like a bug... |
Could you please help. with configurations of tsconfig
Tried this option But unfortunately, include doesn't like rootDir |
@TymofieievVolodymyr For community assistance with TypeScript, I recommend joining the community run TS Discord server: https://discord.gg/BMBfgBGvf7 I suspect someone there will be able to help you out. |
Thanks) |
TypeScript: 2.0.0
I have a
fixtures
folder which contains.ts
files. In mytsconfig.json
I setrootDir
tosrc
which does contain all my source code (including test).The fixtures files are there for test cases. tsc shouldn't complain their existence and fail the compilation.
The text was updated successfully, but these errors were encountered: