Search code examples
shellmakefile

Make lint syntax error: unexpected end of file running a multi-line shell fragment


I have lint target in Makefile

lint:
    @echo "Checking with SwiftLint"
    if test -d "/opt/homebrew/bin/"; then
        PATH="/opt/homebrew/bin/:${PATH}"
    fi

    export PATH

    if which swiftlint > /dev/null; then
        swiftlint
    else
        echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"
    fi

When I run make lint it shows me syntax error: unexpected end of file

I've checked with Shellcheck and it shows that everything is legit


Solution

  • Every physical line in a recipe is executed by a separate shell instance. You need (mostly) this entire recipe to be execute by one shell instance; for that you need to join the physical lines into a single logical line using line continuation.

    lint:
        @echo "Checking with SwiftLint"
        if test -d "/opt/homebrew/bin/"; then \
            PATH="/opt/homebrew/bin/:${PATH}"; \
        fi; \
        export PATH; \
        if which swiftlint > /dev/null; then \
            swiftlint; \
        else \
            echo "warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint"; \
        fi
    

    Notice the expicit semicolons; because the line continuation effectively erases the newlines from the command, you need command terminators so that the shell parses your command correctly.