TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Compile on save not always working

Open Anupheaus opened this issue 11 years ago • 18 comments

I have an intermittent issue with the compile on save feature in VS2013, which is that it seems to get stuck after a while for some files. It is a strange issue, i'll be working for an hour or so and then one file just won't compile when saved, it says in the bottom left "Item(s) Saved" but I'll not get the "Output(s) generated successfully" message. Quite often it is a few seconds before that message finally arrives anyway, but once a file get's "stuck" it just won't compile at all and I'll never get that message, yet I can switch to another file within the IDE, save that and it recompiles the file fine. Then I can go back to that "stuck" file and save it and....nothing, just "Item(s) Saved". The only way I've found to fix this issue is to close VS and reopen it then save the file and it works again. Strange that it only affects one file after a good while of coding.

I know it is not my machine as it happens both at work and home, although it is the same solution file.

Anupheaus avatar Feb 18 '15 23:02 Anupheaus

Which version of TypeScript are you using? Are you able to share the solution where this is happening?

danquirk avatar Feb 18 '15 23:02 danquirk

I'm using version 1.4 but it did happen in the previous version as well (1.3), unfortunately I wouldn't be able to share the solution (my boss would most likely kill me). I'm not sure how that would help either because closing and reopening VS solves the problem.

I suspect it has something to do with the compiler not freeing up some resource and becoming overwhelmed for that particular file. I would guess that upon saving, the tsc has to go through the references given and check everything that is visible and available to this particular file? Is a thread or process started for each file? Does it try and re-use that same thread or process when saving the same file? If so, I'd say it is getting stuck and some kind of check for this should be put in place that "restarts" the thread or process for this file. I really am just guessing at how you've done this though.

Anupheaus avatar Feb 19 '15 00:02 Anupheaus

I have also experienced exactly the same problem. The compile on save feature is not always stable.

We also have combine output to a single file set. This seems to make things a bit worse (I'm guessing).

Edit

I should have said that we have "compine output to single file" set for a couple of projects, but for the others we have all output written to an output folder (without combining).

NoelAbrahams avatar Feb 19 '15 10:02 NoelAbrahams

What are the messages in the status bar? Do you see a message for successful emit? Or an error?

mhegazy avatar Feb 20 '15 03:02 mhegazy

As noted above

it says in the bottom left "Item(s) Saved" but I'll not get the "Output(s) generated successfully" message

i.e. there is no error message.

If there was a way to set logging verbosity in typescriptservices.js then problems like this would be trivial to track down.

I've also had a crash or two in VS 2015. There is nothing useful in the Windows event log.

NoelAbrahams avatar Feb 20 '15 10:02 NoelAbrahams

Sorry yeah, like @NoelAbrahams stated, no error messages, but I've not had VS crash on me but then I am using VS 2013.

I noticed yesterday that I restarted VS, then roughly only 3 mins later I opened a file, changed something, saved it and it didn't compile, it wasn't a file I'd been working on for a long time and it wasn't a file previously opened. Anyway I tried saving a couple of times, but it wouldn't recompile, so I closed VS, re-opened it and saved it and then it compiled - strange issue really.

Just so you know, I have to close VS around 4-5 times every day now due to this issue and whilst this isn't really too bad, it is becoming a big nuisance, especially when you don't realise and you refresh the page and go to where you need to to test it (which can take several seconds each time) and the fix hasn't worked and you then have to check whether the file compiled or not.

Anupheaus avatar Feb 20 '15 11:02 Anupheaus

@NoelAbrahams you enable dev mode and you should be able to debug when this happens.

to enable dev mode: https://github.com/Microsoft/TypeScript/wiki/Dev-Mode-in-Visual-Studio

you can put a break point in getEmitOutput, which will be called on save.

mhegazy avatar Feb 20 '15 18:02 mhegazy

@Anupheaus sorry for the trouble. Do you see a pattern for when it stops working? is it a specific file that often triggers the issue? any help identifying the source of the problem would be greatly appreciated.

mhegazy avatar Feb 20 '15 18:02 mhegazy

@mhegazy no worries, hey you guys are doing an amazing job with this and here I am complaining after it has made my life sooooo much better as a developer. I really want you to know that despite my complaining - I seriously appreciate every effort you guys have made and my team and I love you all for it!

As for identifying a pattern, I really wish I could, it seems very random, like I said, I can work for hours without it happening then it can happen twice in 10 minutes. I can't identify a type of file either, it happens on short files, long files, simple files, etc. I can see no reason for it sometimes - especially when I can switch to another tab which can be way more complicated and the compiler works fine on that file, switch back and it just won't work.

I did notice, and this may be an unrelated issue or it may just be the key (though I doubt it) but I rebuilt my solution yesterday and one file broke the building of it because TypeScript complained that it was not able to access or write to it or something...bit strange, don't know whether I'd got it open but because that failed to compile, all my related libraries then failed to compile as well. I simply rebuilt again after it had finished and it was fine. I'm wondering if file locking may be an issue here? Is it possible that compile on save just doesn't compile and doesn't error if a file lock is in place? It would explain all the things we have been saying, such as closing and re-opening VS would fix it (assuming it was something in VS locking it) and why it would still not work just closing the tab and re-opening it and trying to save it again.

Anyway, that's my thoughts, sorry I can't give you anything more concrete but you can be sure I'll keep looking for any patterns, and once again, great job guys!

Anupheaus avatar Feb 20 '15 20:02 Anupheaus

@Anupheaus, I'm having better luck with Visual Studio 2015 CTP 5. The compile on save occasionally appears to stick for up to 4-5 seconds but fires through in the end.

This seems to be different from VS 2013, where the feature just stops working. I'm not yet sure that the behaviour I'm experiencing in 2015 is consistent, because I've been working on specific projects and there may be problems in other projects.

But it would be interesting if you can try out VS 2015, since you have consistent problems.

NoelAbrahams avatar Feb 23 '15 11:02 NoelAbrahams

Unfortunately, at this time I can't upgrade, we are due to release in a couple of months and upgrading now could seriously affect that if things don't go well with the upgrades.

Anupheaus avatar Feb 23 '15 11:02 Anupheaus

Does unloading and reloading the project help? If so, this macro may be of use to you as a workaround: https://gist.github.com/nycdotnet/947025d922fa2af87d03 (You can run with the Visual Commander extension in VS 2013). I have seen Compile on Save not function correctly, but usually only when first having converted a file from JS (#1768)

nycdotnet avatar Feb 27 '15 04:02 nycdotnet

Just to recap we have two problems here:

  1. Compile-on-save fails to work occasionally, with the status bar simply saying "Item(s) Saved".
  2. Even when it works, there is at times a significant delay (5-8 seconds) before the save completes.

I am happy to report that the first problem appears to be solved in Visual Studio 2015 (CTP 5). In the fifteen days since the comment above there was only one occasion when it failed (which we may attribute to an external factor).

The second problem of the delay still remains. I believe this is due to the output file being locked. We have the following options set for a particular project:

  • Compile on save = ON
  • Combine JavaScript output into file = bin\$(ProjectName).js
  • Generate declaration files = ON
  • Number of .ts files in the project = 50

These options mean that in the bin directory the following files will be generated whenever any file is saved in the project:

  • bin\myproject.d.ts
  • bin\myproject.js

After a file is saved and compile-on-save completes (after the 8 second delay) the following gets printed out under the Workspace option of the output tab:

[Failure] The process cannot access the file 'c:\foo\bin\myproject.d.ts' because it is being used by another process.
[Failure] The process cannot access the file 'c:\foo\bin\MyProject.d.ts' because it is being used by another process.
[Failure] The process cannot access the file 'c:\foo\bin\MyProject.d.ts' because it is being used by another process.

(I'm not sure of the significance of the difference in casing for MyProject.d.ts but that is how it appears.)

In addition to solving this problem I suggest that the UX can be improved by printing compiling... on the status bar.

Edit:

The compiler option "Combine JavaScript output into file" doesn't appear to be the source of the problem, because in another project where we do not have this option set (we have redirect all output to bin set instead) I am still seeing the cannot access the file messages.

NoelAbrahams avatar Mar 10 '15 10:03 NoelAbrahams

Glad to hear the first issue is fixed. I am having a lot of the same issue now with what could be either a completely different issue or it could be related to issue 2 and I mentioned it before, about a file being locked when I compile. Well, recently (since last post) this has been happening a lot more - now it is on almost every rebuild of the solution, which is very strange. Here is the error I'm getting:

Error   11  Build: Could not write file 'C:/Workspace/****/scripts/bindings/binder.attr.any.js': Write to file failed.

I've obfuscated the path due to security but this seems to happen on random files, it seems like two threads are fighting over accessing or writing this file. Well, actually I'm assuming it is a file locking issue, but I can't think of what else might cause this.

To be clear though, this only seems to happen on a rebuild (not a normal build) it doesn't just happen to me, it happens to my colleagues as well (similar H/W, exact same S/W, exact same solution). When it does happen, I just have to build it again (not a rebuild) and it does not cause any error and the solution is built. But this is happening now probably about 8/10 rebuilds.

Anupheaus avatar Mar 10 '15 14:03 Anupheaus

@Anupheaus, looks like you have two projects that are referencing the file, and you are not using --out, and no project build dependency between the two. in this case, MSBuild will think they are independent and will build each on a different thread. this results in attempt to write the same file in two threads. To work around this, either specify build order, or add a reference between the two projects, or extract common pieces of your code (e.g: binder.attr.any.ts) to be built before the two projects consuming it, and reference the resulting .d.ts instead.

mhegazy avatar Mar 10 '15 17:03 mhegazy

Yep, I don't want to output everything to a single file so I'm not using --out.

The project build dependency might be the issue, but a build order is specified and used. I have many, many files and it would take me a long time to product a d.ts for each "common" file. I have a _references.d.ts file in each project that references each file within that project and each project that uses this project also references that _references.d.ts in their own _references.d.ts file. Okay, that wasn't very clear, but essentially, i think you are right, MSBuild is using two threads and clashing when writing the files.

I will look further into the build order but one is definitely specified for the solution and it does appear to be compiling each project one at a time within the solution.

Anupheaus avatar Mar 10 '15 17:03 Anupheaus

This issue should be solved in VS 2015. we have rewrote the infrastructure for compile-on-save. i do not think we can port these changes back to VS2013 at this point. so closing it as won't fix.

mhegazy avatar Jul 01 '15 20:07 mhegazy

I know this was closed, but for those that will still have to use VS2013...

I had the same problem and I could replicate it quite consistently. My project was originally migrated from a Visual Studio 2012 solution. If I 'run' the website and try to edit a .ts file, the compile on save didn't work.

I've found this thread on StackOverflow: http://stackoverflow.com/questions/17711542/typescript-compile-on-save-with-visual-studio-2013

The suggestion to check the 'ToolsVersion' of the .csproj and change if to "12.0" (the version used by VS2013) from "4.0" (used by VS2012) did the trick and the compile on save started to work again even when I'm debugging the website.

AGiorgetti avatar Jul 16 '15 14:07 AGiorgetti