Skip to content

Port GetProcessorArchitecture() - #11046

Merged
Ilya (iSazonov) merged 8 commits into
PowerShell:masterfrom
iSazonov:port-getprocessorarchitecture
Jan 14, 2020
Merged

Ilya (iSazonov) merged 8 commits into
PowerShell:masterfrom
iSazonov:port-getprocessorarchitecture

Conversation

@iSazonov

@iSazonov Ilya (iSazonov) commented Nov 12, 2019 •

Copy link
Copy Markdown
Collaborator

PR Summary

Fix #4474
Fix #6533

Before the change GetProcessorArchitecture() call Windows P/Invoke so on Unix it would throw if user loaded a module with ProcessorArchitecture value in the module manifest. Now we use ProcessorArchitecture from current assembly.

PR Context

PR Checklist

@iSazonov Ilya (iSazonov) added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Nov 12, 2019
@iSazonov Ilya (iSazonov) added this to the 7.0.0-preview.6 milestone Nov 12, 2019
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Do you agree to merge the PR before GA?

@iSazonov Ilya (iSazonov) self-assigned this Nov 14, 2019
@iSazonov

Ilya (iSazonov) commented Nov 15, 2019 •

Copy link
Copy Markdown
Collaborator Author

Aditya Patwardhan (@adityapatwardhan) Please review the PR again.

After looking in depth how the method is used I very wonder that we implement the conversion of these two enums if we can request current value from an assembly using Reflection.
On my system typeof(object).Assembly.GetName().ProcessorArchitecture returns Amd64.

@anmenaga

Copy link
Copy Markdown

Ilya (@iSazonov) , can you please double check that currently GetProcessorArchitecture() returns system-wide info and comment on this function is wrong?
If that's the case, then probably to avoid future confusion:

  1. current GetProcessorArchitecture() should be renamed across the code to something like GetSystemProcessorArchitecture(), and comment updated;
  2. since the functionality of getting current process arch is also needed, code of this PR should go into PsUtils as a separate method like GetCurrentProcessProcessorArchitecture().

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Andrew (@anmenaga) If the name confuses you we could remove the method at all because it is used in one place/scenario. Th scenario is a module loading. We read ProcessorArchitecture parameter from the module manifest and check that we can load the module. The check is to compare the ProcessorArchitecture parameter and ProcessorArchitecture property from current assembly. And we can do this directly. I pulled the commit.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Andrew (@anmenaga) Please update your review.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Andrew (@anmenaga) Could you please continue?

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Andrew (@anmenaga) I think the PR could be in 7.0.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) Sorry, I did not saw the issue. New tests are added.

@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) It seems I need help to fix tests on Unix-s. :-( I guess SMA compiled to MSIL on Unix-s. In the case I don't know how make functional tests on Unix-s.

Get-TP -ErrorAction SilentlyContinue | Should -BeExactly "$arch"

Remove-Module "TP_$arch"
Remove-Item -LiteralPath $testFolder -Recurse -Force -ErrorAction SilentlyContinue > $null

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should have in finally to ensure this cleans up if test fails for some reason

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Add-Type -TypeDefinition $a -CompilerOptions "/platform:$roslynArch" -OutputAssembly $assemblyPath
New-ModuleManifest -Path $modulePath -NestedModules "TP_$arch.dll" -RequiredAssemblies "TP_$arch.dll" -ProcessorArchitecture $arch -CmdletsToExport "Get-TP"
Import-Module $modulePath

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Extra whitespace

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@ghost ghost added the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 10, 2020
@SteveL-MSFT

Copy link
Copy Markdown
Member

Ilya (@iSazonov) You can probably not even worry about Add-Type, the module manifest issue was at validation time when it is imported so you can just generate the module manifest with ProcessorArchitecture and it should succeed on compatible arch and fail on not-compatible arch

@ghost ghost removed the Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept label Jan 11, 2020
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) I try to create functional tests to ensure that scenario like #6533 really works. But test failures show that perhaps the PR doesn't fix #6533 in general (the PR only removes broken p/invoke but do not ensure that import-module works) because Roslyn works in another way on Unix-s and probably loader too. Now I set to skip new tests on Unix-s.

@iSazonov
Ilya (iSazonov) merged commit 0dfeeb5 into PowerShell:master Jan 14, 2020
@iSazonov
Ilya (iSazonov) deleted the port-getprocessorarchitecture branch January 14, 2020 03:41
@iSazonov

Copy link
Copy Markdown
Collaborator Author

Notice. The code was not covered by tests. Now only Windows on Intel is covered. Unix-s and Arm are not covered.

@ghost

Copy link
Copy Markdown

🎉v7.0.0-rc.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Module manifest .psd1 file on linux/mac doesn't support ProcessorArchitecture='AMD64' or 'x86' GetProcessorArchitecture in PSUtil.cs is not ported

7 participants