Repository navigation
Closed
Labels
7.4-regressionRegression in 7.4Regression in 7.4In-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issueNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.The behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working GroupNeeds a review by the labeled Working Group
Description
Prerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Executing the following script in PowerShell 7.4.0 results in an unexpected behavior which also differs from PowerShell 5.1
function foo($x) {
$val = switch ($x){
1 { 5 }
default {
return 10
}
}
Write-Host "Val is $val"
$val + 1
}
Write-Host "foo 1: $(foo 1)"
Write-Host "foo 2: $(foo 2)"
PS 5.1 output:
Val is 5
foo 1: 6
foo 2: 10
PS 7.4.0 output:
Val is 5
foo 1: 6
foo 2:
It's not clear if this is a bug or simply a documente change from classic PowerShell, but it certainly behaves in a way I don't find intuitive.
Expected behavior
PS> <execute script>
Val is 5
foo 1: 6
foo 2: 10Actual behavior
PS> <execute script>
Val is 5
foo 1: 6
foo 2:Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.4.0
PSEdition Core
GitCommitId 7.4.0
OS Microsoft Windows 10.0.19045
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Name Value
---- -----
PSVersion 5.1.19041.3570
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.3570
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1Visuals
No response
Reacted by Michael Klement
Metadata
Metadata
Assignees
Labels
7.4-regressionRegression in 7.4Regression in 7.4In-PRIndicates that a PR is out for the issueIndicates that a PR is out for the issueNeeds-InvestigationThe behavior reported in the issue is unexpected and needs further investigation.The behavior reported in the issue is unexpected and needs further investigation.WG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtimeWG-NeedsReviewNeeds a review by the labeled Working GroupNeeds a review by the labeled Working Group
Activity
IISResetMe commentedon Nov 21, 2023
I have a feeling this isn't specific to
switchstatements - it appears thereturnexpression ends up in the original assignment target when you return from any dot-sourced block:danstur commentedon Nov 21, 2023
A colleague just ran the above script in 7.3.4 and there the behavior is identical to PS 5.1 - so this seems to be a bug that was introduced with 7.4.0.
mklement0 commentedon Nov 21, 2023
The different behaviors can be demonstrated with the following simple command:
10.That is, what the behaviors share is that no assignment takes place, but that there is no output in 7.4.0 is clearly a bug.
The WinPS / PS 7.3- behavior may be surprising, but is defensible:
It is important to note that
returndoes not exit just a{ ... }-enclosed part of a language statement such asiforswitch, but its enclosing function/script file/script block.That is, the
{ ... }parts of such statements do not function like stand-alone script blocks such as the ones passed toForEach-Object, in whichreturntruly only exits that block.Therefore, in the original code the
returnimmediately exits the whole function, with 7.4.0 inexplicably discarding the output.danstur commentedon Nov 21, 2023
Michael Klement (@mklement0) The behavior of 7.3.x and previous is what I'd intuitively expect to happen when reading the code.
microsoft-github-policy-service commentedon Mar 25, 2024
📣 Hey Daniel Sturm (@danstur), how did we do? We would love to hear your feedback with the link below! 🗣️
🔗 https://aka.ms/PSRepoFeedback