From 2ca84eea8686d63d1add2c958c1f9771b0fa3879 Mon Sep 17 00:00:00 2001 From: Jordan Borean Date: Thu, 30 Oct 2025 04:14:45 +1000 Subject: [PATCH] [stable-2.20] Remove AddType warning for cleanup (#86029) (#86039) * Remove AddType warning for cleanup (#86029) Removes the warning emitted when using Add-Type and the cleanup of temp files fails due to a file still being in use. The cleanup should be handled by AnsibleModule on exit giving it more time to wait for any open file handles to close. The exception is still present if calling `Add-CSharpType` without an `AnsibleModule` object. (cherry picked from commit 99bb587906e20742c1e009e3247022073be83ae7) * Update win_exec_wrapper integration test to match #86029 (#86052) * Remove assertion now that there is no warning (cherry picked from commit df34bf9e70115f8f67bb2cb712f05a3c21be4121) --------- Co-authored-by: Sloane Hertel <19572925+s-hertel@users.noreply.github.com> --- changelogs/fragments/add-type-warning.yml | 5 +++++ .../powershell/Ansible.ModuleUtils.AddType.psm1 | 16 +++++++++------- .../targets/win_exec_wrapper/tasks/main.yml | 8 -------- 3 files changed, 14 insertions(+), 15 deletions(-) create mode 100644 changelogs/fragments/add-type-warning.yml diff --git a/changelogs/fragments/add-type-warning.yml b/changelogs/fragments/add-type-warning.yml new file mode 100644 index 00000000000..aa08053d786 --- /dev/null +++ b/changelogs/fragments/add-type-warning.yml @@ -0,0 +1,5 @@ +bugfixes: + - >- + Windows - ignore temporary file cleanup warning when using AnsibleModule to + compile C# utils. This should reduce the number of warnings that can safely + be ignored when running PowerShell modules - https://github.com/ansible/ansible/issues/85976 diff --git a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 index 407fc0968a1..3a14608e1f4 100644 --- a/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 +++ b/lib/ansible/module_utils/powershell/Ansible.ModuleUtils.AddType.psm1 @@ -278,10 +278,16 @@ Function Add-CSharpType { if ($PSCmdlet.ParameterSetName -eq "Module") { $temp_path = $AnsibleModule.Tmpdir $include_debug = $AnsibleModule.Verbosity -ge 3 + + # AnsibleModule will handle the cleanup after module execution + # which should be enough time for AVs or other processes to release + # any locks on the temp files. + $tmpdir_clean_is_error = $false } else { $temp_path = [System.IO.Path]::GetTempPath() $include_debug = $IncludeDebugInfo.IsPresent + $tmpdir_clean_is_error = $true } $temp_path = Join-Path -Path $temp_path -ChildPath ([Guid]::NewGuid().Guid) @@ -388,17 +394,13 @@ Function Add-CSharpType { } finally { # Try to delete the temp path, if this fails and we are running - # with a module object write a warning instead of failing. + # with a module object, ignore and let it cleanup later. try { [System.IO.Directory]::Delete($temp_path, $true) } catch { - $msg = "Failed to cleanup temporary directory '$temp_path' used for compiling C# code." - if ($AnsibleModule) { - $AnsibleModule.Warn("$msg Files may still be present after the task is complete. Error: $_") - } - else { - throw "$msg Error: $_" + if ($tmpdir_clean_is_error) { + throw "Failed to cleanup temporary directory '$temp_path' used for compiling C# code. Error: $_" } } } diff --git a/test/integration/targets/win_exec_wrapper/tasks/main.yml b/test/integration/targets/win_exec_wrapper/tasks/main.yml index 8b9b4b9dcbc..22d97a560d0 100644 --- a/test/integration/targets/win_exec_wrapper/tasks/main.yml +++ b/test/integration/targets/win_exec_wrapper/tasks/main.yml @@ -268,14 +268,6 @@ <<: *become_vars ansible_remote_tmp: C:\Windows\TEMP\test-dir - - name: assert warning about tmpdir deletion is present - assert: - that: - - temp_deletion_warning.warnings | count == 1 - - >- - temp_deletion_warning.warnings[0] is - regex("(?i).*Failed to cleanup temporary directory 'C:\\\\Windows\\\\TEMP\\\\test-dir\\\\.*' used for compiling C# code\\. Files may still be present after the task is complete\\..*") - always: - name: ensure test user is deleted win_user: