Importing custom vscode snippets stopped working
Running
:lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" } })
No longer works. This snippet directory has always worked, and previously I had adjusted the runtime path to include it, but I prefer this method (less messy). However, it seems to have stopped loading the snippets, even when called directly from a file that would use them, i.e. t.tex and all my snippets are for tex. For reference
package.json
{
"name": "custom-snippets",
"contributes": {
"snippets": [
{
"language": [
"tex",
"markdown"
],
"path": "tex-math.json"
},
{
"language": [
"tex",
"markdown"
],
"path": "tex-stat.json"
},
{
"language": [
"markdown"
],
"path": "markdown.json"
}
]
}
}
And one of these as an example, tex-stat.json:
{
"probability": {
"prefix": "pr",
"body": "\\Pr($1)$0"
},
"expectation": {
"prefix": "E",
"body": "\\mathbb{E}($1)$0"
},
"measurable events": {
"prefix": "me",
"body": "\\mathcal{L}"
},
"independent": {
"prefix": "indep",
"body": "\\perp"
},
"event space": {
"prefix": "es",
"body": "\\Omega"
},
"null set": {
"prefix": ["null", "ns"],
"body": "\\varnothing"
}
}
These are all top level of the ~/.config/snippets directory.
Weird, it works fine for me (with friendly-snippets)...
Could you clone friendly-snippets (not necessarily to rtp) and see whether they load?
I already load friendly snippets, and it works just fine
Here is part of my config where I load luasnip:
{
"L3MON4D3/LuaSnip",
wants = "friendly-snippets",
after = "nvim-cmp",
config = function()
local luasnip = require("luasnip")
luasnip.config.set_config({
history = true,
updateevents = "TextChanged,TextChangedI",
})
require("luasnip.loaders.from_vscode").lazy_load({ paths = { "~/.config/snippets" } })
end,
},
I already load friendly snippets, and it works just fine
By directly specifying paths or just via rtp?
...I'm actually not sure what the difference is..I believe the above config says it wants friendly-snippets, so I'm not sure what packer machinery is involved, but I assume it loads it? I'm not referencing friendly-snippets anywhere else fwiw
Ohh, IIUC wants only specifies that some plugin depends on another one, the snippets have to be added to luasnip via eg. load.
Are the snippets from friendly-snippets present in the output of :LuaSnipListAvailable?
they appear to be? that generates quite a long list; how can I confirm the source of the snippets?
Not :sweat_smile: The best way to debug that, right now, would be to just add some snippet to the different sources and check whether it's expandable.
That aside, running
:lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" } }) from a buffer for a tex file should load the snippets from that directory that are for tex, no? Isn't the simplest way just to call that command manually and see if they show up?
Almost, the filetypes to load can be passed to load():
load({
paths = {"..."},
include = {"ft1", "ft2"},
exclude = {"notft3", "notft4"}
})
So, yeah, remove the other load calls, load tex-snippets and check for presence :+1:
yea, this isn't working when I call directly from a buffer in t.tex:
:lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.config/snippets" }, include={"tex"} })
Hmmmmmm, how about loading from friendly-snippets that way? eg. :lua require("luasnip.loaders.from_vscode").load({ paths = { "~/.local/share/nvim/site/pack/packer/start/friendly-snippets/" }, include={"tex"} })
(Not sure if your path for friendly-snippets is exactly the same as mine, but something like that should work)
Looks like there is an issue on NvChad detailing this too and it might be removed because it's not working? https://github.com/NvChad/NvChad/issues/719 Can you call load multiple times?
Generally yes, but that could be prone to race-conditions as we utilize the async-file-loading from luv and append snippets to ls.snippets in the callbacks, which could lead to some snippets missing from the final tables.
Not sure how all that plays out actually, I'll be able to look into it some more in the next few days.
There definitely seems to be some non-determinism going on here. Sometimes it works, sometimes it doesn't. Below are two tex files in the same directory:

You can see that the one on the left is letting me select my custom al snippet, while it doesn't even appear on the right.
Oh, could you verify snippet-availability using :LuaSnipListAvailable? That way there's fewer moving pieces (-cmp_luasnip) where the error could be introduced
Could you try if the deterministic_load-branch fixes your issues?
I'm in the middle of midterms but will try after; how do I specify a branch in packer?
Thank you :+1:
You can specify the branch in the use-call, eg. use({"L3MON4D3/Luasnip", branch="deterministic_load"}).
Not sure if that plays nice with NvChad though.. (are you using NvChad?)
I'm having the same issue and am unable to load these snippets anymore in my JS/TS files: https://github.com/dsznajder/vscode-react-javascript-snippets
No config changes on my end just stopped working recently. Tried the deterministic load branch but no luck here.
Could you post the generated snippets? Running the install-function from that repo results in an empty table for me.
Same issue here, I'm the one who created the issue on NvChad https://github.com/NvChad/NvChad/issues/719 I'm using Packer, and now I'm not using the preinstallated LuaSnip from NvChad, because I disabled it and tried to do it myself from scratch.
My installation:
use {
"L3MON4D3/LuaSnip",
event = "InsertEnter",
after = "nvim-cmp",
config = function()
local luasnip = require("luasnip")
luasnip.config.set_config({
history = true,
updateevents = "TextChanged,TextChangedI",
})
require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})
end,
}
When I open the file that I want to edit and I enter INSERT mode, no snippets are shown and if i run LuaSnipListAvailablethere is an empty map.
The strange thing is that if I run:
:lua require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})
From the opened file, the snippets are loaded.
Could you use the other branch and call print(vim.inspect(luasnip.available())) (prints the same as :LuaSnipListAvailable) after the load?
The map is still empty
Weird.. Could you post a minimal config that reproduces the issue?
Here it is my .dotfiles https://github.com/nank1ro/.dotfiles
If you want a miminal reproduction, you can follow this: Copy this folder https://github.com/nank1ro/.dotfiles/tree/main/nvim/lua/custom/my_snippets somewhere. To install I use Packer:
use {
"L3MON4D3/LuaSnip",
branch = "deterministic_load",
event = "InsertEnter",
after = "nvim-cmp",
config = function()
local luasnip = require("luasnip")
luasnip.config.set_config({
history = true,
updateevents = "TextChanged,TextChangedI",
})
require("luasnip/loaders/from_vscode").load({ paths = { "/Users/alex/.config/nvim/lua/custom/my_snippets/" }})
end
}
Replace /Users/alex/.config/nvim/lua/custom/my_snippets/ with your path.
Then I open a Flutter project, If you haven't flutter installed you can see https://docs.flutter.dev/get-started/install
Then run
flutter create myapp
cd myapp
nvim lib/main.dart
Now enter Insert mode and start writing stw and no snippets are shown.
stw is a shortcut for a snippet
I cannot reproduce the issue, the stw-snippet loads correctly for me :/
Please try creating a file that, if sourced via nvim -u somefile, reproduces the issue (the snippet-directory can be downloaded separately, that's no problem).
This should be a good starting point.
Something that solved my problem was that the file type seems to change in nvim before and after a save. nvim t.tex and then running :set filetype? returns plaintex. However, after saving and reopening the file, this returns tex, and all my snippets were defined only for tex -- adding plaintex to the snippets worked...although it's still weird that filetype inference changes..
I cannot reproduce the issue, the
stw-snippet loads correctly for me :/ Please try creating a file that, if sourced vianvim -u somefile, reproduces the issue (the snippet-directory can be downloaded separately, that's no problem). This should be a good starting point.
Finally it started working, after upgrading NvChad all seems fine. I don't know what was the problem, but now I can see my custom snippets.