Skip to content

[QUESTION] How to enable collection of unused keywords and variables in CLI #587

Closed

Description

@MikeAckerschott

Please describe.
When running robotcode analyze code robotcode does not report unused keywords and variables by default. The vscode extension does do this. After reviewing the source code, i found these lines in robot_framework_language_provider.py, which were commented. Is this feature not implemented yet, or can it be enabled by changing the configuration?

Desktop (please complete the following information):

  • VS Code Version 1.111.0
  • RobotCode Version 2.4.0
  • OS: Windows 11
  • Python Version 3.11.9
  • RobotFramework Version 7.3.2
  • RoboCop version 8.2.2

Activity

  1. added
    question
    on Mar 24, 2026
  2. d-biehl commented on Mar 24, 2026

    @d-biehl
    Member

    This feature is currently disabled because it was too slow on the command line for whole-project usage.

    The code for it is already there, but I decided not to enable it yet. I’m currently working on a project-wide cacheable dependency/reference index to speed this up and improve related functionality.

    I also want to add some command line options and robot.toml settings so it can be enabled or disabled when needed.

    So please bear with me — it will likely be enabled in one of the next releases.

  3. MikeAckerschott commented on Mar 24, 2026

    @MikeAckerschott
    Author

    Thanks for the quick response. I myself noticed it being quite slow and researched it with cProfile to find out what was the culprit. For the project i was working on, it was caused by the variable_not_found function in robotframework, specifically the recommendation finder, as it would search for a similar variable name for creating a suggestion before resolving it and do nothing with the raised VariableError afterwards when it is resolved. This is very very slow for projects with lots and lots of variables. Manually changing the function in my venv to just this:

    def variable_not_found(name, candidates, message=None, deco_braces=True):
        """Raise DataError for missing variable name.
    
        Return recommendations for similar variable names if any are found.
        """
        raise VariableError(message)
    

    Would make the diagnostics for running it within vscode using the robotcode language server take about 30-ish minutes for 375 .robot/.resource files. Quite significant.

    I also messed around with monkeypatching robotframework to fix this issue in my own project. perhaps you could try my approach and see if it improves it:

    # ---------------------------------------------------------------------------
    # Monkey-patch: suppress the slow "Did you mean ...?" RecommendationFinder
    # in robot.variables.notfound.variable_not_found.
    #
    # The original function builds fuzzy-match recommendations against all known
    # variables, which is expensive.  We replace it with a fast version that just
    # raises immediately.
    #
    # IMPORTANT: evaluation.py does `from .notfound import variable_not_found`,
    # so it holds a *direct reference* to the original function object.  We must
    # patch BOTH the module attribute AND every caller that already imported it.
    # ---------------------------------------------------------------------------
    from robot.errors import VariableError
    import robot.variables.notfound as _notfound_module
    import robot.variables.evaluation as _evaluation_module
    import robot.variables.finders as _finders_module
    import robot.variables.store as _store_module
    import robot.variables as _variables_pkg
    
    _original_variable_not_found = _notfound_module.variable_not_found
    
    
    def _patched_variable_not_found(name, candidates, message=None, deco_braces=True):
        raise VariableError(message or f"Variable '{name}' not found.")
    
    
    # Patch every module that does `from .notfound import variable_not_found`
    _PATCH_TARGETS = [
        (_notfound_module, "robot.variables.notfound"),
        (_evaluation_module, "robot.variables.evaluation"),
        (_finders_module, "robot.variables.finders"),
        (_store_module, "robot.variables.store"),
        (_variables_pkg, "robot.variables.__init__"),
    ]
    for _mod, _name in _PATCH_TARGETS:
        _mod.variable_not_found = _patched_variable_not_found
    
  4. d-biehl commented on Mar 30, 2026

    @d-biehl
    Member

    Thanks for the detailed analysis @MikeAckerschott! Your monkey-patch approach was spot on.

    I've now implemented this fix directly in RobotCode — we patch variable_not_found across all RF modules that import it, exactly as you suggested. This will be included in the next release.

    I've also filed an upstream issue at Robot Framework to add an official way to disable the RecommendationFinder so we don't need the monkey-patch long-term: robotframework/robotframework#5644

    The --collect-unused CLI option and robot.toml setting for enabling unused keyword/variable collection on the command line is also coming soon. see here 5734690

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

    question

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions