Skip to content

Feature request: Per-firectory or per-file environments #3185

Open

Description

@SlashScreen

Is there a way I could set files under a specific directory to use a @meta file as an environment? If not, how would I accomplish this? In my project, a certain folder as a number of modular scripts that have an environment separate to the rest of the project.

Activity

  1. tomlau10 commented on May 19, 2025

    @tomlau10
    Contributor

    There are already a few similar feature requests and discussions:

    The closest thing for supporting separate environment currently is using the multi-root workspace feature: https://luals.github.io/wiki/developing/#multiple-workspace-support
    And vscode is one of the editors that support this feature.

  2. justarandomgeek commented on Jul 25, 2025

    @justarandomgeek

    i have recently found that doing local _ENV = _ENV --[[@as SomeClassToUseAsEnv]] at the top of a file kinda-sorta works to change the environment - the autocomplete is still only using the regular global environment, but the type checking applies the new class as env to tell you what globals are actually valid! (and i've been considering a small plugin hack to rewrite some special comment into this, but it doesn't quite work well enough for me to go down that road...)

    (Notably though, doing ---@cast _ENV SomeClassToUseAsEnv instead seems to do nothing at all!)

  3. ChrisKader commented on Mar 8, 2026

    @ChrisKader

    This is a start. https://github.com/ChrisKader/lua-language-server

    ---@class testenv1
    local testenv1 = {
      test = false,
    }
    
    
    ---env within a function.
    setfenv((function()
      ---@env testenv1
      print(type(test))
    end), testenv1)()
    ------------------------
    
    ---@class testenv2
    local testenv2 = {
      type = type,
      print = print,
      test = false,
    }
    
    setfenv((function()
      ---@env testenv2
      print(type(test))
    end), testenv2)()
    print(type(test))
    
    ------------------------
    
    ---@class testenv3 : _G
    local testenv3 = setmetatable({x = 0}, {__index = _G})
    
    local function test2()
      ---@env testenv3
      print(x, y)
    end
    
    ------------------------
    
    ---@class testenv4
    local testenv4 = setmetatable({
      test1 = 1,
      test2 = "2",
      test3 = false,
    }, { __index = _G })
    
    
    ---@class testenv5
    ---@field test1 fun<testenv4>(): integer
    ---@field test2 function<testenv4>
    local test = {
      test1 = function() return test1 end,
      test2 = function() return test2 end,
      test3 = function() return test3 end, --[[@env testenv4]]
    }

    "Per File" env.

    ---@env testenv4
    
    print(test)
  4. Svarshick commented on Jun 6, 2026

    @Svarshick

    @ChrisKader, have you considered opening a PR for the ---@env feature?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions