compiler icon indicating copy to clipboard operation
compiler copied to clipboard

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

Open jfmengels opened this issue 3 years ago • 4 comments

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.

  • Elm: 0.19.1
  • Browser: NA
  • Operating System: Linux Ubuntu

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.

jfmengels avatar Apr 25 '22 20:04 jfmengels

Thanks for reporting this! To set expectations:

  • Issues are reviewed in batches, so it can take some time to get a response.
  • Ask questions in a community forum. You will get an answer quicker that way!
  • If you experience something similar, open a new issue. We like duplicates.

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

github-actions[bot] avatar Apr 25 '22 20:04 github-actions[bot]

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

gampleman avatar Apr 26 '22 10:04 gampleman

@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.

jfmengels avatar Apr 26 '22 11:04 jfmengels

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.

gampleman avatar Apr 26 '22 14:04 gampleman