Powershell runner starts with errors in the global $Error variable (regression since powershell scripts are passed by stdin)

Summary

The $Error automatic variable already contains things immediately at the beginning of execution of a script, using the powershell shell in gitlab-runner.

$Error contains the following error a few times: Missing closing '}' in statement block or type definition..

This started happening sometime between 14.8.2 and 15.10.0, and is still present in 16.0.1 (I did not attempt to find the exact version where the problem was introduced)

This is problematic since failing the build when $Error is not empty is a common pattern.

Steps to reproduce

This can easily be reproduced using the following gitlab-ci.yml:

test:
  script:
    - $Error

and then executing the following command: .\gitlab-runner.exe exec shell --shell powershell test to run the job locally.

What is the current bug behavior?

Output of the previous command on version 15.10.0 of the runner:

Runtime platform                                    arch=amd64 os=windows pid=9532 revision=456e3482 version=15.10.0
WARNING: You most probably have uncommitted changes.
WARNING: These changes will not be tested.
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Running with gitlab-runner 15.10.0 (456e3482)
Preparing the "shell" executor
Using Shell (powershell) executor...
executor not supported                              job=1 project=0 referee=metrics
Preparing environment
Running on MYSERVERNAME...
Getting source from Git repository
Fetching changes...
Initialized empty Git repository in D:/Gitlab-Runner/asdf/builds/0/project-0/.git/
Created fresh repository.
Checking out da67c2ae as detached HEAD (ref is master)...
git-lfs/3.3.0 (GitHub; windows amd64; go 1.19.3; git 77deabdf)

Skipping Git submodules setup
Executing "step_script" stage of the job script
$ $Error
Missing closing '}' in statement block or type definition.
Missing closing '}' in statement block or type definition.
Missing closing '}' in statement block or type definition.
Missing closing '}' in statement block or type definition.
Missing closing '}' in statement block or type definition.
Job succeeded

What is the expected correct behavior?

Output of the previous command on version 14.8.2 of the runner:

Runtime platform                                    arch=amd64 os=windows pid=5212 revision=c6e7e194 version=14.8.2
WARNING: You most probably have uncommitted changes.
WARNING: These changes will not be tested.
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
Running with gitlab-runner 14.8.2 (c6e7e194)
Preparing the "shell" executor
Using Shell executor...
executor not supported                              job=1 project=0 referee=metrics
Preparing environment
Running on MYSERVERNAME...
Getting source from Git repository
Fetching changes...
Initialized empty Git repository in D:/Gitlab-Runner/asdf/builds/0/project-0/.git/
Created fresh repository.
Checking out da67c2ae as master...
git-lfs/3.3.0 (GitHub; windows amd64; go 1.19.3; git 77deabdf)

Skipping Git submodules setup
Executing "step_script" stage of the job script
$ $Error
Job succeeded

Workaround:

It's possible to do a $Error.Clear() at the beginning of the script to avoid problems further down...

EDIT: I discovered that setting FF_DISABLE_POWERSHELL_STDIN to true also prevents the issue described above from happening.
So it seems the issue is caused by the recent-ish changes in !3728 (merged) (v15.6.0 according to the changelog)

Edited by Melvyn Laïly