From 06569bfd253609ce53665b87b348e30eedd4dd65 Mon Sep 17 00:00:00 2001
From: Fredrik Averpil <fredrik.averpil@gmail.com>
Date: Mon, 16 Jun 2025 07:21:15 +0200
Subject: [PATCH] fix: move plenary import into function

Commit cf02033 broke test detection in Neotest. When another plugin
calls `require("plenary.filetype")` before Neotest does, it seems that
Neotest cannot successfully complete the call to
`require("neotest.lib").treesitter.parse_positions` and does not find
any tests.

This seem like a bug in either plenary or Neotest and this commit is
merely a workaround.

Related issues:
- https://github.com/nvim-neotest/neotest/issues/502
- https://github.com/CopilotC-Nvim/CopilotChat.nvim/issues/1099
---
 lua/CopilotChat/utils.lua | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lua/CopilotChat/utils.lua b/lua/CopilotChat/utils.lua
index 1be2504b..7cb28979 100644
--- a/lua/CopilotChat/utils.lua
+++ b/lua/CopilotChat/utils.lua
@@ -1,7 +1,6 @@
 local async = require('plenary.async')
 local curl = require('plenary.curl')
 local scandir = require('plenary.scandir')
-local filetype = require('plenary.filetype')
 
 local M = {}
 M.timers = {}
@@ -236,6 +235,7 @@ end
 ---@param filename string The file name
 ---@return string|nil
 function M.filetype(filename)
+  local filetype = require('plenary.filetype')
   local ft = filetype.detect(filename, {
     fs_access = false,
   })