elm / compiler

Compiler for Elm, a functional language for reliable webapps.
https://elm-lang.org/
BSD 3-Clause "New" or "Revised" License
7.55k stars 662 forks source link

Bug: It's possible to compile a main function as an application when the project is a package #2256

Open jfmengels opened 2 years ago

jfmengels commented 2 years ago

Quick Summary: It is possible to run elm make <file containing main> which will output an index.html file (or js when using --output), even when the Elm project is a package.

SSCCE

I created a repository to showcase the problem: https://github.com/jfmengels/elm-package-with-application-sscce The project is a package, but you can run elm make src/Main.elm and it will output an index.html file.

Additional Details

I believe this to be an oversight? I do think it's unwanted behavior and one not to recommend, at least for the fact the dependencies used for building the app are not "locked" like they would in an application project.

The behavior I expect would be that running elm make <file containing main> typechecks the file but doesn't output anything.

github-actions[bot] commented 2 years ago

Thanks for reporting this! To set expectations:

Finally, please be patient with the core team. They are trying their best with limited resources.

gampleman commented 2 years ago

I think this can be useful for testing the package in browser.

jfmengels commented 2 years ago

@gampleman You can do the same thing using a separate project where you add the package's source code under source-directories. That is a much cleaner separation IMO.

elm-review packages even have a system where there is an example folder, which uses the latest published package as a dependency, meaning people can copy-paste it and use it as a starter project (not easy to do if your example is in the package's source). There is also a preview folder, which is the same but it uses the source-directory method instead of the package as a dependency, so you can try out the package with the unpublished changes.

At publish or version bump time, I have a script to regenerate the example folder from the preview one.


A bit on the backstory behind the issue: I noticed this issue in elm-book's codebase where the documentation/example application for the project is in the src/ folder. I then tried to run elm-review and got a lot of false positives because main is not considered a special variable when the project is a package (and was therefore considered unused and to be removed).

I'd be fine with changing that behavior in elm-review, but I believe this to be an oversight with a nicer workaround. If this behavior is to be supported officially, I'll change the behavior of elm-review to match it.

gampleman commented 2 years ago

Sure you can do something else, but personally I much prefer the simplicity of this (especially during initial development) than fuffing around with extra directories and trying to keep multiple elm.json's in sync.