Sublime Forum

Ensure new line at end of file on save setting is not being honored

#1

I saw a very old thread that asks the same question ([Bug] "ensure_newline_at_eof_on_save" option not honored), but it’s not answered and is super old. I recently noticed that a new line is being added at the end of every file on save, even though I have not configured it to do so. Initially I didn’t have the ensure_newline_at_eof_on_save preference configured, so it should have defaulted to false. But I tried adding "ensure_newline_at_eof_on_save": false to my preferences file, still did nothing. After I save any file, then do super+z to undo, the status bar shows “Undo: Ensure Newline At Eof”, so that tells it is directly related to this setting.

Anybody else aware of this or know how I can fix this asap? That extra line is breaking some tests for me unfortunately in a bunch of files…

0 Likes

#2

Does it happen for every type of file or only of files of a particular type (plain text, HTML, etc)?

0 Likes

#3

I just ran a quick test on js, html, css, py, md, and txt files… it happens with all of those.

0 Likes

#4

The code for this comes from the Default package and is in the trim_trailing_white_space.py plugin file, where the command is triggered by an event listener that listens for files about to be saved:

class EnsureNewlineAtEofCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        if self.view.size() > 0 and self.view.substr(self.view.size() - 1) != '\n':
            self.view.insert(edit, self.view.size(), "\n")


class EnsureNewlineAtEof(sublime_plugin.EventListener):
    def on_pre_save(self, view):
        if view.settings().get("ensure_newline_at_eof_on_save") is True:
            if view.size() > 0 and view.substr(view.size() - 1) != '\n':
                view.run_command("ensure_newline_at_eof")

It’s definitely checking that the setting is set to True prior to trying to run the command.

As a test, you can open up the Sublime console with View > Show console, then enter view.settings().get("ensure_newline_at_eof_on_save") in the input area and see what it says.

In a clean Sublime, I get this result:

>>> view.settings().get("ensure_newline_at_eof_on_save")
False

If you see False, the first thing I would suspect is that another plugin somewhere (either in your User package or in a third party package) has an event listener similar to the default one, but it’s always invoking the command instead of respecting the setting. If that’s the case you would have to temporarily revert to a freshly installed state (check if the problem still exists) and then slowly re-enable your third party packages to see if one of them causes it to start happening.

If you see True, then something somewhere is forcing the setting to be True even though your preferences are setting it to False. My first guess would be a typo, but since was happening before you tried to disable it that seems unlikely.

Syntax specific settings and project specific settings can alter settings from the defaults. You should check the syntax specific settings for one of the affected file types to see, but since there are so many of them affected that seems like an unlikely candidate.

If you’re using a project, I would check the project settings to see if something perhaps put the setting in there.

1 Like

#5

@OdatNurd Thank you for your help!

It is definitely returning True when I check my view settings. It’s not anything project specific, nor do I see anywhere it’s syntax specific (which as you mentioned seems unlikely given so many syntaxes affected). So I suspect it’s a plugin, but I am going to do some more digging when I catch some time. I will update here what I find.

Thanks again!

0 Likes

#6

I had the same problem. I enabled debug mode for Package manager, then in the console, I noticed that Sublime could not load my user settings. This was due to a broken symlink. In other words, it was my fault.

I hope this helps.

0 Likes