"Latest/Edge" is relying on undefined behaviour. Interpreting it as "Latest/Edge" for now, but this will be an error later

My PowerShell code

snap list | Select-String -Pattern `"\S+`" | ForEach-Object { snap switch $_ --channel Latest/Edge }

produces

Warning: Specifying a channel "Latest/Edge" is relying on undefined behaviour. Interpreting it as
         "Latest/Edge" for now, but this will be an error later.

error: invalid risk in channel name: Latest/Edge
NativeCommandExitException: Program "snap" ended with non-zero exit code: 1.

Why?

The reason it’s not liking that channel name is because snap channel names are case-sensitive. You want latest/edge rather than Latest/Edge.

However, I think you’re still going to get errors with that output. I believe what you’re intending is this:

snap list | Select-String -Pattern "^(?!Name )\S+" -CaseSensitive | ForEach-Object { snap switch $_.Matches[0].Value --channel latest/edge }

The change I’ve made to the Select-String pattern guarantees that it’ll find the first word in each line and will skip the header row by ignoring 'Name ' at the start of the row. (This should be fine since snap names aren’t allowed to have spaces and are all lower-case). ForEach-Object now outputs just the matching portion of the line rather than the full text.

This is roughly equivalent to the following bash:

snap list | grep -oP '^(?!Name )\S+' | xargs -n 1 snap switch --channel latest/edge

You could also query the snapd REST API directly, but I’m not sure whether Powershell can be coaxed to make Invoke-RestMethod talk to a unix-domain socket rather than a TCP one.

1 Like

Does the 1st example deliberately contain latest-edge rather than latest/edge? snap appears to accept it, but I had not heard of the channel previously, and the output causes me to believe that it merely reverts to stable because the channel name is potentially invalid. Please advise. Thanks.

PS /home/BEEDELLROKEJULIANLOCKHART> snap list | Select-String -Pattern "^(?!Name )\S+" -CaseSensitive | ForEach-Object { snap switch $_.Matches[0].Value --channel latest-edge }
"authy" switched to the "latest-edge/stable" channel
"bare" switched to the "latest-edge/stable" channel
"core18" switched to the "latest-edge/stable" channel
"core20" switched to the "latest-edge/stable" channel
"gnome-3-28-1804" switched to the "latest-edge/stable" channel
"gnome-3-38-2004" switched to the "latest-edge/stable" channel
"gtk-common-themes" switched to the "latest-edge/stable" channel
"raindrop" switched to the "latest-edge/stable" channel
"snapd" switched to the "latest-edge/stable" channel
"spotify" switched to the "latest-edge/stable" channel
"spotify-qt" switched to the "latest-edge/stable" channel
PS $Home> snap list
Name               Version                     Rev    Tracking            Publisher     Notes
authy              2.2.2                       12     latest-edge/stable  twilio-authy  -
bare               1.0                         5      latest-edge/stable  canonicalâś“    base
core18             20221212                    2667   latest-edge/stable  canonicalâś“    base
core20             20221216                    1796   latest-edge/stable  canonicalâś“    base
gnome-3-28-1804    3.28.0-19-g98f9e67.98f9e67  185    latest-edge/stable  canonicalâś“    -
gnome-3-38-2004    0+git.6ed44b3               105    latest-edge/stable  canonicalâś“    -
gtk-common-themes  0.1-81-g442e511             1535   latest-edge/stable  canonicalâś“    -
raindrop           5.5.10                      6      latest-edge/stable  exentrich     -
snapd              2.58+git294.g5caf673        18146  latest-edge/stable  canonicalâś“    snapd
spotify            1.1.84.716.gc5f8b819        60     latest-edge/stable  spotifyâś“      -
spotify-qt         v3.9                        2228   latest-edge/stable  kraxarn       -
PS $Home> snap list | Select-String -Pattern "^(?!Name )\S+" -CaseSensitive | ForEach-Object { snap switch $_.Matches[0].Value --channel latest/edge }
"authy" switched to the "latest/edge" channel
"bare" switched to the "latest/edge" channel
"core18" switched to the "latest/edge" channel
"core20" switched to the "latest/edge" channel
"gnome-3-28-1804" switched to the "latest/edge" channel
"gnome-3-38-2004" switched to the "latest/edge" channel
"gtk-common-themes" switched to the "latest/edge" channel
"raindrop" switched to the "latest/edge" channel
"snapd" switched to the "latest/edge" channel
"spotify" switched to the "latest/edge" channel
"spotify-qt" switched to the "latest/edge" channel
PS $Home> snap list                                                                                                                                   
Name               Version                     Rev    Tracking     Publisher     Notes
authy              2.2.2                       12     latest/edge  twilio-authy  -
bare               1.0                         5      latest/edge  canonicalâś“    base
core18             20221212                    2667   latest/edge  canonicalâś“    base
core20             20221216                    1796   latest/edge  canonicalâś“    base
gnome-3-28-1804    3.28.0-19-g98f9e67.98f9e67  185    latest/edge  canonicalâś“    -
gnome-3-38-2004    0+git.6ed44b3               105    latest/edge  canonicalâś“    -
gtk-common-themes  0.1-81-g442e511             1535   latest/edge  canonicalâś“    -
raindrop           5.5.10                      6      latest/edge  exentrich     -
snapd              2.58+git294.g5caf673        18146  latest/edge  canonicalâś“    snapd
spotify            1.1.84.716.gc5f8b819        60     latest/edge  spotifyâś“      -
spotify-qt         v3.9                        2228   latest/edge  kraxarn       -
PS $Home>

Ope, no! That was a typo. I’ve fixed it now - thanks for pointing it out!

1 Like

It’s alright! Thanks a darn lot for fixing my code for me.

1 Like