Skip to content

Commit ddf8ba6

Browse files
authored
Merge pull request #6659 from kit-ty-kate/fix-var-opamfile
Make the computation of pkg:opamfile match its specification
2 parents a9ad523 + 9a19c0c commit ddf8ba6

File tree

8 files changed

+1855
-16
lines changed

8 files changed

+1855
-16
lines changed

doc/pages/Manual.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,10 @@ the package being defined.
561561
a hash identifying the precise package version and metadata, and that of all
562562
its dependencies
563563
- <a id="pkgvar-opamfile">`opamfile`</a>:
564-
if the package is installed, path of its opam file, from opam internals,
565-
otherwise not defined
564+
if the package is installed or pinned it is the path to the internally
565+
stored opam file, otherwise, if the package exists, it is the path to
566+
a newly created opam file in a directory under the system temporary
567+
directory (see `Stdlib.Filename.get_temp_dir_name`)
566568

567569
Extra variables can be defined by any package at installation time, using a
568570
[`<pkgname>.config`](#lt-pkgname-gt-config) file with a

master_changes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ users)
4848
## Show
4949

5050
## Var/Option
51+
* Make the computation of `pkg:opamfile` match its specification [#6659 @kit-ty-kate - fix #5346]
5152

5253
## Update / Upgrade
5354

@@ -131,6 +132,8 @@ users)
131132
* Add a test showing that `opam upgrade` allows downgrades when necessary [#6690 @kit-ty-kate]
132133
* Add a test showing the behaviour of `opam tree` on local packages that happen to be already pinned [#6688 @kit-ty-kate]
133134
* Add a test showing the behaviour of version pins when one of the dependencies isn't up-to-date [#6691 @kit-ty-kate]
135+
* Add a test showing the behaviour of pre-defined variables in command hooks [#6659 @rjbou]
136+
* Add a test showing the behaviour of `opam var <pkg>:opamfile` [#6659 @kit-ty-kate]
134137

135138
### Engine
136139
* Fix gcc < 14.3 bug on mingw i686 [#6624 @kit-ty-kate]

src/state/opamPackageVar.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -309,20 +309,20 @@ let resolve st ?opam:opam_arg ?(local=OpamVariable.Map.empty) v =
309309
| "dev", Some opam -> Some (bool (is_dev_package st opam))
310310
| "build-id", Some opam -> Option.map string (build_id st opam)
311311
| "opamfile", Some opam ->
312-
(* Opamfile path is retrieved from overlay directory for pinned packages,
313-
or from temporary repository in /tmp *)
314-
let repos_roots reponame =
315-
match Hashtbl.find st.switch_repos.repos_tmp reponame with
316-
| lazy repo_root -> repo_root
317-
| exception Not_found ->
318-
OpamRepositoryPath.root st.switch_global.root reponame
319-
in
320-
OpamFile.OPAM.get_metadata_dir ~repos_roots opam
321-
|> Option.map (fun d ->
322-
OpamFilename.Op.(d//"opam")
323-
|> OpamFilename.to_string
324-
|> string
325-
)
312+
let nv = get_nv opam in
313+
let ret file = Some (string (OpamFile.to_string file)) in
314+
if OpamPackage.Set.mem nv st.installed then
315+
ret (OpamPath.Switch.installed_opam st.switch_global.root st.switch nv)
316+
else if OpamPackage.Set.mem nv st.pinned then
317+
ret (OpamPath.Switch.Overlay.opam st.switch_global.root st.switch (OpamPackage.name nv))
318+
else if OpamPackage.Set.mem nv st.packages then
319+
let opam = OpamPackage.Map.find nv st.opams in
320+
let temp_dir = OpamFilename.mk_tmp_dir () in
321+
let dest = OpamFile.make OpamFilename.Op.(temp_dir // "opam") in
322+
OpamFile.OPAM.write dest opam;
323+
ret dest
324+
else
325+
None
326326
| _, _ -> None
327327
in
328328
let make_package_local v =

tests/reftests/dune.inc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,69 @@
902902
%{targets}
903903
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:git.test} %{read-lines:testing-env}))))
904904

905+
(rule
906+
(alias reftest-hooks-variables)
907+
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
908+
(action
909+
(diff hooks-variables.test hooks-variables.out)))
910+
911+
(alias
912+
(name reftest)
913+
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
914+
(deps (alias reftest-hooks-variables)))
915+
916+
(rule
917+
(targets hooks-variables.out)
918+
(deps root-N0REP0)
919+
(enabled_if (and (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
920+
(package opam)
921+
(action
922+
(with-stdout-to
923+
%{targets}
924+
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:hooks-variables.test} %{read-lines:testing-env}))))
925+
926+
(rule
927+
(alias reftest-hooks-variables.unix)
928+
(enabled_if (and (= %{os_type} "Unix") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
929+
(action
930+
(diff hooks-variables.unix.test hooks-variables.unix.out)))
931+
932+
(alias
933+
(name reftest)
934+
(enabled_if (and (= %{os_type} "Unix") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
935+
(deps (alias reftest-hooks-variables.unix)))
936+
937+
(rule
938+
(targets hooks-variables.unix.out)
939+
(deps root-N0REP0)
940+
(enabled_if (and (= %{os_type} "Unix") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
941+
(package opam)
942+
(action
943+
(with-stdout-to
944+
%{targets}
945+
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:hooks-variables.unix.test} %{read-lines:testing-env}))))
946+
947+
(rule
948+
(alias reftest-hooks-variables.win32)
949+
(enabled_if (and (= %{os_type} "Win32") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
950+
(action
951+
(diff hooks-variables.win32.test hooks-variables.win32.out)))
952+
953+
(alias
954+
(name reftest)
955+
(enabled_if (and (= %{os_type} "Win32") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
956+
(deps (alias reftest-hooks-variables.win32)))
957+
958+
(rule
959+
(targets hooks-variables.win32.out)
960+
(deps root-N0REP0)
961+
(enabled_if (and (= %{os_type} "Win32") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))
962+
(package opam)
963+
(action
964+
(with-stdout-to
965+
%{targets}
966+
(run ./run.exe %{exe:../../src/client/opamMain.exe.exe} %{dep:hooks-variables.win32.test} %{read-lines:testing-env}))))
967+
905968
(rule
906969
(alias reftest-init-ocaml-eval-variables.unix)
907970
(enabled_if (and (= %{os_type} "Unix") (or (<> %{env:TESTALL=1} 0) (= %{env:TESTN0REP0=0} 1))))

0 commit comments

Comments
 (0)