Replies: 13 comments 11 replies
-
|
I could be mistaken, but the linked documentation sounds as if envFile is a property that some of the debug adapters already support, not as if it's something that vscode handles in a special way? Currently nvim-dap passes all properties 1:1 to the debug adapter, so if a debug adapter supports such a property it should already work |
Beta Was this translation helpful? Give feedback.
-
|
FYI, the go extension from vs-code. envFile seems to be a property defined by vscode extension (not only go, but also nodejs, python etc) instead of a standard dap property. |
Beta Was this translation helpful? Give feedback.
-
|
@mfussenegger @ray-x thanks for the research. Are there some workarounds to working with .env files in nvim-dap? |
Beta Was this translation helpful? Give feedback.
-
|
seems you have to write lua code to load .env file line by line into |
Beta Was this translation helpful? Give feedback.
-
|
Alright, got it. Thanks for helping guys! |
Beta Was this translation helpful? Give feedback.
-
|
It would be possible to support |
Beta Was this translation helpful? Give feedback.
-
|
@niklod or @ray-x would you mind sketching out what this code might look like? I'm using the adapter defined here and I'm having difficulty figuring out how I should modify it using the template from the -- :h dap-adapter
local adapter = {
type = 'server';
host = '127.0.0.1';
port = 8080;
enrich_config = function(config, on_config)
local final_config = vim.deepcopy(config)
final_config.extra_property = 'This got injected by the adapter'
on_config(final_config)
end;
} |
Beta Was this translation helpful? Give feedback.
-
|
If you read your .env into a lua list with you function env_read e.g. envFileElem = env_read('.env')
vim.tbl_extend(final_config.env, envFileElem)
|
Beta Was this translation helpful? Give feedback.
-
|
@benortiz honestly I have the same problem. I tried configure it like this: But it doesn't work |
Beta Was this translation helpful? Give feedback.
-
|
Turning this into a discussion because this is a debug adapter/extension issue. |
Beta Was this translation helpful? Give feedback.
-
|
Any movement on this? I'm at a point that this would be super helpful and would be willing to contribute if needed |
Beta Was this translation helpful? Give feedback.
-
|
So at the very least it appears that if you expose var_placeholders, a simple function could be written client side to expand the envFile to env vars in the configuration in memory like so: dap = require('dap')
-- Parse out envFiles
for type, _ in pairs(dap.configurations) do
for _, config in pairs(dap.configurations[type]) do
if config.envFile then
local filePath = config.envFile
for key, fn in pairs(dap.get_placeholders()) do
filePath = filePath:gsub(key, fn)
end
for line in io.lines(filePath) do
local words = {}
for word in string.gmatch(line, "[a-zA-Z0-9_]+") do
table.insert(words, word)
end
if not config.env then
config.env = {}
end
config.env[words[1]] = words[2]
end
end
end
endA more ideal situation would be to have this function within dap.lua in this plugin and have it execute and expand the env file when launching the individual configuration. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
|
Also my final working solution: enrich_config = function(finalConfig, on_config)
local final_config = vim.deepcopy(finalConfig)
-- Placeholder expansion for launch directives
local placeholders = {
["${file}"] = function(_)
return vim.fn.expand("%:p")
end,
["${fileBasename}"] = function(_)
return vim.fn.expand("%:t")
end,
["${fileBasenameNoExtension}"] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:t"), ":r")
end,
["${fileDirname}"] = function(_)
return vim.fn.expand("%:p:h")
end,
["${fileExtname}"] = function(_)
return vim.fn.expand("%:e")
end,
["${relativeFile}"] = function(_)
return vim.fn.expand("%:.")
end,
["${relativeFileDirname}"] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:.:h"), ":r")
end,
["${workspaceFolder}"] = function(_)
return vim.fn.getcwd()
end,
["${workspaceFolderBasename}"] = function(_)
return vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
end,
["${env:([%w_]+)}"] = function(match)
return os.getenv(match) or ""
end,
}
if final_config.envFile then
local filePath = final_config.envFile
for key, fn in pairs(placeholders) do
filePath = filePath:gsub(key, fn)
end
for line in io.lines(filePath) do
local words = {}
for word in string.gmatch(line, "[^=]+") do
table.insert(words, word)
end
if not final_config.env then
final_config.env = {}
end
final_config.env[words[1]] = words[2]
end
end
on_config(final_config)
end,
adapter initialization example: dap.adapters.go = {
type = "server",
port = 38697,
executable = {
command = "dlv",
args = { "dap", "-l", "127.0.0.1:38697" },
},
enrich_config = function(finalConfig, on_config)
local final_config = vim.deepcopy(finalConfig)
-- Placeholder expansion for launch directives
local placeholders = {
["${file}"] = function(_)
return vim.fn.expand("%:p")
end,
["${fileBasename}"] = function(_)
return vim.fn.expand("%:t")
end,
["${fileBasenameNoExtension}"] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:t"), ":r")
end,
["${fileDirname}"] = function(_)
return vim.fn.expand("%:p:h")
end,
["${fileExtname}"] = function(_)
return vim.fn.expand("%:e")
end,
["${relativeFile}"] = function(_)
return vim.fn.expand("%:.")
end,
["${relativeFileDirname}"] = function(_)
return vim.fn.fnamemodify(vim.fn.expand("%:.:h"), ":r")
end,
["${workspaceFolder}"] = function(_)
return vim.fn.getcwd()
end,
["${workspaceFolderBasename}"] = function(_)
return vim.fn.fnamemodify(vim.fn.getcwd(), ":t")
end,
["${env:([%w_]+)}"] = function(match)
return os.getenv(match) or ""
end,
}
if final_config.envFile then
local filePath = final_config.envFile
for key, fn in pairs(placeholders) do
filePath = filePath:gsub(key, fn)
end
for line in io.lines(filePath) do
local words = {}
for word in string.gmatch(line, "[^=]+") do
table.insert(words, word)
end
if not final_config.env then
final_config.env = {}
end
final_config.env[words[1]] = words[2]
end
end
on_config(final_config)
end,
}
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Problem Statement
Request
Implement "envFile" attribute in "launch.json" dap configuration.
Possible profits
A more convenient way to manage dotenvs while debugging complex projects.
Description
According to VSCode debugger config https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes there is a very useful attribute "envFile" that helps manage big Dotenv files while debugging complex projects. Without this attribute, there is only one way to use .env files – "env" attribute.
Now it works properly, but after editing variables, we should constantly reload nvim-dap. A workflow like this is painful in my point of view
Ideas or possible solutions
No response
Beta Was this translation helpful? Give feedback.
All reactions