PowerShell / PowerShell/PowerShell

`using` statements aren't allowed in script-block literals, but are in script blocks created with `[scriptblock]::Create()`

未关闭
#19,985 0 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs-Triage WG-Language
主要语言
C#
星标
55.5k
派生
8.5k
平均合并
1 天 2 小时
30 天内合并 PR
88

描述

Prerequisites
Steps to reproduce
  • using statements are disallowed in script-block literals, whereas
  • source code passed to [scriptblock]::Create() permits them.

I don't if there's a good reason not to allow them - at least a first glance, they seem to work as intended - but the inconsistency is worth resolving either way.

# Define a snippet of source code as  a string.
$sbText = 'using namespace System.Collections; [ArrayList]'

# OK script block created from string via [scriptblock]::Create()
& ([scriptblock]::Create($sbText))

# !! BROKEN: A script block *literal* with the same source code
# !!         doesn't permit `using` statements.
Invoke-Expression "& { $sbText }"
Expected behavior

Both script-block calls should succeed and output the [System.Collections.ArrayList] type.

Actual behavior

The 2nd call, based on a script-block literal, fails with A 'using' statement must appear before any other statements in a script.

Error details

No response

Environment data
PowerShell 7.4.0-preview.4
Visuals

No response

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先复现 issue 中的两个示例:一个脚本块字面量,以及一个使用 [scriptblock]::Create() 创建的脚本块。比较它们各自如何处理 using 语句,并确定是否应使它们的行为保持一致。完成的标准是文档中的示例相互一致,并且预期的 ArrayList 类型输出能够成功生成且不出现顺序错误。

由索引模型根据 Issue 内容生成。

评估

技术栈
powershell
领域
cli
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。