-
Notifications
You must be signed in to change notification settings - Fork 843
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
infer --package options from imports in standalone scripts #1944
Comments
This is something that I would really like to have. From an implementation perspective it will require a bit more parsing now. Till now we have had simple attoparsec parser for the hashbang line. For this job we may need a real source parser to handle all the cases correctly. |
This would also be useful in general for determining package dependencies. There are a few ways to achieve this, preferred options earlier in the list:
The main issue with (1) is that HIE would need to be made to work reliably for all stack projects, and I have had some difficulty with that in the past. This is something I want to have happen for many other reasons, though. This would also mean that if HIE isn't built yet, it'll need to be built before autodetecting dependencies. The main issue with (2) is that error message format changes between GHC versions. Also, I'm not sure it will give the errors for all imports transitively in the project. It would suck to have to do iterations with the compiler. (3) has a number of issues. We'd need to run CPP somehow or try to munge it out (potentially including imports that aren't actually used). HSE is a large dependency which gives people trouble compiling it (OOM issues). It wouldn't handle. For this single file script usecase, it'd be the easiest way to get this implemented. But if we want to go any further with this, it'll start getting hacky and complicated.
Yep, this would be good. It's tracked by #1208 |
Does the new |
I think |
The |
Oh, great! I just tested a little and have some quick notes:
- with one or more --package options, stack script does restrict imports to the declared packages.
- with no --package options, it detected and installed the right packages just based on the import statements. The PackageImports-style package declarations weren't needed either.
- it showed the "No packages provided, using experimental import parser" message at default verbosity. Maybe we'd rather see this only at --verbosity=info and up.
|
I can't reproduce your first point, e.g.: #!/usr/bin/env stack
-- stack script --resolver lts-8.12 --package base
import Control.Monad.Reader
main = return () results in:
Your third point is already implemented on master. |
That's what I'm saying, it works. All looks good!
Off topic, but while I'm testing, I still find myself wishing
- that I didn't have to specify a resolver sometimes. Eg at times when it seems any newer snapshot would work equally well, and by specifying one I feel I'm forcing users to do a lot of unnecessary downloading and recompiling.
- that I could specify specific versions of packages with the script command (as I can with runghc), to force stack to use just-released versions that aren't in stackage lts (or nightly) yet.
These seem to come up most when I'm writing install instructions for newly released software.
|
PS and maybe these are just incompatible with repeatability.. I'm thinking aloud in case there might still be a few more conveniences it could allow without sacrificing that. |
Ah, I misread your comment. The two other comments you gave are, in fact, specifically avoided due to concerns around reproducibility. With the upcoming extensible snapshots feature, however, it should be possible to define a custom snapshot with the just-released (or even not-yet-released) versions of your packages and include those in a script. |
Cool beans. This new auto-detection of required packages seems a pretty great feature, by the way. One less thing a haskell coder has to think about. Thanks! |
Cool, thanks for the feedback. You OK with closing out this issue then? |
Absolutely. |
In the stack script excerpt below, I started with just a few --package options. I'm using it in a non-cabal project, so there's no local .stack-work directory and it's using my global stack package db, which often has the required packages already. So imports tend to work even if I don't specify all the required packages, which is not good as it means the script may not run on another machine. (Possibly worth a separate feature request: allow imports only from explicitly specified packages, as cabal does).
So I started using PackageImports and writing the package explicitly in the imports, to find which additional --package declarations were needed (many). This also seems useful simply as documentation, it's often unclear which package a module comes from. Now there is obvious redundancy, and I'm wondering if we could omit the --package declarations and have stack infer them from the packages mentioned in imports (or even just from the imported module names with no package names needed).
The text was updated successfully, but these errors were encountered: