commercialhaskell / stack

The Haskell Tool Stack
http://haskellstack.org
BSD 3-Clause "New" or "Revised" License
3.98k stars 842 forks source link

Specify a default snapshot for `stack init` #6496

Closed chansey97 closed 7 months ago

chansey97 commented 7 months ago

Since I sometimes would create some temporary projects (via stack new myproject) for experiment, I hope Stack can set a default snapshot and template for stack new. I don't like to manually enter --snapshot every time.

Firstly, I tried to set resolver in global project stack.yaml setting, but it doesn't work.

For example, I set my global project stack.yaml as

packages: []
resolver: lts-21.25

but when I call stack new myproject in a clean directory, it still selected lts-22.12.

Then, I thought, maybe Stack select snapshot based on the base version specified in the default template, perhaps I ought to specify base-4.17.2.1 for new-template.hsfiles.

I download new-template.hsfiles from https://github.com/commercialhaskell/stack-templates/tree/master, modify base version, and replace the original new-template.hsfiles in %STACK_ROOT%/templates.

Unfortunately, it still doesn't work.

When I call stack new myproject, it still selected lts-22.12 (and a new new-template.hsfiles generated and replace mine).

So, is it possible to specify a default snapshot and template for stack new?

Thanks.

P.s.

  1. This might be a feature request.

  2. Comparing with setting default template, I prefer to set default snapshot if possible.

Stack version

Version 2.15.1, Git revision 2828cc5326e27f16db480b1a7826a91004dcc2bc x86_64 hpack-0.36.0

mpilgrem commented 7 months ago

Currently, no.

As regards stack new and templates, the default service is GitHub, the default username is commercialhaskell, and the default project template name is new-template: https://docs.haskellstack.org/en/stable/new_command/#project-templates. Those defaults are not currently configurable. I can see that it could be useful to make them configurable. See below about the default-template key.

As regards the initialisation of Stack's project-level configuration file, unless you specify --snapshot (or --resolver), stack init (also used by stack new' by default) looks for the most recent Stackage snapshot that is consistent with the Cabal file, preferring (in order) most recent LTS Haskell, most recent Stackage Nightly, other LTS Haskell (most recent first). (I see that needs to be documented online.) So, specifying a template that placed an upper bound onbase` that 'bites' would be a way, in practice, of sort-of-specifying a snapshot.

mpilgrem commented 7 months ago

The existing templates: key (https://docs.haskellstack.org/en/stable/yaml_configuration/#templates) could be extended, as follows:

templates:
  default:
    service: # one of `github`, `gitlab` and `bitbucket` (currently) Default: `github`
    username: # Default `commercialhaskell`
    template-name: # Default `new-template`
mpilgrem commented 7 months ago

@chansey97, my initial response was not accurate: there is an existing default-template key: https://docs.haskellstack.org/en/stable/yaml_configuration/#default-template.

mpilgrem commented 7 months ago

I have updated the online documentation to cover these points.

chansey97 commented 7 months ago

Hi @mpilgrem,

So, specifying a template that placed an upper bound on base that 'bites' would be a way, in practice, of sort-of-specifying a snapshot.

I thought about it again. Specifying base version may not fully specify snapshot. For example: many Nightly snapshots use the same GHC version (includes base), but they are different snapshots.

I still hope --snapshot can be globally configurable. When creating a new project, it is hard to remember which the snapshot version (lts-xxx) I'd like to use. It would be nice if Stack could select a default (daily use) snapshot.

templates:
  default:
    service: # one of `github`, `gitlab` and `bitbucket` (currently) Default: `github`
    username: # Default `commercialhaskell`
    template-name: # Default `new-template`

Thanks for clarifying that default template is configurable.

From docs:

A project template file can be located in a repository named stack-templates on GitHub, GitLab or Bitbucket; at a URL; or on the local file system.

However,

where is one of github for GitHub, gitlab for GitLab, or bitbucket for Bitbucket.

It would be if service can also be a local file path.

P.s. Personally, I am not very eager for the default template configuration. I would rather have a global configuration for the default snapshot.

mpilgrem commented 7 months ago

@chansey97, subject to the CI clearing, this is implemented in #6500. You can test it by upgrading Stack (if you do not use GHCup to manage versions of Stack) with stack upgrade --source-only --git --git-branch fix6496. EDIT: Branch now merged, so stack upgrade --source-only --git.

chansey97 commented 6 months ago

Hi @mpilgrem, did the default-init-snapshot field be supported in the latest release?

I tried with v2.15.3, but did not work.

## config.yaml
default-init-snapshot: global
$ stack --version
Version 2.15.3, Git revision cffdec6ea6cf4500e08c92fea044c48a6032759d x86_64 hpack-0.36.0

$ stack new myproject
Warning: C:\sr\config.yaml: Unrecognized field in ConfigMonoid: default-init-snapshot

Also, why you renamed the issue from stack new to stack init.

IMHO, there are some subtle difference between these two commands.

mpilgrem commented 5 months ago

@chansey97, sorry for the slow response - I missed your post.

The recent Stack 2.15.x versions have been to fix bugs that urgently needed fixing and were 'off-cycle'. The next 'proper' release of Stack will likely be at the start of June 2024.

I renamed the issue because init deals with stack.yaml and its contents; new deals with other project files (from a template) and then applies init (by default; there is also stack new --no-init) - that is why all of the options for stack init are also options for stack new.