Skip to content

A def --env completion does not update the environment when run as a completion #12801

Open
@drbrain

Description

@drbrain

Describe the bug

I want to cache completion results in $env but nushell does not allow this with dev --env.

Here is part of a completion for git diff tool --tool [COMPLETE-TOOL-HERE. bug-tools is the completion function for the --tool argument and it takes some time for git to respond. I want to cache the output in $env to make future completions fast.

# Show diff tools
export def --env bug_tools [] {
  if ("GIT_NU_DIFF_TOOLS" in $env) {
    $env.GIT_NU_DIFF_TOOLS
  } else {
    let tools = run-external "git" "difftool" "--tool-help"
      | parse -r "\t\t(?<value>.+?)\\s+(?<description>.*)"

    $env.GIT_NU_DIFF_TOOLS = $tools

    $tools
  }
}

# Show changes using common diff tools
export extern main [
  ...pathspec: path
  --tool(-t): string@bug_tools
]

How to reproduce

  1. Save the above file to bug.nu
  2. use bug.nu *
  3. Note the time taken for bug --tool <TAB>
  4. Note the time taken for bug --tool <TAB> (a second time)
  5. Note the time taken for bug_tools
  6. Note the time taken for bug_tools (a second time)

Expected behavior

The second invocations of bug --tool <TAB> should be significantly faster than the first run.

I expect invocation of bug --tool <TAB> to behave like this:

❯ use bug.nu *
❯ timeit bug_tools
1sec 706ms 167µs 417ns
❯ timeit bug_tools
72µs 125ns

Screenshots

No response

Configuration

key value
version 0.93.0
major 0
minor 93
patch 0
branch
commit_hash
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09) (Homebrew)
cargo_version cargo 1.77.2
build_time 2024-04-30 22:51:13 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, system-clipboard, trash, which
installed_plugins dns, example, highlight, net

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    completionsIssues related to tab completionpolishthis problem makes nu feel unpolishedunhelpful-errorThe error message you observe is not helpful to identify the problem

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions