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

Beautify on save adding extra character(s) to results #1213

Open
4 tasks done
chevalierc opened this issue Sep 22, 2016 · 34 comments
Open
4 tasks done

Beautify on save adding extra character(s) to results #1213

chevalierc opened this issue Sep 22, 2016 · 34 comments

Comments

@chevalierc
Copy link

chevalierc commented Sep 22, 2016

Description

I have been using this add on for a while. Occasionally it adds extra characters (usually closing brackets but occasionally whole words). It recently picked up. Changing from js-beautify to pretty-diff didn't stop the issues.

Expected Results

The beautified code should have looked like:

module.exports = {
    start: start
}

but produces

module.exports = {
    start: start
}
}
}

and ocassionaly stuff like this:

module.exports = {
    start: start
}
rt: start }
}

Steps to Reproduce

  1. Add code to Atom editor
  2. Run command Atom Beautify: Beautify Editor
  3. This beautified code does not look right!

Debug

Here is a link to the debug.md Gist: https://gist.github.com/anonymous/ccd9cc18973fc63eee2ade737be9f631

Checklist

  • I have tried uninstalling and reinstalling Atom Beautify to ensure it installed properly
  • I have reloaded (or restarted) Atom to ensure it is not a caching issue
  • Searched for existing Atom Beautify Issues at https://github.com/Glavin001/atom-beautify/issues
    so I know this is not a duplicate issue
  • Generated debugging information and added link for debug.md Gist to this issue
@prettydiff
Copy link
Collaborator

Would you mind generating the debug.md file? This really helps us to reproduce the problem so that we can direct targeted guidance to all concerned parties. https://github.com/Glavin001/atom-beautify/blob/master/docs/troubleshooting.md

@chevalierc
Copy link
Author

https://gist.github.com/anonymous/ccd9cc18973fc63eee2ade737be9f631

here you go. Its a good one, where its more then just adding brackets. Took a bit to happen on a file that didn't have corporate info

@prettydiff
Copy link
Collaborator

I am looking at this and I see extra characters at the bottom of the original code sample that don't appear related to the above code:

module.exports = {
    create: create
}
e
}

}

@chevalierc
Copy link
Author

are you asking me about the above? It does that alot..
Here is another example I just got...

module.exports = {
    upload_obj_to_db: upload_obj_to_db
}
j_to_db
}

@prettydiff
Copy link
Collaborator

@chevalierc When I looked at the debug.md file the original code sample already had the extra characters at the bottom. That source sample is code that exists before the beautification operation, which means the defect is elsewhere.

@chevalierc
Copy link
Author

module.exports = {
    upload_obj_to_db: upload_obj_to_db
}
_db: upload_obj_to_db
}
_db: upload_obj_to_db
}

@chevalierc
Copy link
Author

I'll create a new debug next time. I'm very confident it is from this package. I only have 2 3rd party packages installed on my two machines and the odd out package is different for each machine. Ill try disabling that package as well.

@Glavin001
Copy link
Owner

I recommend creating a small snippet of code that can reproduce this issue, generating the debug.md Gist and sharing it here, and also temporarily disabling Atom-Beautify, reloading Atom, and see if that fixes it.
It likely is Atom-Beautify. If so, we need a clean debug.md Gist and then we can tackle this issue.
Although I say "we" but it will probably be @prettydiff 😜

@chevalierc
Copy link
Author

First wanted to say I greatly appreciate your guys package. Has made writing code so much more enjoyable.

Did some debugging this morning. I was thinking maybe the NPM package nodemon was causing the issue but disabling it did not stop the symptoms.

Disabling beautify for an hour I didn't have an issues unfortunately. I usually have the issue pop up around 20 minutes to some times immediately following removing the past mistake.

I keep creating debugs and they have the extra characters in the original code.

Another piece of info, I have tried the different JS engines but they surprisingly causes identical issues. The issue has never append in HTML files.

I'll keep looking for pasterns in errors and try and capture some useful info for @prettydiff . Keeping the package regardless as it is very valuable.

@Glavin001
Copy link
Owner

First wanted to say I greatly appreciate your guys package. Has made writing code so much more enjoyable.

Awesome to hear 😄 . Thank you!

I usually have the issue pop up around 20 minutes to some times immediately following removing the past mistake.

Could you elaborate on this? Was there a typo/mistake in your previous code and when you saved Atom-Beautify executed and created the extra brace.

This could be a Beautify On Save only issue. If you enable Atom-Beautify, disable beautify_on_save for the language(s), and test around, can this be reproduced? I think it may be an issue with how beautify on save code is loading the file and passing it to beautifiers.
Even saving twice without delay in between could cause the issues shown above.

If removing Beautify On Save results this issue then I think it is something that will be addressed with Atom-Beautify v1 and when #1174 is complete. I am going to completely rewrite the Beautify On Save implementation and think more about how to prevent issues like this.

@Glavin001 Glavin001 added this to the v0.30.0 milestone Sep 28, 2016
@chevalierc
Copy link
Author

immediately following removing the past mistake.

After saving a file, it would add the extra characters. I would remove the extra character, then save, but It would again add the extra characters during the save.

If you enable Atom-Beautify, disable beautify_on_save for the language(s), and test around, can this be reproduced?

Will do. Hadn't thought of that.

@Glavin001 Glavin001 changed the title Extra characters created on JS code frequently Beautify on save adding extra character(s) to results Sep 30, 2016
@Glavin001 Glavin001 self-assigned this Sep 30, 2016
@Glavin001
Copy link
Owner

Both @chevalierc and @ikeamanual are experiencing extra characters being added after beautify on save. Unfortunately, I have 3 midterms coming up and a new work project, so my time is limited and also I have not yet reproduced this so I may not be much help, yet. I am confident we have narrowed it down to beautify on save being the culprit, now we need to figure out how to fix it.

If you could both change Logger Level to verbose and when it occurs look at the Atom Developer Console and check the Atom-Beautify logs. Unfortunately we can not see what is going on using the Help Debug Editor tool so instead we will have to look for helpful information within the logs. Let me know if you find anything useful and please submit a Pull Request if you develop a solution! Thank you!

@ikeamanual
Copy link

I attach the debug log here, and a screenshot. This is from duplicate issue #1236 which was closed.

To generate this log I started with correct html, an href with a " at the end.

  • clear console
  • Ctrl-S
  • remove the second "
  • Ctrl-S
    The extra > appears.

Note that the extra > does not appear in the log, only on the screen. But see the screenshot...

Also note that the extra > is actually saved to disk in my html file, I see it with "cat piet.html".

-1475339288479.txt
screenshot_2016-10-01_18-27-23

@Glavin001
Copy link
Owner

Note that the extra > does not appear in the log, only on the screen.

If you were to Ctrl-S (save and run beautify again) does the extra > appear now (since it is shown in the TextEditor)? I expect this to be Yes, however just want to be sure. If you do not change anything and repeated beautify on save, does that create even more extra >?

I'm fairly confident there is something wrong with how the beautification results are being set for the TextEditor: https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L111-L119
Note that the beautifiers are asynchronous and Atom's TextEditor saving implementation is synchronous so there could be some racing going on. See https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L558-L572 for details.

It could also be another package making changes to the TextEditor while Atom-Beautify is off processing and then comes back and attempts to update the already changed TextEditor. If this were the case, even something as simple as highlighting/selecting the all of the text (Ctrl-A / Command-A) in the TextEditor and then saving could cause problems if beautifyEntireFileOnSave option was set to false (which it is not: https://gist.github.com/anonymous/2ce03368bcc78bad7660e11ef186282f#package-settings ) and therefore the resulting beautified text was saved using setTextInBufferRange: https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L111-L115.
Maybe atom/atom#477 would help with this.

I attach the debug log here

Also is your Logger Level set to verbose? I only see debug logs. I expect to see even more logs than usual beautification because this is for beautify on save: https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L557-L564


This is a great start. Could you verify you are using verbose logging and try some more and longer tests and report back? Thanks!

@ikeamanual
Copy link

I changed the log level from debug to verbose.

Yes, if I do another Ctrl-S right after I first see the extra > then the > is show in the log.

Here are the actions for this log:

  • start with clean formatted valid html
  • remove the last "
  • Ctrl-S ( no extra > appears... code is beautified )
  • add the last "
  • Ctrl-S ( no extra > appears, code is formatted )
  • remove the last "
  • Ctrl-S ( extra > appears! )
  • Ctrl-S ( the one extra > remains. I now als see the extra > in the log file )

-1475398398097.txt

@ikeamanual
Copy link

ikeamanual commented Oct 2, 2016

Note that if I repeatedly do Ctrl-S, no extra > keep adding.
Also, if I add a newline between html tag and body tag, and do Ctrl-S, the code is beautified ( extra newline removed ) but no extra > is added.

To get the beautifier to add extra >>>> I first have to create some invalid html. Adding a extra newline is not invalid html.

So if I fix the html by adding the last ", then do Ctrl-S, then create invalid html by removing the last ", then do Ctrl-S, then I get a second extra > at the end of my file.

So the bug seems to be trigged by invalid html, not just removing spacing...

@prettydiff
Copy link
Collaborator

@ikeamanual If you change beautifiers and attempt to repeat this experiment do you continue to get the extra characters? Both issues used the same beautifier even if the languages are different.

@ikeamanual
Copy link

If I configure Pretty Diff and do the same thing the code is actually chopped off behind the <a.
see screendump and log file.

screenshot_2016-10-02_18-04-30
-1475424185478.txt

@ikeamanual
Copy link

ikeamanual commented Oct 2, 2016

Note that I cannot reproduce the extra > on a Windows 10 laptop.

@prettydiff
Copy link
Collaborator

Yep, that is a reproducible limitation of Pretty Diff. This suggests the problem could be related to the specific beautifier or its relationship to Atom Beautify.

@chevalierc
Copy link
Author

Just stopping into say I have been using with beautify on-save for the past week and haven't had any of the relevant issues. Ill read the rest of this thread soon, to see how else I can help?

@dalelotts
Copy link

I have two classrooms of 30 that have had to disable it due to a number of issues, one of which is extra characters being added.

@prettydiff
Copy link
Collaborator

@dalelotts Do you know which beautifiers your students where using when the extra characters were generated?

@dalelotts
Copy link

@prettydiff All are set to the default. JS Beautify.

@gunderson
Copy link

+1 also experiencing this intermittently using jsbeautify

In my attempt to fix this, I have now broken beautify on save for JS files, other files still beautify on save :(

@Foo42
Copy link

Foo42 commented Nov 23, 2016

I've had to disable this plugin for the same reason. I've been using with with js and at times it adds a block around 10 lines long at the end on each save!

@jeffjg
Copy link

jeffjg commented Jan 2, 2017

+1. Also happening for me.

@Glavin001
Copy link
Owner

I do not fully understand why disabling the plugin is required course of action, as this issue appears only with Beautify On Save enabled -- correct me if I am wrong -- and Beautify On Save can simply be disabled -- and is disabled by default -- therefore still providing value using quick keyboard shortcuts.

Regardless, I do want to resolve this bug.

Questions for anyone able to reproduce this bug:

  • @ikeamanual suggested this happens from invalid code. Can each you of please provide sample code of what is breaking?
  • Confirm it only occurs with beautify on save. I think it is an Atom-Beautify bug not with a specific beautifier.
    • Does this occur when you save multiple times, possibly too quickly?
  • Do you have any code selected when you save?
  • Is it always at the end of the file the extra character(s) are added?
    • Is it always the last part of the file which is added?

Please provide a debug.md Gist so we know your configurations of Atom-Beautify as well. Since it appears the issue is beautify on save related, I do not expect to be able to use this debug.md to see a reproducible case, however it may still provide some insights into common configurations.

Ideally, someone who is able to reproduce this issue will debug a little and hopefully be able to submit a Pull Request solving this issue. I think the applicable code to start looking at is https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L115-L119
It could be something like selectedBufferRange is set and the beautified code is replacing only a portion of the file instead of the entire file. Or it could be a race condition as a result of Atom's on save event only allowing synchronous handlers and Atom-Beautify being asynchronous. See https://github.com/Glavin001/atom-beautify/blob/master/src/beautify.coffee#L533-L547 for more details.

@Simon-Payne
Copy link

I get this issue all the time with atom-beautify and it's quite annoying.

@prettydiff
Copy link
Collaborator

@Simon-Payne Could you provide us with your debug.md when this happens. We need evidence of the problem in order to properly investigate and solve the defect.

@IderAghbal
Copy link

I'm having this problem too.. with both html and js files. (never had it with php or anything else).
It just happens randomly and breaks my code.
Disabling beautify on save for now.

@york-xtrem
Copy link

I have disabled "Beautify on save" to continue using this package.
I don't think it's a problem with any beautifier or Whitespace package. To me it adds a new line at the end and erases it many times. Until the undo stack becomes unusable.

@Glavin001 Glavin001 modified the milestones: v0.31.0, v0.33.0 (Next) Mar 2, 2018
@stevenzeck
Copy link
Contributor

Has this happened to anyone in the last few months? If so, can you provide further details.

@truglodite
Copy link

truglodite commented May 29, 2019

Yep, tonight I just scrambled my c code up good using atom beautify on save (with clang). It will be fun to sort out since I added a big chunk since my last commit. This critical issue has been open long enough that you might assume it was fixed but someone just forgot to hit the close button. Must be a hard one to sort out. Anyhow... disabling and moving on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests