8000 Make ResourceManagerCache check for alternative resource paths by anmenaga · Pull Request #4139 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Make ResourceManagerCache check for alternative resource paths #4139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 4, 2017

Conversation

anmenaga
Copy link

This PR is for 2 things:

  1. From WindowsPS to PSCore resource paths have changed like this example:
    WindowsPS: FileSystemProviderStrings
    PSCore: System.Management.Automation.resources.FileSystemProviderStrings
    ... and some existing modules use 'WindowsPS' syntax in their "format.ps1xml" files; for example:
    <Text AssemblyName="System.Management.Automation" BaseName="FileSystemProviderStrings" ResourceId="DirectoryDisplayGrouping"/>
    So in these cases resource is not found and Import-Module/Update-FormatData returns error on PSCore.
    The fix is to check for alternative resource path if original fails.

  2. when a resource is not found, currently error message has wrong assembly path which was tried for loading the resource; for example:
    ... resource NonExistingResource in assembly C:\System.Management.Automation is not found.
    This is rather confusing, specifically because assembly, containing the resource, can be located anywhere in the filesystem. The fix makes error message accurate: ... resource NonExistingResource in assembly C:\GitHub\PowerShell\src\powershell-win-core\bin\Debug\netcoreapp2.0\win10-x64\publish\System.Management.Automation.dll is not found.

Results on PSCore after the fix:
afterfix

Results on PSCore before the fix:
beforefix

Fix #3057

}
else
{
newBaseName = assembly.GetName().Name + resourcesSubstring + baseName; // e.g. "System.Management.Automation.resources.FileSystemProviderStrings"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not concatenate strings, use a StringBuilder.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A single call to string.Concat with 4 or fewer arguments could be better than using a StringBuilder - the length of the string can be pre-calculated, so there would only be 1 allocation instead of multiple for the StringBuilder, the array of characters in StringBuilder, and the final string allocation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to string.Concat

</TableControl>
</View>
</ViewDefinitions>
</Configuration>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the test should not look anything like something that is built-in - otherwise it might cause problems with other tests.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added newline;
renamed the format file to indicate relation to tests;
these tests are run in separate PowerShell/Runspace instances, so there should be no conflict with other tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Today there is no conflict.

You should write the test so there can never be a conflict, even if the code is reused.

It's really simple to use a type name specific to this scenario, so I see no reason to not do as I've suggested.

$ps.Invoke()
$sma = [appdomain]::CurrentDomain.GetAssemblies() | ? { if ($_.Location) {$_.Location.EndsWith("System.Management.Automation.dll")}}
$smaLocation = $sma.Location
$ps.Streams.Error | %{ $_.Exception.Message.Contains($smaLocation) | Should be $true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you do:

$_.Exception.Message | Should Match $smaLocation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this fails with
parsing 'C:\Users\GitHub\PowerShell\New\src\powershell-win-core\bin\Debug\netcoreapp2.0\win10-x64\publish\System.Management.Automation.dll' - Unrecognized escape sequence \\U.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MatchExactly fails as well.

$ps.Streams.Error | %{ $_.Exception.Message.Contains($smaLocation) | Should be $true }
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

$ps.Streams.Error | %{ $_.Exception.Message.Contains($smaLocation) | Should be $true }
}
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a test where we lookup are string resource which does not exist and expect an MissingManifestResourceException

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this last test is for when resource is not found;
I've added check for FullyQualifiedErrorId


// FYI: for a non-existing resource defined by {assembly,baseName,resourceId}
// MissingManifestResourceException is thrown only at the time when resource retrieval method such as ResourceManager.GetString or ResourceManager.GetObject is called,
// Not when you instantiate a ResourceManager object.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems 'FYI' abbreviation should be removed or expanded.
Please reformat and even a width of lines.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.

// Not when you instantiate a ResourceManager object.
try
{
// try with original baseName
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment as a single is obvious. So we should combining the comment with next one (move from line 194) to give a reader a full description.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.

@@ -0,0 +1,88 @@
<Configuration>
<SelectionSets>
Copy link
Collaborator
@iSazonov iSazonov Jul 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should put the file in 'asserts' subfolder.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved it.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.



Describe "Update-FormatData with resources in CustomControls" -Tags "CI" {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file has multiple wrong indentations. See raw view https://raw.githubusercontent.com/anmenaga/PowerShell/fd95dd19b90e7cb5d0094f52503b59c59ec1ca5b/test/powershell/Modules/Microsoft.PowerShell.Utility/Update-FormatData.Tests.ps1
It seems you copy-paste tabs.
Could you please reformat by first commit and put new code in next commits?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed indentations.

F438 Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closed.

@TravisEz13
Copy link
Member

@adityapatwardhan Can you update your review?

@TravisEz13 TravisEz13 dismissed adityapatwardhan’s stale review July 21, 2017 18:21

PR Changed since review

Copy link
Member
@TravisEz13 TravisEz13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues

$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.HadErrors | Should be $false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would testing $ps.Streams.Error give a more meaningful error if something went wrong in the test?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea; updated.

$null = $ps.AddScript("Update-FormatData -PrependPath $formatFilePath")
$ps.Streams.Error.Clear()
$ps.Invoke()
$ps.HadErrors | Should be $false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated.

Copy link
Member
@adityapatwardhan adityapatwardhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@TravisEz13 TravisEz13 changed the title Making ResourceManagerCache check for alternative resource paths Make ResourceManagerCache check for alternative resource paths Aug 4, 2017
@TravisEz13 TravisEz13 merged commit 99236b1 into PowerShell:master Aug 4, 2017
@anmenaga anmenaga deleted the ResourceManagerCachePatch branch October 31, 2018 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Errors occurred while loading the format data file
0