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

First value in ForEach-Object loop becomes null when preceded by Switch statement in Pode file #1503

Open
helpimnotdrowning opened this issue Feb 24, 2025 · 0 comments
Labels

Comments

@helpimnotdrowning
Copy link

helpimnotdrowning commented Feb 24, 2025

Describe the Bug

When a foreach-object loop (@(...) | % {...}) is anywhere preceded by a switch statement in a .pode file, the first element of that loop will become null. This does not affect foreach statement loops.

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create the following pode file "test.pode":
$(
	switch (1) {
		default {}
	}
	
	foreach ( $x in @(1,2,3,4) ) {
		if ($x -eq $null) {
			write-warning "X item was null!"
		} else {
			write-host "X looking at $x"
		}
	}
	write-host '---'
	
	@(1,2,3,4) | ForEach-Object {
		if ($_ -eq $null) {
			write-warning "item was null!"
		} else {
			write-host "looking at $_"
		}
	}
)
  1. Run the following Pode server in the same directory:
import-module pode

Start-PodeServer {
	New-PodeLoggingMethod -Terminal | Enable-PodeErrorLogging -Levels Error, Warning, Informational
	Add-PodeEndpoint -Address localhost -Port 8080 -Protocol Http
	
	Add-PodeRoute -Method GET -Path '/*' -ScriptBlock {
		Write-PodeFileResponse -path test.pode
	}
}
  1. Open or get http://localhost:8080
  2. In the terminal, the following will print out:
X looking at 1
X looking at 2
X looking at 3
X looking at 4
---
WARNING: item was null!
looking at 2
looking at 3
looking at 4

Lines starting with X come from the foreach statement loop, while the ones that don't come from the foreach-object loop. In the second loop, the first element is null.

Removing/commenting the switch statement, as well as moving it to after the second loop, will print the expected output:

X looking at 1
X looking at 2
X looking at 3
X looking at 4
---
looking at 1
looking at 2
looking at 3
looking at 4

Expected Behavior

The first element of the foreach-object loop should be accessible like normal.

Platform

  • OS: Linux, Debian 12
  • Browser: Firefox 135
  • Versions:
    • Pode: Pode 2.12.0 (via PS Gallery)
    • PowerShell: 7.5.0

Notes

While writing this test case, I noticed that replacing the single quotes with double quotes in the line write-host '---' throws errors aswell:

...
Category: NotSpecified: (:) [ConvertFrom-PodeFile], MethodInvocationException
Message: Exception calling "Create" with "1" argument(s): "At line:15 char:2
+     @(1,2,3,4) | ForEach-Object {
+     ~~
Unexpected token '@(' in expression or statement."
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Backlog
Development

No branches or pull requests

1 participant