Skip to content

Conversation

@aheejin
Copy link
Member

@aheejin aheejin commented Oct 23, 2025

--preserve-manifest was doing the opposite of what it meant to do: deleting the manifest file when --preserve-manifest was given and not deleting it when it was not given.

But simply changing

with tempfile.NamedTemporaryFile(suffix=".manifest", mode='w+', delete=args.preserve_manifest) as f:
with tempfile.NamedTemporaryFile(suffix=".manifest", mode='w+', delete=not args.preserve_manifest) as f:

doesn't work, because of Windows' peculiar behavior.

It looks in Windows it is not allowed to re-open an already opened file unless some conditions are met:
https://docs.python.org/3/library/tempfile.html

Opening the temporary file again by its name while it is still open works as follows:

  • On POSIX the file can always be opened again.
  • On Windows, make sure that at least one of the following conditions are fulfilled:
    • delete is false
    • additional open shares delete access (e.g. by calling os.open() with the flag O_TEMPORARY)
    • delete is true but delete_on_close is false. Note, that in this case the additional opens that do not share delete access (e.g. created via builtin open()) must be closed before exiting the context manager, else the os.unlink() call on context manager exit will fail with a PermissionError.

And we are trying to reopen a temporary file within the with context. The Windows CI didn't error out before this PR because the first condition (delete is false) was accidentally satisfied due to this bug. To fix this I think we can't help but use try-finally.

`--preserve-manifest` was doing the opposite of what it meant to do:
deleting the manifest file when `--preserve-manifest` was given and not
deleting it when it was not given.

But simply changing
```py
with tempfile.NamedTemporaryFile(suffix=".manifest", mode='w+', delete=args.preserve_manifest) as f:
```
```py
with tempfile.NamedTemporaryFile(suffix=".manifest", mode='w+', delete=not args.preserve_manifest) as f:
```
doesn't work, because of Windows' peculiar behavior.

It looks in Windows it is not allowed to re-open an already opened file
unless some conditions are met:
https://docs.python.org/3/library/tempfile.html
> Opening the temporary file again by its name while it is still open
> works as follows:
> - On POSIX the file can always be opened again.
> - On Windows, make sure that at least one of the following conditions
>   are fulfilled:
>   - `delete` is false
>   - additional open shares delete access (e.g. by calling
>     [os.open()](https://docs.python.org/3/library/os.html#os.open)
>     with the flag `O_TEMPORARY`)
>   - `delete` is true but `delete_on_close` is false. Note, that in
>     this case the additional opens that do not share >delete access
>     (e.g. created via builtin
>     [open()](https://docs.python.org/3/library/functions.html#open))
>     must be closed before exiting the context manager, else the
>     [os.unlink()](https://docs.python.org/3/library/os.html#os.unlink)
>     call on context manager exit will fail with a
>     [PermissionError](https://docs.python.org/3/library/exceptions.html#PermissionError).

And we are trying to reopen a temporary file within the `with` context.
The Windows CI didn't error out before this PR because the first
condition (`delete` is false) was accidentally satisfied due to this
bug. To fix this I think we can't help but use `try`-`finally`.
@aheejin aheejin requested a review from sbc100 October 23, 2025 21:45
Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Thanks windows.

@aheejin aheejin merged commit eb36294 into emscripten-core:main Oct 24, 2025
34 checks passed
@aheejin aheejin deleted the preserve_manifest_fix branch October 24, 2025 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants