Bug 269663 - bmake: Interrupted phony target removes a file
Summary: bmake: Interrupted phony target removes a file
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 13.1-RELEASE
Hardware: Any Any
: --- Affects Only Me
Assignee: Simon J. Gerraty
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-02-18 16:11 UTC by Mateusz Piotrowski
Modified: 2023-04-25 12:08 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mateusz Piotrowski freebsd_committer freebsd_triage 2023-02-18 16:11:09 UTC
I have a target that is marked as phony. I also have a file with the same name as that phony target in my directory. When I interrupt the target, it removes the file. Here's the repro script:

    set -eu

    # Try to detect bmake.
    make="$(which bmake || which make)"

    rm -rf work
    mkdir work
    cd work

    # Create a file with the same name as the make target we are about to run.
    touch aaa

    # Mark target aaa as phony.
    {
        printf '%s\n'   ".PHONY: aaa"
        printf '%s\n'   "aaa:"
        printf '\t%s\n' "sleep 10"
    } > Makefile

    # Time out before the aaa target completes.
    #
    # NB: The return code of the timeout command is not 0,
    # so temporarily turn of set -e.
    set +e
    timeout 2s make aaa
    set -e

    # Verify that the aaa file is still there.
    if [ -f aaa ]; then
            echo OK
            exit 0
    else
            echo FAIL
            exit 1
    fi


I've tested that on FreeBSD 13.1-RELEASE amd64.

Interestingly, I cannot reproduce it on macOS with bmake 20230208.
Comment 1 Simon J. Gerraty freebsd_committer freebsd_triage 2023-02-19 02:46:50 UTC
bmake is behaving as documented.

You can used `.PRECIOUS` to indicate that the target should not be removed,
and if you have a src file and target with same name use `.NOPATH` to indicate that the target should not be searched for via `.PATH`
Comment 2 Mateusz Piotrowski freebsd_committer freebsd_triage 2023-02-20 14:14:22 UTC
(In reply to Simon J. Gerraty from comment #1)

Thank you for your reply!

I assumed that if you have .PHONY set, then bmake does not treat the target as a file (that's what the manual says):

     .PHONY    The target does not correspond to an actual file; it is always
               considered to be out of date, and will not be created with the
               -t option.

That's why I assumed there is no need to set .PRECIOUS and .NOPATH.

Am I misunderstanding something?
Comment 3 Simon J. Gerraty freebsd_committer freebsd_triage 2023-03-17 22:52:19 UTC
Hi, 

Yes .PHONY effectively implies .NOPATH

As for needing .PRECIOUS the behavior depends on whether we are in jobs mode.
JobDeleteTarget will skip for PHONY as well as PRECIOUS
but CompatDeleteTarget only looks at PRECIOUS - that should be fixed.

Right now you need .PRECIOUS to protect your .PHONY target in compat mode.
Comment 4 Mateusz Piotrowski freebsd_committer freebsd_triage 2023-04-25 12:08:45 UTC
The newest import of bmake into 14.0-CURRENT (bmake-20230414) seems to fix the reported issue. Thank you!