Skip to content

Replace custom method with File.ReadAllText() in ScriptAnalysis.cs#26060

Merged
iSazonov merged 5 commits into
PowerShell:masterfrom
amritanand-py:master
Oct 15, 2025
Merged

Replace custom method with File.ReadAllText() in ScriptAnalysis.cs#26060
iSazonov merged 5 commits into
PowerShell:masterfrom
amritanand-py:master

Conversation

@amritanand-py
Copy link
Copy Markdown
Contributor

@amritanand-py amritanand-py commented Sep 16, 2025

Fix #25136

The ReadScript helper method in src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs was redundant. It created a FileStream and StreamReader only to return the file’s contents, which can be achieved directly with File.ReadAllText(path, Encoding.Default).

This PR removes the ReadScript method and replaces all call sites with File.ReadAllText.

Changes

Removed the ReadScript method.

Updated all usages to:

File.ReadAllText(path, Encoding.Default)

PR Summary

This PR simplifies file reading in ScriptAnalysis.cs by removing the unnecessary ReadScript helper method. The built-in File.ReadAllText method provides the same functionality in a cleaner and more efficient way.

Removed:
ReadScript method and replaces all call sites with File.ReadAllText.

PR Context

Extra code in ScriptAnalysis.cs #25136

PR Checklist

…ine\Modules\ScriptAnalysis.cs

The ReadScript method was retrieving a SafeFileHandle from the FileStream but never using it.
This line was unnecessary since proper disposal is already handled by the using blocks.

Changes

Removed redundant SafeFileHandle assignment.
@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Sep 16, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@xtqqczze
Copy link
Copy Markdown
Contributor

This may be a change in behavior, e.g. if the SafeFileHandle getter throws.

@amritanand-py
Copy link
Copy Markdown
Contributor Author

Yes, removing the SafeFileHandle line does technically change behavior, because the getter could throw in some scenarios (e.g., if the stream was already disposed or in restricted environments).

However, since the handle value was never used, this call didn’t contribute to the method’s functionality.

The method now only depends on FileStream and StreamReader, both of which are already safely wrapped in using.

@xtqqczze
Copy link
Copy Markdown
Contributor

The same functionality could be implemented with File.ReadAllText Method.

@amritanand-py
Copy link
Copy Markdown
Contributor Author

That’s true ,File.ReadAllText could achieve the same result in a single call.

In this PR, my intent was only to remove the unused SafeFileHandle line to clean up the existing implementation without altering its overall structure, as stated in issue #25136 (Extra code in ScriptAnalysis.cs).

If replacing ReadScript with File.ReadAllText is desirable, I’d be happy to either update this PR to use File.ReadAllText, or open a follow-up PR focused on that refactoring.

@xtqqczze
Copy link
Copy Markdown
Contributor

We might as well remove the method and replace the call sites with File.ReadAllText(path, Encoding.Default).

@amritanand-py
Copy link
Copy Markdown
Contributor Author

I will update this PR with Refactor commit

amritanand-py and others added 2 commits September 17, 2025 14:45
The ReadScript helper method has been removed, and all call sites have been updated to use File.ReadAllText(path, Encoding.Default) directly.
Changes

Deleted the ReadScript method from ScriptAnalysis.cs.

Replaced all usages of ReadScript with:
File.ReadAllText(path, Encoding.Default)
@amritanand-py
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@xtqqczze
Copy link
Copy Markdown
Contributor

@amritanand-py Please update the PR description.

@amritanand-py
Copy link
Copy Markdown
Contributor Author

@xtqqczze Done

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Oct 1, 2025
return result;
}

internal static string ReadScript(string path)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MartinGC94 Do you think it is good to remove the method? Won't this code be less readable?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's perfectly fine. If anything it's arguably easier to understand when it's just File.ReadAllText because you don't have to check if ReadScript does anything weird.

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 3, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Review - Needed The PR is being reviewed label Oct 3, 2025
@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 7, 2025

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@iSazonov
Copy link
Copy Markdown
Collaborator

iSazonov commented Oct 7, 2025

@amritanand-py Please add dummy commit to unlock merging.

@xtqqczze
Copy link
Copy Markdown
Contributor

xtqqczze commented Oct 7, 2025

@amritanand-py Please add dummy commit to unlock merging.

You can add an empty commit using:

git commit --allow-empty --message 'Empty commit'

@amritanand-py
Copy link
Copy Markdown
Contributor Author

@xtqqczze Done

@iSazonov iSazonov changed the title Remove unused SafeFileHandle in PowerShell/src/System.Management.Automation/engine/Modules/ScriptAnalysis.cs Replace custom method with File.ReadAllText() ScriptAnalysis.cs Oct 8, 2025
@iSazonov iSazonov changed the title Replace custom method with File.ReadAllText() ScriptAnalysis.cs Replace custom method with File.ReadAllText() in ScriptAnalysis.cs Oct 8, 2025
@iSazonov iSazonov enabled auto-merge (squash) October 8, 2025 03:44
@microsoft-github-policy-service microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Oct 15, 2025
@xtqqczze
Copy link
Copy Markdown
Contributor

@iSazonov It looks like auto-merge isn’t working?

@iSazonov
Copy link
Copy Markdown
Collaborator

/azp run PowerShell-Windows-Packaging-CI, PowerShell-CI-linux-packaging

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines could not run because the pipeline triggers exclude this branch/path.

@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Review - Needed The PR is being reviewed label Oct 15, 2025
@iSazonov iSazonov added CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log and removed CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log labels Oct 15, 2025
@iSazonov iSazonov merged commit b77c80e into PowerShell:master Oct 15, 2025
41 of 42 checks passed
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

microsoft-github-policy-service Bot commented Oct 15, 2025

📣 Hey @@amritanand-py, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗 https://aka.ms/PSRepoFeedback

@iSazonov
Copy link
Copy Markdown
Collaborator

@amritanand-py Thanks for your contribution!

@amritanand-py
Copy link
Copy Markdown
Contributor Author

Thank you! Glad I could help. Looking forward to contributing more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extra code in ScriptAnalysis.cs

4 participants