Connect-DbaInstance - Keep the tab completion cache of instance names an array - #10672
Open
andreasjordan wants to merge 1 commit into
Open
Connect-DbaInstance - Keep the tab completion cache of instance names an array#10672andreasjordan wants to merge 1 commit into
andreasjordan wants to merge 1 commit into
Conversation
… an array The cache starts as $null, and "$null += name" makes it a string. From the second instance on, every name was concatenated onto that string, -notcontains compared the whole string, and every later connection appended its name again, so the completer offered one long blob instead of names. A new private function rebuilds the cache as an array of unique lower-cased names, and the three writers (Connect-DbaInstance, Add-DbaInstanceList, the dynamicparams script) call it instead of +=. (do Connect-DbaInstance, Add-DbaInstanceList)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The tab completion cache for
-SqlInstance([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"]) is aSystem.Stringinstead of an array as soon as one instance is in it. From the second distinct instance on, every new name is concatenated onto that string, and because-notcontainsthen compares the whole string, every later connection to an already known instance appends its name again. In a long session the cache readssql05\sql2025sql05\sql2019sql05\sql2019sql05\sql2019..., and the completer offers that blob instead of instance names. Both editions, reproduced with twoConnect-DbaInstancecalls: typeSystem.String, valuesql05\sql2019sql05\sql2022.Surfaced in a full lab run:
Add-DbaInstanceList.Tests.ps1failed withExpected 'dbatoolsci_testinstance_...' to be found in collection sql05\sql2025sql05\sql2019sql05\sql2019.... The file passes alone because a fresh process has an empty cache and a one-name string still satisfies-contains.Mechanism
The cache starts as
$null.$null += "x"yields the string"x", not a one-element array, and every writer used+=:Connect-DbaInstanceafter each connection,Add-DbaInstanceList, andprivate/dynamicparams/sqlinstance.ps1for the configured and environment instances. The@()initialisation in that script does not help, because the TEPP maintenance task that loads it runs later than the first connection in most sessions.What changed
Add-DbaTeppInstanceName(inprivate/functions/tabcompletion, so the TEPP maintenance runspace has it too): rebuilds the cache as an array every time, lower cases the names as the completer expects, skips empty entries and duplicates.+=. No behaviour change beyond the cache being an array of unique names.Tests
Add-DbaInstanceList.Tests.ps1: new test adds two instances and asserts the cache is an array containing both, with the first one exactly once. Red on old:Expected $true, but got $falseon the array check (the cache is the stringdbatoolsci_testinstance2_...dbatoolsci_testinstance_...).Connect-DbaInstance.Tests.ps1: new context connects toInstanceMulti1andInstanceMulti2and asserts both names are separate entries.created by Claude and reviewed by Andreas Jordan
🤖 Generated with Claude Code