Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
drbrain opened this issue May 7, 2024 · 1 comment
Labels
completions Issues related to tab completion polish this problem makes nu feel unpolished unhelpful-error The error message you observe is not helpful to identify the problem

Comments

@drbrain
Copy link
Contributor

drbrain commented May 7, 2024

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

@drbrain drbrain added the needs-triage An issue that hasn't had any proper look label May 7, 2024
@kubouch
Copy link
Contributor

kubouch commented May 8, 2024

This works as intended IMO. Running completions shouldn't have side effects. There could be an error if you attempt to use def --env as a completion function, though, to make it more clear.

You could try using the stor commands if you really need to store some data from the completion function.

@kubouch kubouch added polish this problem makes nu feel unpolished unhelpful-error The error message you observe is not helpful to identify the problem and removed needs-triage An issue that hasn't had any proper look labels May 8, 2024
@sholderbach sholderbach added the completions Issues related to tab completion label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completions Issues related to tab completion polish this problem makes nu feel unpolished unhelpful-error The error message you observe is not helpful to identify the problem
Projects
None yet
Development

No branches or pull requests

3 participants