RegionOrebroLan / PowerShell-Transforming

PowerShell commands for json-, package- and xml-transformation.
MIT License
0 stars 0 forks source link

"Assembly with same name is already loaded" #4

Open clRecruitment21 opened 7 months ago

clRecruitment21 commented 7 months ago

Hi, so we use RegionOrebroLan.Transforming and have done for a while.

On Friday we noticed that things had stopped working in our azure pipeline wit the error below.

The 'New-AzSubscriptionDeployment' command was found in the module'Az.Resources', but the module could not be loaded due to the following error: [Assembly with same name is already loaded] For more information, run 'Import-Module Az.Resources'.

Today I started looking at this in anger, initially we assumed that it related to the fact the azure pipeline had updated their Az version.

It seemed like a poignant change between when we last knew this to work, and now.

The issue is tracked here: https://github.com/actions/runner-images/issues/9445 With my notes and comments.

I was fully convinced that issue lay here - with a nuance of powershell Az version, and have tried just about everything you can think of.

Then I noticed - out output a variable from our scripts that is the path where there transformation output path is was suddenly 'capturing' additional output.

Where we were seeing this: "Deploying /home/vsts/work/1/s/Infrastructure/Deploy/Out\ResourceGroup\ResourceGroup.bicep"

we were now seeing this: "Deploying The transformation was successful: "/home/vsts/work/1/s/Infrastructure/Deploy/Out" /home/vsts/work/1/s/Infrastructure/Deploy/Out\ResourceGroup\ResourceGroup.bicep"

It was a total fluke spot - but it opened the door to the fact that it might not have just been the Azure Image that had changed - and low and behold RegionOrebroLan.Transforming 2.1 had been released.

I set this back to 2.0 and our error went away, I set it to 2.1 and 'squelched' the output like this

    New-PackageTransform `
        -Destination $destinationPath `
        -FileToTransformPatterns "**/*.json" `
        -Source $sourcePath `
        -TransformationNames "Specification.$($this.Specification)", "Platform.$($this.Platform.Name)", "Environment.$($this.Name)" `
        **| Out-Null # suppress output**

and sadly it returned.

So the issue is not purely that we were inadvertently capture the extra/new output from the script.

I have tried to skim the code and the changes, searched for obvious references to thing - nothing sticks out as a smoking gun.

Do you have any idea what might have changed between 2.0 and 2.1 that would cause our error?

In the mean we'll have to pin - but I am not a fan of doing this as we'd like to stay up-to-date with your awesome work.

Please let me know if anything comes to mind or if there is anything we can try?

Thanks.

HansKindberg commented 7 months ago

Hi

Nice that you use this.

There has been a change from 2.0 to 2.1 regarding the output from the cmdlets. In 2.0 there was no output but in 2.1 there is an output: this.WriteObject($"The transformation was successful: \"{this.Destination}\"");

Here: https://github.com/RegionOrebroLan/PowerShell-Transforming/blob/main/Source/Project/Commands/NewPackageTransformCommand.cs#L59

It is a just a confirmation so you know it worked properly. Before it was silent.

If I run this, on my Windows-machine:

New-PackageTransform -Destination "C:\_Testing\Out" -Source "C:\_Testing\In"

I get this output:

The transformation was successful: "C:\_Testing\Out"

If I run this, on my Windows-machine:

New-PackageTransform -Destination "C:\_Testing\Out" -Source "C:\_Testing\In" | Out-Null

I get NO output.

If I run this, on my Windows-machine:

New-PackageTransform -Destination "C:\_Testing\Out" -Source "C:\_Testing\In" > $null

I also get NO output.

So as I see it, what you are trying to do should work. It should not give the extra output.

You say you try this:

    New-PackageTransform `
        -Destination $destinationPath `
        -FileToTransformPatterns "**/*.json" `
        -Source $sourcePath `
        -TransformationNames "Specification.$($this.Specification)", "Platform.$($this.Platform.Name)", "Environment.$($this.Name)" `
        **| Out-Null # suppress output**

I guess the "**" wildcards are there for clearity. But have you tried this:

    New-PackageTransform `
        -Destination $destinationPath `
        -FileToTransformPatterns "**/*.json" `
        -Source $sourcePath `
        -TransformationNames "Specification.$($this.Specification)", "Platform.$($this.Platform.Name)", "Environment.$($this.Name)" `
        | Out-Null

or this:

    New-PackageTransform `
        -Destination $destinationPath `
        -FileToTransformPatterns "**/*.json" `
        -Source $sourcePath `
        -TransformationNames "Specification.$($this.Specification)", "Platform.$($this.Platform.Name)", "Environment.$($this.Name)" `
        > $null

Maybe also try to put parantheses around:

    (New-PackageTransform `
        -Destination $destinationPath `
        -FileToTransformPatterns "**/*.json" `
        -Source $sourcePath `
        -TransformationNames "Specification.$($this.Specification)", "Platform.$($this.Platform.Name)", "Environment.$($this.Name)" `
        | Out-Null)

There are other changes to, but as you say it seems to be the output that gives the problems.

Other changes:

Regards Hans

HansKindberg commented 6 months ago

Hi again

So I read what you wrote more carefully.

PowerShell-Transforming uses .NET-Transforming. In .NET-Transforming we have added functionality for configuration/options, dependency-injection and logging. This means that more dll's are used in version 2.1 than was used in version 2.0. Some of the new ones:

So your error: "Assembly with same name is already loaded"

I am pretty sure that when your PowerShell scripts load, [Ubuntu, Windows] Az Powershell module will be updated to v11.3.1 on March 18 #9445, some of the dll's listed above are already loaded in your "script-context" by some of the other ps-modules/scripts. Then they are being loaded again but with a different version and the exception is thrown.

I am not sure how your build tasks, actions, are set up, if it is a Azure DevOps pipeline or GitHub actions. I think you have to put the transformations in a separate task/action.

An example how we use it with Azure DevOps 2022 on premise:

jobs:
- job: "Some job"
  steps:
  - script ...
  - script ...
  - powershell: |
      $destination = "$(DEPLOY_SOURCE_DIRECTORY_PATH)";
      $source = "$(Build.SourcesDirectory)/Source/Application";
      $transformationName = "${{ parameters.Environment }}";
      Write-Host "Creating deploy-source '$($destination)' from soure '$($source)' with transformation-names 'Release' & '$($transformationName)' ...";
      New-PackageTransform `
        -Destination $destination `
        -FileToTransformPatterns "Resources/*.xml", "appsettings*.json" `
        -Source $source `
        -TransformationNames "Release", $transformationName;
      Write-Host "Deploy-source '$($destination)' from soure '$($source)' with transformation-names 'Release' & '$($transformationName)' created.";
  - script ...
  - script ...

Now we can use the transformed files in other tasks/actions.

Regards Hans