microsoft / winget-cli

WinGet is the Windows Package Manager. This project includes a CLI (Command Line Interface), PowerShell modules, and a COM (Component Object Model) API (Application Programming Interface).
https://learn.microsoft.com/windows/package-manager/
MIT License
23.3k stars 1.45k forks source link

FileContentDsc fails in winget configure #3904

Open egerlach opened 1 year ago

egerlach commented 1 year ago

Brief description of your issue

I am experimenting with the new winget configure system, and loving it so far. I wanted to start using it to edit some files, and selected the FileContentDsc module published by DSC Community.

The KeyValuePairFile resource does not work correctly through winget configure, though it works when invoked in Powershell directly.

I debated where to file this issue and eventually decided to at least start here because the resource works correctly when invoked directly from Powershell but fails when invoked from winget.

I've provided input files, command output and logs in a gist.

Steps to reproduce

  1. Create the following file, replacing <username> with your username, or choose another writable path:
# save as filecontentdsc.yaml
properties:
  configurationVersion: '0.2.0'
  resources:
    - directives:
        allowPrerelease: true
      resource: FileContentDsc/KeyValuePairFile
      settings:
        Ensure: Present
        Name: foo
        Path: "C:\\Users\\<username>\\test.conf"
        Text: bar
  1. Create the file "C:\Users\\<username>\test.conf" with the content:
    foo=baz
  2. winget configure .\filecontentdsc.yaml --verbose-logs --accept-configuration-agreements

Expected behavior

I would expect the file to be updated to read:

foo=bar

Actual behavior

The configure command errors out with:

PS C:\Users\egerlach\src\dsc-test> winget configure .\filecontentdsc.yaml --verbose-logs --accept-configuration-agreements
Apply :: KeyValuePairFile
  C:\Users\egerlach\test.conf: Set foo to bar
  Module: FileContentDsc by DSC Community [Local]
    https://github.com/dsccommunity/FileContentDsc
    DSC resources for for manipulating the content of text files.
  Settings:
    Path: C:\Users\egerlach\test.conf
    Ensure: Present
    Name: foo
    Text: bar
You are responsible for understanding the configuration settings you are choosing to execute. Microsoft is not responsible for the configuration file you have authored or imported. This configuration may change settings in Windows, install software, change software settings (including security settings), and accept user agreements to third-party packages and services on your behalf.  By running this configuration file, you acknowledge that you understand and agree to these resources and settings. Any applications installed are licensed to you by their owners. Microsoft is not responsible for, nor does it grant any licenses to, third-party packages or services.
Apply :: KeyValuePairFile
  The configuration unit failed while attempting to test the current system state.
Cannot process argument transformation on parameter 'Encoding'. 'byte' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')
Cannot index into a null array.
<See the log file for additional details>
Some of the configuration was not applied successfully.

Full log

Environment

PS C:\Users\egerlach> winget --info
Windows Package Manager v1.6.2771
Copyright (c) Microsoft Corporation. All rights reserved.

Windows: Windows.Desktop v10.0.22621.2428
System Architecture: X64
Package: Microsoft.DesktopAppInstaller v1.21.2771.0

Winget Directories
-----------------------------------------------------------------------------------------------------------------------
Logs                               %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\Diag…
User Settings                      %LOCALAPPDATA%\Packages\Microsoft.DesktopAppInstaller_8wekyb3d8bbwe\LocalState\sett…
Portable Links Directory (User)    %LOCALAPPDATA%\Microsoft\WinGet\Links
Portable Links Directory (Machine) C:\Program Files\WinGet\Links
Portable Package Root (User)       %LOCALAPPDATA%\Microsoft\WinGet\Packages
Portable Package Root              C:\Program Files\WinGet\Packages
Portable Package Root (x86)        C:\Program Files (x86)\WinGet\Packages
Installer Downloads                %USERPROFILE%\Downloads

Links
---------------------------------------------------------------------------
Privacy Statement   https://aka.ms/winget-privacy
License Agreement   https://aka.ms/winget-license
Third Party Notices https://aka.ms/winget-3rdPartyNotice
Homepage            https://aka.ms/winget
Windows Store Terms https://www.microsoft.com/en-us/storedocs/terms-of-sale

Admin Setting                             State
--------------------------------------------------
LocalManifestFiles                        Disabled
BypassCertificatePinningForMicrosoftStore Disabled
InstallerHashOverride                     Disabled
LocalArchiveMalwareScanOverride           Disabled

PS C:\Users\egerlach\src\workstation-setup> Get-Module

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   2.0.0      preview00… FileContentDsc
Manifest   7.0.0.0               Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-Item, Clear-It…
Manifest   7.0.0.0               Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Ob…
Script     1.4.8.1               PackageManagement                   {Find-Package, Find-PackageProvider, Get-Package,…
Script     2.2.5                 PowerShellGet                       {Find-Command, Find-DscResource, Find-Module, Fin…
Script     2.0.7                 PSDesiredStateConfiguration         {Configuration, Get-DscResource, Invoke-DscResour…
Script     2.2.6                 PSReadLine                          {Get-PSReadLineKeyHandler, Get-PSReadLineOption, …
denelon commented 12 months ago

I did a couple of tests based on the error.

I tried to set Encoding: ASCII even though the Wiki states it's the default since the error was Encoding. byte is not a supported encoding name.

If the file doesn't exist, the configuration will succeed and create the file. I think this might be a bug in the DSC Resource.

How did you test with PowerShell directly?

egerlach commented 12 months ago
PS C:\Users\egerlach\src\workstation-setup> cat C:\Users\egerlach\test.conf
foo=false
PS C:\Users\egerlach\src\workstation-setup> Invoke-DscResource -Name KeyValuePairFile -Method Set -ModuleName FileContentDsc -Property @{
>>     Path   = 'c:\Users\egerlach\test.conf'
>>     Name   = 'foo'
>>     Ensure = 'Present'
>>     Text   = 'bar'
>> }

RebootRequired
--------------
         False

PS C:\Users\egerlach\src\workstation-setup> cat C:\Users\egerlach\test.conf
foo=bar
egerlach commented 12 months ago

It could absolutely be a bug in the resource. I'll admit I'm a n00b with DSC, though I know my way around PS okay. Based on what I was reading in the error logs it looked like it might be a problem with how this particular resource was invoked rather than with the resource itself. I'm not set up to debug through the winget-cli code into the resource invocation though, so I didn't know how I could proceed further.

denelon commented 11 months ago

Do you see the same thing in terms of calling Invoke DSC Resource failing to modify the key value pairs when the file exists with a different value?

It seems to work when there is no file (it creates one with the right key value pair), and it seems to return $true when the file has the correct pair. It only fails for me to change the value of the "value" in the pair.