VSCode keeps deleting my import line

74 views
Skip to first unread message

cub...@gmail.com

unread,
Nov 18, 2025, 1:48:55 PM (19 hours ago) Nov 18
to golang-nuts
Hi.   I type 


and save, and VSCode deletes my line. 

I know why it's doing it.  I haven't yet used the library in my routine, and I have yet added the library to my go.mod file. 

But I want it to inform me of the problem, and leave it to me to fix it!  Not change my code without my permission.  (My Prius yanks the wheel when it thinks I'm about to hit something.  Not appreciated.) 

Is there some set of settings that will accomplish this?  I hate to abandon VSCode.  The Go team has put in a lot of effort to make it the best place to write Go code.  Thanks. 

Keywords:   VSCode autoformat format formatonsave 

cub...@gmail.com

unread,
Nov 18, 2025, 3:53:28 PM (17 hours ago) Nov 18
to golang-nuts
I got a couple of good ideas from Will F. for coping with it.   Also, I tried Vim-Go.   Then I bit the bullet and bought Goland (not too pricey for a personal license). 

Goland had the same behavior (arghh) but after a long conversation with their chatbot, it came down to this: 

1) Go to Actions on Save in the settings, and disable two actions, Format Code and Optimize Imports.   2) When I want those things to happen, press option-cmd-L for Format Code, or ctrl-option-O for Optimize Imports. 

That solves it for me, for now.   Maybe writing it down will help someone else. 

David Finkel

unread,
Nov 18, 2025, 4:41:48 PM (16 hours ago) Nov 18
to cub...@gmail.com, golang-nuts
On Tue, Nov 18, 2025 at 3:55 PM cub...@gmail.com <cub...@gmail.com> wrote:
I got a couple of good ideas from Will F. for coping with it.   Also, I tried Vim-Go.   Then I bit the bullet and bought Goland (not too pricey for a personal license). 

FWIW, with vim-go, you should be able to add this to your vimrc to disable auto-imports (or neovim's nvim/init.nvim):
let go_imports_autosave = 0


Goland had the same behavior (arghh) but after a long conversation with their chatbot, it came down to this: 

1) Go to Actions on Save in the settings, and disable two actions, Format Code and Optimize Imports.   2) When I want those things to happen, press option-cmd-L for Format Code, or ctrl-option-O for Optimize Imports. 

That solves it for me, for now.   Maybe writing it down will help someone else. 

On Tuesday, November 18, 2025 at 12:48:55 PM UTC-6 cub...@gmail.com wrote:
Hi.   I type 


and save, and VSCode deletes my line. 

I know why it's doing it.  I haven't yet used the library in my routine, and I have yet added the library to my go.mod file. 

But I want it to inform me of the problem, and leave it to me to fix it!  Not change my code without my permission.  (My Prius yanks the wheel when it thinks I'm about to hit something.  Not appreciated.) 

Is there some set of settings that will accomplish this?  I hate to abandon VSCode.  The Go team has put in a lot of effort to make it the best place to write Go code.  Thanks. 

Keywords:   VSCode autoformat format formatonsave 

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/8032f71e-d39a-4ce2-8d73-ed5c4dede9ean%40googlegroups.com.

Stephen Illingworth

unread,
Nov 18, 2025, 4:52:55 PM (16 hours ago) Nov 18
to golang-nuts
The other vim option is to use gopls directly through one of the lsp plugins. A really good lsp plugin is https://github.com/yegappan/lsp

With the basic gopls setup described in the plugin's readme, saving a file will not cause the imports to be reorganised, which fortunately is what the OP wants.

If you do want the effect of the goimports command every time save then you can do that too. I have this in my vimrc:

au Filetype go command! GoFormat :LspFormat
au Filetype go command! GoImports :LspCodeAction /Organize\sImports

function! s:goSave()
execute('GoFormat')
execute('GoImports')
endfunction

au BufWritePre *.go silent call s:goSave()

I'd be interested in hearing other people's vim/lsp/gopls setups.

Robert Engels

unread,
Nov 18, 2025, 9:47:47 PM (11 hours ago) Nov 18
to Stephen Illingworth, golang-nuts

You can do the same in VSCode. 

    {
        "editor.codeActionsOnSave": {
            "source.organizeImports": "never"
        }
    }



On Nov 18, 2025, at 3:58 PM, Stephen Illingworth <stephen.i...@gmail.com> wrote:



Brian Candler

unread,
4:18 AM (4 hours ago) 4:18 AM
to golang-nuts
I don't know anything about fancy editors, but I note that go.dev/play/ does the same (maybe for same underlying reason).

You can prevent it by writing:


(although you then have to remove the underscore later when you actually want to use the module)

Stephen Illingworth

unread,
4:34 AM (4 hours ago) 4:34 AM
to golang-nuts
Having an unused import is a compilation error so it's generally good that editors do this (by calling goimports or using the gopls code action). Using the underscore notation is a good tip though.
Reply all
Reply to author
Forward
0 new messages