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

Start a related project to parse and format Makefiles #730

Closed
lassik opened this issue Sep 16, 2021 · 5 comments
Closed

Start a related project to parse and format Makefiles #730

lassik opened this issue Sep 16, 2021 · 5 comments

Comments

@lassik
Copy link

lassik commented Sep 16, 2021

There now exist dozens of autoformatters covering almost every popular language but I can't find one for makefiles.

Makefiles most declare build targets, with a small shell script written beneath each target. Hence it looks like the problem of makefile formatting boils down to a relatively simple make formatter which calls a complex shell formatter as a subroutine to do most of the actual work.

Given that shfmt already has excellent coverage of shell, would it interest you to try adding a makefile wrapper around it?

@mvdan
Copy link
Owner

mvdan commented Sep 16, 2021

There are quite a lot of languages which are relatively thin wrappers over shell, like Makefile and Dockerfile. I agree it feels like a nice fit for parsing and even formatting.

We do something kinda similar for Bats right now; it's an extension of the bash language for testing, and adds @test as a reserved word for that purpose. Supporting that in the parser is trivial, and just adds one node type. One can also argue that supporting Bats directly is reasonable, since it's the de facto standard for testing shell code.

Makefile (and by extension Dockerfile etc) are a bit less clear to me, because they are less directly related to developing shell code, and add significantly more syntax of their own. Makefile adds the target: blocks, annotations like .PHONY: target, as well as their own assignments like FOO = bar with multiple operators including :=. They even seem to have their own conditionals with ifeq. I'm just scratching the surface here, and it seems like adding Makefile support in the syntax package would easily add 10 times as much code as Bats support did.

All that said, I think it would be a very good idea to start a project building on top of this module's syntax package to parse and format Makefiles (or Dockerfiles etc). For example, see jessfraz/dockfmt#2. I'm more than happy to help out where I can, such as by improving the API if that is needed to integrate better at a low level.

I personally think that "Makefile parser+formatter" idea belongs in a different repository, simply because I do not have the bandwidth to write and maintain it myself, and do not want to become a bottleneck. I hope that makes sense. We could link these closely related projects in the README, though - see https://github.com/mvdan/sh#related-projects :)

@lassik
Copy link
Author

lassik commented Sep 16, 2021

Thanks for the encouraging response! That sounds like a good plan.

I probably won't have time to write a makefmt, but for anyone who wants to try it, GNU make probably has the most complex syntax of the various make variants floating around. Here are the key parts:

Line splitting is handled differently in recipes (i.e. embedded shell scripts) and non-recipe parts.

It looks like the most obvious strategy for formatting all that is to read in "logical lines" and tokenize each line. Then write out the tokens (space-separated in most case), starting a new line when a token would go over the width limit.

Some projects use makefiles where the recipes are written in Windows batch file (cmd.exe) syntax, so it may be prudent to add an option for that at some point.

@lassik
Copy link
Author

lassik commented Sep 16, 2021

Bonus: here's the POSIX make specification, written in manpage style.

@mvdan mvdan changed the title Could shfmt format makefiles? Start a related project to parse and format Makefiles Sep 16, 2021
@mvdan
Copy link
Owner

mvdan commented Sep 16, 2021

Sounds good, thanks for the info. I've repurposed the issue to see if anyone would be interested in starting this; we can leave it open for a while and see.

@mvdan
Copy link
Owner

mvdan commented Sep 29, 2024

It has been three years; it's time to close this as there is no interest. I'm still happy to assist here and there if someone wants to take this on, but it doesn't make sense to track it here.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Sep 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants