Skip to content

Connect-DbaInstance - Keep the tab completion cache of instance names an array - #10672

Open
andreasjordan wants to merge 1 commit into
developmentfrom
fix-tepp-sqlinstance-cache-array
Open

Connect-DbaInstance - Keep the tab completion cache of instance names an array#10672
andreasjordan wants to merge 1 commit into
developmentfrom
fix-tepp-sqlinstance-cache-array

Conversation

@andreasjordan

Copy link
Copy Markdown
Collaborator

Problem

The tab completion cache for -SqlInstance ([Dataplat.Dbatools.TabExpansion.TabExpansionHost]::Cache["sqlinstance"]) is a System.String instead 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 -notcontains then compares the whole string, every later connection to an already known instance appends its name again. In a long session the cache reads sql05\sql2025sql05\sql2019sql05\sql2019sql05\sql2019..., and the completer offers that blob instead of instance names. Both editions, reproduced with two Connect-DbaInstance calls: type System.String, value sql05\sql2019sql05\sql2022.

Surfaced in a full lab run: Add-DbaInstanceList.Tests.ps1 failed with Expected '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-DbaInstance after each connection, Add-DbaInstanceList, and private/dynamicparams/sqlinstance.ps1 for 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

  • New private function Add-DbaTeppInstanceName (in private/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.
  • The three writers call it instead of +=. 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 $false on the array check (the cache is the string dbatoolsci_testinstance2_...dbatoolsci_testinstance_...).
  • Connect-DbaInstance.Tests.ps1: new context connects to InstanceMulti1 and InstanceMulti2 and asserts both names are separate entries.
  • Both files green through the testing-dbatools harness on PowerShell 7.6 and 5.1.

created by Claude and reviewed by Andreas Jordan

🤖 Generated with Claude Code

… 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant