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

Fix: Replace Clear-Content with Out-File #66

Conversation

hche608
Copy link

@hche608 hche608 commented Feb 7, 2024

Seems like Clear-Content is creating a new file and replaces the old one with it, while Out-File tries to write into an existing file (so opened read handles do not effect this)

https://stackoverflow.com/questions/46724371/delete-clear-opened-text-file-which-is-used-by-another-process

@leojonathanoh
Copy link
Member

Ah, I see now, nice catch and information. Thank you for that finding. One little request, could you prefix the PR title with 'Fix: ' to follow conventional commits, for E.g. 'Fix: Replace Clear-Content with Out-File'. It should be good then, thank you 😄

@leojonathanoh leojonathanoh self-assigned this Feb 7, 2024
@leojonathanoh leojonathanoh added this to the v1.6.1 milestone Feb 7, 2024
@leojonathanoh leojonathanoh linked an issue Feb 7, 2024 that may be closed by this pull request
@@ -29,7 +29,11 @@ function New-LogObject {
if ($copytruncate) {
Write-Verbose "Truncating $my_fullname"
if (!$WhatIf) {
Clear-Content $my_fullname
if ($PSVersionTable.PSVersion.Major -le 6) {
"" | Out-File $my_fullname -NoNewline -Encoding ASCII
Copy link
Member

@leojonathanoh leojonathanoh Feb 7, 2024

Choose a reason for hiding this comment

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

Hmm why the if else? It would mean Windows Powershell (<6) will output ASCII encoding, and Powershell Core (>=6) will output UTF8, which means Windows Powershell users won't get a UTF8 encoding.

Copy link
Author

Choose a reason for hiding this comment

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

because replacing ASCII with utf8 or removing encoding, will fail in Windows Server 2019 ps 5.1 build

Copy link
Member

@leojonathanoh leojonathanoh Feb 8, 2024

Choose a reason for hiding this comment

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

which component of Windows Server 2019 are you using, that fails with utf8 files? Most modern applications, and my guess with Windows Server is that it would fully support utf8 by now.

Copy link
Author

@hche608 hche608 Feb 8, 2024

Choose a reason for hiding this comment

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

which component of Windows Server 2019 are you using, that fails with utf8 files? Most modern applications, and my guess with Windows Server is that it would fully support utf8 by now.

ci-master-pr / test-powershell-5-1-windows-2019 will fail if encoding with utf8

error build
https://github.com/theohbrothers/Log-Rotate/actions/runs/7811576564/job/21306915375?pr=65

@leojonathanoh leojonathanoh removed this from the v1.6.1 milestone Feb 7, 2024
@hche608 hche608 changed the title replaced Clear-Content with "" | Out-File Fix: Replace Clear-Content with Out-File Feb 7, 2024
@leojonathanoh
Copy link
Member

Fixed in #67

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

copytruncate failing because file is being used by another process
2 participants