From fff9897ce3f246add656ebf28bbcc843900431f1 Mon Sep 17 00:00:00 2001 From: Puneet Dixit Date: Sun, 17 May 2026 21:48:24 +0530 Subject: [PATCH] fix(startup): emitting useless OptionSet #39830 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem: During startup, we manually trigger a useless and misleading `OptionSet` event, which doesn't set `v:option_*` values (this is a limitation of `nvim_exec_autocmds`). https://github.com/neovim/neovim/blob/ad4bc2d90c5109c24bf6f0119ad31276f1518c6f/runtime/lua/vim/_core/defaults.lua#L939. Solution: The `nvim_exec_autocmds('OptionSet',…)` call does not serve any purpose since 5cbb9d613bf6d8983b5effd7e20343a4ef497c06, so just drop it. --- runtime/doc/news.txt | 2 ++ runtime/doc/options.txt | 2 +- runtime/lua/vim/_core/defaults.lua | 14 -------------- runtime/lua/vim/_meta/options.gen.lua | 2 +- src/nvim/options.lua | 2 +- test/functional/terminal/tui_spec.lua | 23 ++++++++++++++--------- 6 files changed, 19 insertions(+), 26 deletions(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 819b4dc40d..c2bcb61da3 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -270,6 +270,8 @@ CHANGED FEATURES *news-changed* These existing features changed their behavior. +• |OptionSet| is no longer triggered during startup by automatic + |'background'| detection. • |:Open| with no arguments uses the current file. • The "buffer" key was renamed to "buf" in these functions (but the old name "buffer" is still accepted, for backwards compatibility): diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index ddccadd63d..b066b36588 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -851,7 +851,7 @@ A jump table for the options with a short description can be found at |Q_op|. global When set to "dark" or "light", adjusts the default color groups for that background type. The |TUI| or other UI sets this on startup - (triggering |OptionSet|) if it can detect the background color. + if it can detect the background color. This option does NOT change the background color, it tells Nvim what the "inherited" (terminal/GUI) background looks like. diff --git a/runtime/lua/vim/_core/defaults.lua b/runtime/lua/vim/_core/defaults.lua index 31bc56a4db..4490d315af 100644 --- a/runtime/lua/vim/_core/defaults.lua +++ b/runtime/lua/vim/_core/defaults.lua @@ -946,20 +946,6 @@ do local luminance = (0.299 * rr) + (0.587 * gg) + (0.114 * bb) local bg = luminance < 0.5 and 'dark' or 'light' vim.api.nvim_set_option_value('background', bg, {}) - - -- Ensure OptionSet still triggers when we set the background during startup - if vim.v.vim_did_enter == 0 then - vim.api.nvim_create_autocmd('VimEnter', { - group = group, - once = true, - nested = true, - callback = function() - vim.api.nvim_exec_autocmds('OptionSet', { - pattern = 'background', - }) - end, - }) - end end end end) diff --git a/runtime/lua/vim/_meta/options.gen.lua b/runtime/lua/vim/_meta/options.gen.lua index 32f31bfdf7..9bba00501d 100644 --- a/runtime/lua/vim/_meta/options.gen.lua +++ b/runtime/lua/vim/_meta/options.gen.lua @@ -224,7 +224,7 @@ vim.go.awa = vim.go.autowriteall --- When set to "dark" or "light", adjusts the default color groups for --- that background type. The `TUI` or other UI sets this on startup ---- (triggering `OptionSet`) if it can detect the background color. +--- if it can detect the background color. --- --- This option does NOT change the background color, it tells Nvim what --- the "inherited" (terminal/GUI) background looks like. diff --git a/src/nvim/options.lua b/src/nvim/options.lua index e815f0639f..c9c0e99f91 100644 --- a/src/nvim/options.lua +++ b/src/nvim/options.lua @@ -366,7 +366,7 @@ local options = { desc = [=[ When set to "dark" or "light", adjusts the default color groups for that background type. The |TUI| or other UI sets this on startup - (triggering |OptionSet|) if it can detect the background color. + if it can detect the background color. This option does NOT change the background color, it tells Nvim what the "inherited" (terminal/GUI) background looks like. diff --git a/test/functional/terminal/tui_spec.lua b/test/functional/terminal/tui_spec.lua index 8366f02fb5..e93288d2f1 100644 --- a/test/functional/terminal/tui_spec.lua +++ b/test/functional/terminal/tui_spec.lua @@ -4290,23 +4290,28 @@ describe('TUI bg color', function() end) end) - it('triggers OptionSet from automatic background processing', function() + it('does not trigger OptionSet from automatic background processing', function() + command('set background=light') + local child_server = new_pipename() local screen = tt.setup_child_nvim({ '--clean', + '--listen', + child_server, '--cmd', 'colorscheme vim', '--cmd', 'set noswapfile', '-c', - 'autocmd OptionSet background echo "did OptionSet, yay!"', + [[let g:background_optionset = 0]], + '-c', + [[autocmd OptionSet background let g:background_optionset += 1]], }) - screen:expect([[ - ^ | - {5:~} |*3 - {3:[No Name] 0,0-1 All}| - did OptionSet, yay! | - {5:-- TERMINAL --} | - ]]) + screen:expect({ any = '%[No Name%]' }) + local child_session = n.connect(child_server) + retry(nil, nil, function() + eq({ true, 'light' }, { child_session:request('nvim_eval', '&background') }) + end) + eq({ true, 0 }, { child_session:request('nvim_eval', 'g:background_optionset') }) end) it('sends theme update notifications when background changes #31652', function()