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

dune-project: Add the (subst disabled/enabled) stanza #4864

Merged
merged 4 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Unreleased
- Always use 7 char hash prefix in build info version (#4857, @jberdine, fixes
#4855)

- Allow to explicitly disable/enable the use of `dune subst` by adding a
new `(subst <disable|enable>)` stanza to the `dune-project` file.
(#4864, @kit-ty-kate)

2.9.1 (unreleased)
------------------

Expand Down
20 changes: 20 additions & 0 deletions doc/dune-files.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,26 @@ where ``<setting>`` is one of:
- ``(enabled_for <languages>)`` can be used to restrict the languages that are
considered for formatting.

.. _subst:

subst
----------

Starting in dune 3.0, :ref:`dune-subst` can be explicitly disabled or enabled.
By default it is enabled.
controlled by using

.. code:: scheme

(subst <setting>)

where ``<setting>`` is one of:

- ``disabled``, meaning that any call of `dune subst` in this project is
forbidden and will result in an error.

- ``enabled``, allowing substitutions explicitly. This is the default.

.. _generate_opam_files:

generate_opam_files
Expand Down
1 change: 1 addition & 0 deletions src/dune_engine/dune_engine.ml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Source_tree = Source_tree
module Opam_file = Opam_file
module Action_dune_lang = Action_dune_lang
module Format_config = Format_config
module Subst_config = Subst_config
module Dune_lexer = Dune_lexer
module Predicate_lang = Predicate_lang
module Section = Section
Expand Down
8 changes: 8 additions & 0 deletions src/dune_engine/dune_project.ml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ type t =
; dialects : Dialect.DB.t
; explicit_js_mode : bool
; format_config : Format_config.t option
; subst_config : Subst_config.t option
; strict_package_deps : bool
; cram : bool
}
Expand Down Expand Up @@ -207,6 +208,7 @@ let to_dyn
; dialects
; explicit_js_mode
; format_config
; subst_config
; strict_package_deps
; cram
} =
Expand All @@ -232,6 +234,7 @@ let to_dyn
; ("dialects", Dialect.DB.to_dyn dialects)
; ("explicit_js_mode", bool explicit_js_mode)
; ("format_config", option Format_config.to_dyn format_config)
; ("subst_config", option Subst_config.to_dyn subst_config)
; ("strict_package_deps", bool strict_package_deps)
; ("cram", bool cram)
]
Expand Down Expand Up @@ -460,6 +463,8 @@ let format_config t =
let version = dune_version t in
Format_config.of_config ~ext ~dune_lang ~version

let subst_config t = Subst_config.of_config t.subst_config

let default_name ~dir ~(packages : Package.t Package.Name.Map.t) =
match Package.Name.Map.min_binding packages with
| None -> Name.anonymous dir
Expand Down Expand Up @@ -515,6 +520,7 @@ let infer ~dir packages =
; dialects = Dialect.DB.builtin
; explicit_js_mode
; format_config = None
; subst_config = None
; strict_package_deps
; cram
}
Expand Down Expand Up @@ -614,6 +620,7 @@ let parse ~dir ~lang ~opam_packages ~file ~dir_status =
field_o_b "explicit_js_mode"
~check:(Dune_lang.Syntax.since Stanza.syntax (1, 11))
and+ format_config = Format_config.field ~since:(2, 0)
and+ subst_config = Subst_config.field ~since:(3, 0)
and+ strict_package_deps =
field_o_b "strict_package_deps"
~check:(Dune_lang.Syntax.since Stanza.syntax (2, 3))
Expand Down Expand Up @@ -796,6 +803,7 @@ let parse ~dir ~lang ~opam_packages ~file ~dir_status =
; dialects
; explicit_js_mode
; format_config
; subst_config
; strict_package_deps
; cram
}))
Expand Down
2 changes: 2 additions & 0 deletions src/dune_engine/dune_project.mli
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ val explicit_js_mode : t -> bool

val format_config : t -> Format_config.t

val subst_config : t -> Subst_config.t

val equal : t -> t -> bool

val hash : t -> int
Expand Down
23 changes: 23 additions & 0 deletions src/dune_engine/subst_config.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
open! Stdune
open Import
open Dune_lang.Decoder

(* Can be extended later if needed *)
type t =
| Disabled
| Enabled

let to_dyn conf =
let open Dyn.Encoder in
match conf with
| Disabled -> string "disabled"
| Enabled -> string "enabled"

let decoder = enum [ ("disabled", Disabled); ("enabled", Enabled) ]

let field ~since =
field_o "subst" (Dune_lang.Syntax.since Stanza.syntax since >>> decoder)

let of_config = function
| None -> Enabled
| Some conf -> conf
13 changes: 13 additions & 0 deletions src/dune_engine/subst_config.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
open! Stdune
open Import

type t =
| Disabled
| Enabled

val to_dyn : t -> Dyn.t

val field :
since:Dune_lang.Syntax.Version.t -> t option Dune_lang.Decoder.fields_parser

val of_config : t option -> t
20 changes: 19 additions & 1 deletion src/dune_rules/opam_create.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ let default_build_command =
]
[ "dune" "install" "-p" name "--create-install-files" name ]
]
|}))
and from_3_0_without_subst =
lazy
(Opam_file.parse_value
(Lexbuf.from_string ~fname:"<internal>"
{|
[
[ "dune" "build" "-p" name "-j" jobs "--promote-install-files" "false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
[ "dune" "install" "-p" name "--create-install-files" name ]
]
|}))
in
fun project ->
Expand All @@ -66,8 +80,12 @@ let default_build_command =
from_1_11_before_2_7
else if Dune_project.dune_version project < (2, 9) then
from_2_7
else if Dune_project.dune_version project < (3, 0) then
from_2_9
else
from_2_9)
match Dune_project.subst_config project with
| Disabled -> from_3_0_without_subst
| Enabled -> from_2_9)

let package_fields
{ Package.synopsis
Expand Down
13 changes: 13 additions & 0 deletions src/dune_rules/watermarks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,19 @@ let subst vcs =
[ Pp.text "dune subst must be executed from the root of the project."
]
in
(match Dune_project.subst_config dune_project.project with
| Dune_engine.Subst_config.Disabled ->
User_error.raise
[ Pp.text
"dune subst has been disabled in this project. Any use of it is \
forbidden."
]
~hints:
[ Pp.text
"If you wish to re-enable it, change to (subst enabled) in the \
dune-project file."
]
| Dune_engine.Subst_config.Enabled -> ());
let info =
let loc, name =
match dune_project.name with
Expand Down
55 changes: 55 additions & 0 deletions test/blackbox-tests/test-cases/dune-project-meta/main.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,58 @@ the doc dependencies:
"something"
"odoc" {with-doc}
]

$ cat > dune-project <<EOF
> (lang dune 3.0)
> (name foo)
> (generate_opam_files true)
> (subst disabled)
> (package (name foo) (depends (odoc :with-test) something))
> EOF

$ dune build foo.opam
$ grep -A15 ^build: foo.opam
build: [
[
"dune"
"build"
"-p"
name
"-j"
jobs
"--promote-install-files"
"false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]

$ cat > dune-project <<EOF
> (lang dune 3.0)
> (name foo)
> (generate_opam_files true)
> (subst enabled)
> (package (name foo) (depends (odoc :with-test) something))
> EOF

$ dune build foo.opam
$ grep -A16 ^build: foo.opam
build: [
["dune" "subst"] {dev}
[
"dune"
"build"
"-p"
name
"-j"
jobs
"--promote-install-files"
"false"
"@install"
"@runtest" {with-test}
"@doc" {with-doc}
]
["dune" "install" "-p" name "--create-install-files" name]
]
25 changes: 25 additions & 0 deletions test/blackbox-tests/test-cases/subst.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,29 @@ Test subst and files with unicode (#3879)
(version 1.0)
(package (name foo) (authors "John Doe <[email protected]>"))

$ cat > dune-project <<EOF
> (lang dune 3.0)
> (name foo)
> (version 1.0)
> (package (name foo) (authors "John Doe <[email protected]>"))
> (subst disabled)
> EOF

$ dune subst
Error: dune subst has been disabled in this project. Any use of it is
forbidden.
Hint: If you wish to re-enable it, change to (subst enabled) in the
dune-project file.
[1]

$ cat > dune-project <<EOF
> (lang dune 3.0)
> (name foo)
> (version 1.0)
> (package (name foo) (authors "John Doe <[email protected]>"))
> (subst enabled)
> EOF

$ dune subst

$ rm -rf .git