Skip to content
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

Better backwards compatibility w/ 5.1 #18989

Closed
juliusl opened this issue Jan 20, 2023 · 6 comments
Closed

Better backwards compatibility w/ 5.1 #18989

juliusl opened this issue Jan 20, 2023 · 6 comments
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-Answered The question is answered. WG-Cmdlets general cmdlet issues WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module WG-Engine core PowerShell engine, interpreter, and runtime WG-Interactive-Debugging debugging PowerShell script WG-Language parser, language semantics

Comments

@juliusl
Copy link

juliusl commented Jan 20, 2023

Summary of the new feature / enhancement

There are a lot of changes between PS 5.X and PS 7.X, and some of these changes make keeping scripts backwards compatible difficult,

Some examples,

Invoke-WebRequest

The return type has changed and some useful parsing has been removed. This would be mostly tolerable but currently accessing headers is a pain in 7.1 is a bit painful. For example,

$response = Invoke-WebRequest "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.manifest"

API's like this would include Last-Modified as a header. In 5.1 this allowed direct access to the datetime via,

$lastModified = $response.BaseResponse.LastModified

In 7.X the return type has changed, so now it would be,

$response = Invoke-WebRequest "https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.manifest"
$lastModified = [DateTime]$response.BaseResponse.Headers.'Last-Modified'[0]

The problem is here, https://learn.microsoft.com/en-us/dotnet/api/microsoft.powershell.commands.webresponseobject.headers?view=powershellsdk-7.2.0#microsoft-powershell-commands-webresponseobject-headers, which is a Dictionary<String, IEnumerable<String>>.

In order to make this backwards compatible, you would need to write code like this,

$lastModified = $response.Headers.'Last-Modified'

if ($PSVersionTable.PSVersion.Major -gt 5) {
    $lastModified = [DateTime]$lastModified[0]
} else {
    $lastModified = [DateTime]$lastModified
}

Set-Content

Previously, byte encoding was an option in the Encoding parameter. So in 5.1, you could do this,

Set-Content "file" ([byte[]][char[]] "$content") -Encoding Byte

But in 7.X, it's now,

Set-Content "file" ([byte[]][char[]] "$content") -AsByteStream

So now to support backwards compatibility, you would need to write,

if ($PSVersionTable.PSVersion.Major -gt 5) {
  Set-Content "file" ([byte[]][char[]] "$content") -AsByteStream
} else {
  Set-Content "file" ([byte[]][char[]] "$content") -Encoding Byte
}

These are just two examples, and others may have some other examples, but as you can see these are very tiny inconsistencies that end up doubling code.

Proposed technical implementation details (optional)

I'm not sure how to address this problem, but I think part of the challenge is being able to collect feedback about specific backwards compat problems.

Other than that perhaps there could be some sort of polyfill solution, or some sort of dedicated module that bridges 5.1 <-> 7.X. Or maybe some code analysis rules added to the powershell language server for vscode.

If nothing else, maybe there needs to be some sort of knowledge base or wiki in this repo that documents these nuances.

I'm pretty sure it is probably too late to change binaries so maybe this is something 8.0 can address (although then there might be backwards compat issues between 7 -> 8).

@juliusl juliusl added Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. labels Jan 20, 2023
@kilasuit kilasuit added WG-Cmdlets general cmdlet issues WG-Interactive-Debugging debugging PowerShell script WG-Engine core PowerShell engine, interpreter, and runtime WG-Language parser, language semantics WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module labels Jan 20, 2023
@mklement0
Copy link
Contributor

mklement0 commented Jan 20, 2023

As for:

there needs to be some sort of knowledge base or wiki in this repo that documents these nuances.

There's an official page here, and I encourage you to suggest additions / corrections, as warranted: https://learn.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell


Fundamentally, the cross-edition changes fall into the following categories:

This article summarizes the differences and breaking changes from Windows PowerShell 5.1 and the current version of PowerShell that is based on .NET Core.

@iSazonov
Copy link
Collaborator

The gap between WinPS 5.1 and pwsh is already huge - there are probably hundreds of differences. The distance between them is increasing every day. We can assume that in 2-3 years the compatibility policy will have to be abandoned in fact.

@aguzev
Copy link

aguzev commented Jan 20, 2023

The PS7 version looks more generic because there is no guarantee that Last-Modified cannot have multiple values. An array of datetime looks like an alternative for the if monster:

PS> [datetime[]]("2022-12-29","2023-01-20")

Thursday, December 29, 2022 12:00:00 AM
Friday, January 20, 2023 12:00:00 AM

PS> [datetime[]]"2022-12-29"

Thursday, December 29, 2022 12:00:00 AM

@juliusl
Copy link
Author

juliusl commented Jan 20, 2023 via email

@theJasonHelmick theJasonHelmick added Resolution-Answered The question is answered. and removed Needs-Triage The issue is new and needs to be triaged by a work group. labels Feb 1, 2023
@theJasonHelmick
Copy link
Collaborator

Thank you @juliusl for taking the time to report this. PowerShell 7 is not intended to be in complete parity with Windows PowerShell 5.1. There are situations we have decided to not remain compatible. We feel that the your invoke-webrequest issue is something that should be investigated and encourage you to open it as a single issue.

@ghost
Copy link

ghost commented Feb 3, 2023

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

@ghost ghost closed this as completed Feb 3, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Enhancement the issue is more of a feature request than a bug Resolution-Answered The question is answered. WG-Cmdlets general cmdlet issues WG-Cmdlets-Core cmdlets in the Microsoft.PowerShell.Core module WG-Engine core PowerShell engine, interpreter, and runtime WG-Interactive-Debugging debugging PowerShell script WG-Language parser, language semantics
Projects
None yet
Development

No branches or pull requests

6 participants