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

Issues with order of files in combined output #1359

Closed
bjornbacklund opened this issue Dec 3, 2014 · 22 comments
Closed

Issues with order of files in combined output #1359

bjornbacklund opened this issue Dec 3, 2014 · 22 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@bjornbacklund
Copy link

Context: Visual Studio 2013 Ultimate w/ Update 4 with TS 1.3
I've got a TS project with 500+ files that compiled fine until today when I added a few new files. Now the order of the output file is messed up, parent class gets defined after subclass. I've tried both using a "_references.ts" file and without.
Where can I learn about how the compiler arranges the files when combining into one output file? Is there a trick how to force a certain file to be placed earlier? What would cause a parent class to placed after a subclass?
Hoping for some hints.
---bjorn

@DanielRosenwasser
Copy link
Member

If I get the chance, I'll try to write a formal explanation (and I'll double-check what I'm writing here), but basically the idea is that the order in which the file is built is the order in which files get resolved and parsed. This is effectively a depth-first post-order traversal of the dependency tree. If a file has already been emitted, it won't be again.

As an example, let's say you have the following files:

file1.ts

/// <reference path="file2.ts" />
function a() {
}

file2.ts

function b() {
}

Now let's say you compile with the following:

tsc file1.ts file2.ts --out program.js

Although file1.ts was specified first for tsc, it references file2, so your emit output will be something like:

function b() {
}
function a() {
}

Hope that helps a bit; if you're still having trouble taming the output, I'd be willing to take a look at the general structure of your project.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Dec 4, 2014
@danquirk
Copy link
Member

danquirk commented Dec 4, 2014

This is not the first time someone has run into an issue like this. It's possible that we need to expose some way for people to inspect the order that files are resolved via a compiler flag.

@DanielRosenwasser
Copy link
Member

Seems like on of those things other compilers would emit in --verbose mode.

@NoelAbrahams
Copy link

Seems like on of those things other compilers would emit in --verbose mode.

A verbose mode for the Visual Studio plugin would also be very useful. I'm experiencing many cases of intellisense crashing, sticky red squiggles etc with no way to reproduce or report an error.

@DanielRosenwasser
Copy link
Member

@NoelAbrahams, a --verbose mode is more for the command line compiler, not the language service. Hope you've been filing any more issues like that, we're certainly trying to patch them up, and we appreciate the ones you've filed so far!

@NoelAbrahams
Copy link

@DanielRosenwasser, perhaps the language service could write errors to the windows event log? There are a few code files where I'm consistently losing intellisense after a few minutes or so. I suspect it has something to do with circular dependencies, but because of the size of the source tree, I'm not able to reproduce the problem. I'm always happiest when I'm able to repro something!

@DanielRosenwasser
Copy link
Member

Worth considering; pinging @mhegazy.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 6, 2014

We have the errors showing in popups, but we suppress them in retail :) so may be the desired behavior here is a way to switch back to debug/dev mode.

@NoelAbrahams
Copy link

A pop up would be more useful than an error logged to the event log, because the former is better suited to identifying the activity that actually triggered the crash. If there is a way of editing typeScriptServices.js in order to turn on the popups that would be brilliant.

@bjornbacklund
Copy link
Author

It happened again today and it is related to the order of the files in the .csproj file. I auto-generate files into a new folder and then include all of them into the project at once. I have figured out that I can reorder the files in the csproj file to get the correct output but this should indicate a bug in Visual Studio right?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2014

csproj files are not order sensitive. there are no guarantees that order will be maintained. i would use _references.ts at the root to order these files. can you auto generate _references.ts as you generate the files?

@bjornbacklund
Copy link
Author

I did previously use the _references.ts file but ran into similar issues so I gave it up, and I did generate _import.ts files that were included into the main _references.ts file.
My impression was that Visual Studio was capable of figuring out the order of files itself and that the _references.ts was an older (or non VS) technique.

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2014

The language service asks the project system (in this case csproj) for a list of files to include. csproj does not have a notion of order in project files, so it will return the list of files in the order of definition in the xml, this is prone to error as any addition, removal, move, rename can change the order, and the user has no way to manage or observe it from the UI. so we added _references.ts to allow for a way to inform the language service as well as msbuild (which also uses the csproj) about order. if you are doing --out, i would stick with _references.ts; if using _references still does not enforce order, then that is a bug that we need to look into.

@bjornbacklund
Copy link
Author

But shouldn't Visual Studio be able to at least handle the simple case that you have two files, Parent.ts and Subclass.ts, and order them correctly?

@mhegazy
Copy link
Contributor

mhegazy commented Dec 9, 2014

It has been one of our axioms not to change user code. so we have always refrained from reordering user code. having said that, the compiler has enough information to at least tell you it is not going to work, or help you arrange the files. Issue #21 tracks the detection and flagging of use before definition cases. I think we should be able to get this in 1.5.

@bjornbacklund
Copy link
Author

But to reiterate, if you allow Visual Studio to manage the files that go into the output, Visual Studio should also try to order them correctly, anything else doesn't make sense (just my 2 cents). I suggest you try to get a TS project to behave as closely to a C# project as possible.

@mhegazy mhegazy closed this as completed Apr 21, 2015
@PaulHuizer
Copy link

I really whish this could be fixed. Typescript intellisense does not seem to have any problem at all infering the proper order. When the compiler, which has the option "Combine into one file" does not solve this, it renders larger js files upfront unreliable ...
The normal response to this issue is, use dynamic loading ... that is true. But in my case, the modules which are dynamically loaded, already exist of many classes which are in seperate projects compiled into a single file. Hence the problem exists in that scenario as well ...

@Elephant-Vessel
Copy link

I understand that the typescript compiler in itself might be bound to reference-tags and imports, but do you know if there is any way to have Visual Studio do the dependency analysis and maintain a file like _references.ts automatically for all files in a project? The overhead of having to maintain reference-tags in all files in the project in order for it to compile properly is getting a little painful.

Visual Studio seems capable enough to resolve the source for the implicit references that occur in a file, wouldn't it be a relatively small investment to have it doing a dependency analysis and updating a text file as well?

@M0ns1gn0r
Copy link

Please implement the suggest automatic dependency analysis, maintaining a _references.ts in a large project is difficult.

@domchen
Copy link
Contributor

domchen commented Oct 14, 2016

This may help: https://github.com/domchen/typescript-plus . The typescript-plus compiler is an enhanced version of the original typescript compiler, which provides extra features to the original typescript compiler, such as accessors optimization, class reflection, conditional compilation and the most useful one : automatically reordering the source files by analyzing their denpendencies in code.

It is basicly the same to the original compiler on usage, for a documentation of how to use the extra options please see the README.

@haxiomic
Copy link

haxiomic commented Nov 4, 2016

Looks good @domchen! Do you plan to integrate the automatic ordering work back upstream into typescript?

@domchen
Copy link
Contributor

domchen commented Dec 30, 2016

@haxiomic If the typescript team accepts this feature some day, I will be happy to integrate it back to the original typescript project. Util then, I will keep updating the typescript-plus project.

@microsoft microsoft locked and limited conversation to collaborators Jun 18, 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

10 participants