gaelcolas / Sampler

Module template with build pipeline and examples, including DSC elements.
MIT License
167 stars 42 forks source link

In some scenarios, `PSModulePath` should only point to `output/RequiredModules` #393

Closed raandree closed 1 year ago

raandree commented 1 year ago

Problem description

When using sampler for the DscWorkshop, you get compilation errors when the same DSC resource module exists multiple times on multiple locations.

For DSC build, we need to shorten the PSModulePath to point only to output/RequiredModules plus paths that should be definable in the build.yml

@gaelcolas, is this something this should go in Sampler or rather in Sampler.DscPipeline?

Verbose logs

DSCFile04 : DSCFile04 : MOF__ NA                                                                                                                                                        DSCFile04 : FileServer ::> FilesAndFolders .....................................................OK                                                                                  DSCFile04 : FileServer ::> RegistryValues ......................................................FAILED                                                                                  Multiple version of the same module exist        
PS E:\Git\DscWorkshop> Get-Module -ListAvailable xPSDesiredStateConfiguration

    Directory: E:\Git\DscWorkshop\output\RequiredModules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     9.1.0      xPSDesiredStateConfiguration        {Publish-DscModuleAndMof, Publish-ModulesAndChecksum, Publish-MofsInSource, Publish-ModuleToPullServer...}

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     9.1.0      xPSDesiredStateConfiguration        {Publish-DscModuleAndMof, Publish-ModulesAndChecksum, Publish-MofsInSource, Publish-ModuleToPullServer...}

### How to reproduce

Build DscWorkshop with some required DSC resource modules in Program Files.

### Expected behavior

Build succeeds.

### Current behavior

Build fails with `Multiple version of the same module exist`.

### Suggested solution

Make the `PSModulePath` within build process more configurable.

### Operating system the target node is running

```text
NA

PowerShell version and build the target node is running

Windows PowerShell 5.1

Module version used

0.115.0
raandree commented 1 year ago

I am now running into this problem on a number of projects. Additional input would be helpful on how to proceed here.

gaelcolas commented 1 year ago

It's non-trivial iirc because some modules during compilation re-set some PSModulePath (namely PackageManagement) when you compile a mof that uses it (i.e. PowerShellGet). Being able to select what $Env:PSModulePath is set to from the build.yaml sounds like a good feature for Sampler. If you only want to cut down what's in $Env:PSModulePath, that might be more suited to Sampler.DscPipeline...

raandree commented 1 year ago

Thanks for the input. I am going to work on this next week and depending how generic and configurable the new task is going to be, we can put it either into Sampler.DscPipeline or Sampler.

johlju commented 1 year ago

Make sure that the BuiltModuleSubdirectory also still works if used, e.g:

BuiltModuleSubdirectory: builtModule
johlju commented 1 year ago

@raandree is this fixed now in the recent PR?

gaelcolas commented 1 year ago

Closing this as the task is in Sampler and works as intended (see ./.build/tasks/SetPsModulePath.build.ps1) That said, I found that because how fragile the DSC compilation is, I wouldn't recommend using it, except in specific cases when people know what they're doing. BuiltModuleSubdirectory is for sure preferred when that's enough, which rarely is in WMF5.1 DSC.