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

Editing in Visual Studio and watch does not work.

Open rippo opened this issue 11 years ago • 17 comments
trafficstars

I have a weird problem where if I use Visual Studio to edit my ts files the watch does not see the file changes. If I use say Notepad++ and edit a ts file the watch kicks in and compiles the changes.

This is my gruntfile:-

module.exports = function (grunt) {
    "use strict";
    grunt.initConfig({

        ts: {
            options: {
                sourcemap: true
            },
            build: {
                src: ['GruntTesting/Scripts/ts/**/*.ts'],
                //outDir: 'GruntTesting/app',
                out: 'GruntTesting/app/site.js',
                watch: 'GruntTesting/Scripts/ts'
            }
        }
    });

    grunt.loadNpmTasks("grunt-ts");
    grunt.registerTask("default", ["ts:build"]);

};

When editing in Visual Studio I get this (does not work):-

capture01

When editing in Notepad++ I get this (does work):

capture02

Maybe its some setting in VS I need or some other oddity, can anyone shed light on this?

rippo avatar Aug 13 '14 08:08 rippo

If I use say Notepad++ and edit a ts file the watch kicks in and compiles the changes

We have noticed this. When this happens, if you actually open the file on the file system you will notice it doesn't contain your changes. This was happening in VS 2012 for us.

Also for many of us VS 2013 is failing to reload the file from the file system once it changes externally (e.g. by transformers).

Nothing we can do about it. If you wait a bit (like 5 seconds) VS does buffer the changes to disk. And for loading the external changes into VS closing and reopening the file tends to help (sadly)

basarat avatar Aug 13 '14 09:08 basarat

Is this a Visual Studio bug? I've never observed this with other file types in VS. (I can't recall ever having observed it with TypeScript either, though.)

nycdotnet avatar Aug 13 '14 14:08 nycdotnet

Sorry but I don't see this behaviour at all.

I have test1.ts open in both VS and Notepad++ at the same time and I modify the file in VS2013 the changes are picked up straight away in Notepad++. Grunt still sits there so something is amiss.

capture

rippo avatar Aug 13 '14 14:08 rippo

Also for many of us VS 2013 is failing to reload the file from the file system once it changes externally (e.g. by transformers).

@nycdotnet :

image

basarat avatar Aug 13 '14 23:08 basarat

Grunt still sits there so something is amiss.

@rippo Just curious, are you running VS as "Administrator"? (perhaps you have it setup automatically for something like IIS debugging).

PS: This stopped happening for us when we moved to 2013 for some reason.

basarat avatar Aug 13 '14 23:08 basarat

@basarat

Just curious, are you running VS as "Administrator"?

No running as normal. All other editors like sublime, webstorm pick up the fact that that needs to be reloaded when I save in VS. Its only grunt that doesn't pick this up.

rippo avatar Aug 14 '14 07:08 rippo

Its only grunt that doesn't pick this up.

you can put a console.log(arguments) here : https://github.com/grunt-ts/grunt-ts/blob/master/tasks/ts.js#L387 i.e.

function handleFileEvent(filepath, displaystr, addedOrChanged) {
                    console.log(arguments);
                    if (typeof addedOrChanged === "undefined") { addedOrChanged = false; }

to help us know if it is something in our code or 'chokidar'. I do appreciate your time. Sorry that I cannot reproduce it reliably.

basarat avatar Aug 14 '14 09:08 basarat

Hi thanks for your effort going into this, this is what I see in console when I change a ts file in VS:-

capture

So it looks like it is picking something up, its just not changing the JS file in GruntTesting/app/site.js . Strange

rippo avatar Aug 14 '14 09:08 rippo

oh oh. This means we have a bug in our code. Sorry. Can you uncomment this https://github.com/grunt-ts/grunt-ts/blob/master/tasks/ts.js#L395-L399 to see if we are ignoring it by any chance i.e. final code

                    // Do not run if just ran, behaviour same as grunt-watch
                    // These are the files our run modified
                    if ((new Date().getTime() - lastCompile) <= 100) {
                        // Uncomment for debugging which files were ignored
                        grunt.log.writeln((' ///'  + ' >>' + filepath).grey); // !!! Uncomment this 
                        return;
                    }

basarat avatar Aug 14 '14 09:08 basarat

aha, it works now so it does look like a bug....

capture

rippo avatar Aug 14 '14 10:08 rippo

So you press Ctrl+S once and we get three events?

basarat avatar Aug 14 '14 10:08 basarat

Yes this is a single CTRL + S , will double check that statement

Yes tried a few times, always three events. On a SSD drive if that means anything!

rippo avatar Aug 14 '14 10:08 rippo

I see the bug now. If something where to raise these in a row :

on('change', function (path) {
                    handleFileEvent(path, '### changed ', true);

                    // Reset the time for last compile call
                    lastCompile = new Date().getTime();
                })

and lastCompile gets reset while we are still in handleFileEvent and haven't hit this yet:

// Do not run if just ran, behaviour same as grunt-watch
                    // These are the files our run modified
                    if ((new Date().getTime() - lastCompile) <= 100) {

so lastCompile gets reset externally (by the event handler) and this handleFileEvent aborts as well.

@rippo thanks for your time. We know how to reproduce it (some way to ctrl+s in under 100ms ;)). Sorry my bad. Will fix it when I see a clear solution. PRs appreciated.

basarat avatar Aug 14 '14 10:08 basarat

Perfect, least I have a way to use grunt now, look forward to a fix :) If you need to run past anything by me I can be your guinea pig. Thanks

rippo avatar Aug 14 '14 10:08 rippo

Has this issue been resolved? I'm currently experiencing this issue but I've found some work arounds.

I removed the watch from my grunt-ts config and swapped it out for grunt-contrib-watch which seemed to work.

After that I switched back to a grunt-ts watch but with a fresh checkout of my project and it started picking up changes and compiling again.

I also commented out the if statement that checks the last compiled time and it started picking up changes on the offending checkout, but I also noticed it was compiling it 3 to 4 times every time I changed a file.

Preferably don't want to do a fresh checkout every time this starts happening so a fix for this would be appreciated.

duaned avatar Jun 23 '15 09:06 duaned

Sorry we haven't fixed this yet. That sounds like the behavior that @rippo was experiencing.

@basarat do you think a simple debounce would be good enough to work around this?

nycdotnet avatar Jun 23 '15 13:06 nycdotnet

do you think a simple debounce would be good enough to work around this?

Probably not. Simple debounce will only call with last change argument. We probably want to call with all changes. That said, considering all the edge cases is out of scope, so feel free to give it a go with a simple debounce :+1:

basarat avatar Jun 24 '15 00:06 basarat