From 959c42978b4616a6a2bee0068933d7ac96bb2f2e Mon Sep 17 00:00:00 2001 From: Jan Klass Date: Sun, 20 Apr 2025 17:01:42 +0200 Subject: [PATCH] Fix winget-completions breaking winget show (#1090) by dropping output parsing of non-stable unstructured output into structured output. --- winget-completions.nu defines more than custom completions: it defines parsing for `winget show`, `winget source list`, `winget search`, `winget list`. `winget show` shows different user messages and rendered manifest information. #825 reports winget show breakage for `winget show` for packages `carapace`, `Peppy.Osu!`, `flux.flux`. The source yaml (for example [rsteube.Carapace.locale.en-US.yaml][sourceyaml]) is in YAML format. The command implementation tries to parse the rendered output and transform it into yaml, but fails to do so. The logic is quite complicated and error prone, with no guarantees on `winget show` output stability in text or format. Rather than trying to this the script, it seems safer to drop parsing the output into a structure format. #825 indicates winget show has been broken for a year. If we want to support structured output from `winget show`, it would be preferable to depend on `winget show` offering a structured output option (which it currently does not offer), or at least not offer error-prone parsing as a default part of *custom completions*. Users can still parse the output themselves. Independent of this removal, users may suggest alternative less error-prone or custom-completion-breaking approaches. [sourceyaml]: https://github.com/microsoft/winget-pkgs/blob/2ec31577c47a3c9fb141a2b6ef3113333ccef00f/manifests/r/rsteube/Carapace/1.3.0/rsteube.Carapace.locale.en-US.yaml Resolves #825 BREAKING: Drops structured output from `winget show` --- .../winget/winget-completions.nu | 62 +------------------ 1 file changed, 2 insertions(+), 60 deletions(-) diff --git a/custom-completions/winget/winget-completions.nu b/custom-completions/winget/winget-completions.nu index 9795e4c..e2e04d8 100644 --- a/custom-completions/winget/winget-completions.nu +++ b/custom-completions/winget/winget-completions.nu @@ -169,7 +169,7 @@ export extern "winget install" [ ] export alias "winget add" = winget install -export def "winget show" [ +export extern "winget show" [ pos_query?: string, --query(-q): string, # The query used to search for a package --id: string, # Filter results by id @@ -189,66 +189,8 @@ export def "winget show" [ --accept_package_agreements, # Accept all licence agreements for packages --header: string, # Optional Windows-Package-Manager REST source HTTP header --accept_source_agreements, # Accept all source agreements during source operations - --raw, # Output the raw CLI output instead of structured data --help(-?), # Display the help for this command -] { - let flagify = { |name, value| nu-complete winget flagify $name $value } - - def sanitize-line []: string -> string { - let it = $in - let parsed = ($it | parse '{name}:{value}') - if ($parsed | is-empty) { return $"($it)" } - let parsed = ($parsed | first) - try { - $"($parsed.name):(if ($parsed.value | str trim | is-empty) { '' } else { $"(char space)(char dq)($parsed.value | str trim)(char dq)" })" - } catch { - $"($it)" - } - } - - let params = ([ - "show" - ] | append ([ - $pos_query - (do $flagify query $query) - (do $flagify id $id) - (do $flagify name $name) - (do $flagify moniker $moniker) - (do $flagify version $version) - (do $flagify source $source) - #(do $flagify scope $scope) - (do $flagify exact $exact) - (do $flagify interactive $interactive) - (do $flagify silent $silent) - (do $flagify locale $locale) - (do $flagify log $log) - (do $flagify override $override) - (do $flagify location $location) - (do $flagify force $force) - (do $flagify accept_package_agreements $accept_package_agreements) - (do $flagify header $header) - (do $flagify accept_source_agreements $accept_source_agreements) - (do $flagify help $help) - ] | flatten) | filter { not ($in | is-empty)}) - - let output = ^winget ...$params - if $raw or $help or ($output | str contains "No package selection argument was provided") { - $output - } else { - let lines = ($output | lines) - - if ($lines | first) =~ "Multiple packages found matching input criteria." { - $"(ansi yellow)($lines | first | str trim)(ansi reset)" - nu-complete winget parse table ($lines | skip 1) | select name id source - } else if ($lines | first) =~ "No package found matching input criteria." { - $"(ansi yellow)($lines | first | str trim)(ansi reset)" - } else { - let header = ($lines | first | parse -r 'Found (?P.+) \[(?P.+)\]') - let manifest = ($lines | skip | each { sanitize-line } | str join (char newline) | from yaml) - $header | first | merge $manifest - } - } -} +] export alias "winget view" = winget show # Manage sources of packages