summaryrefslogtreecommitdiff
path: root/lib/spack/spack/environment/__init__.py
blob: 2f293d9eb8f81b1a644c1a69e52e6b2ad3957a7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
# Copyright 2013-2023 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
"""This package implements Spack environments.

.. _lockfile-format:

`spack.lock` format
===================

Spack environments have existed since Spack ``v0.12.0``, and there have been 4 different
``spack.lock`` formats since then. The formats are documented here.

The high-level format of a Spack lockfile hasn't changed much between versions, but the
contents have.  Lockfiles are JSON-formatted and their top-level sections are:

  1. ``_meta`` (object): this contains details about the file format, including:
      * ``file-type``: always ``"spack-lockfile"``
      * ``lockfile-version``: an integer representing the lockfile format version
      * ``specfile-version``: an integer representing the spec format version (since
        ``v0.17``)

  2. ``spack`` (object): optional, this identifies information about Spack
      used to concretize the environment:
      * ``type``: required, identifies form Spack version took (e.g., ``git``, ``release``)
      * ``commit``: the commit if the version is from git
      * ``version``: the Spack version

  3. ``roots`` (list): an ordered list of records representing the roots of the Spack
      environment. Each has two fields:
      * ``hash``: a Spack spec hash uniquely identifying the concrete root spec
      * ``spec``: a string representation of the abstract spec that was concretized

  4. ``concrete_specs``: a dictionary containing the specs in the environment.

Compatibility
-------------

New versions of Spack can (so far) read all old lockfile formats -- they are
backward-compatible. Old versions cannot read new lockfile formats, and you'll need to
upgrade Spack to use them.

.. list-table:: Lockfile version compatibility across Spack versions
   :header-rows: 1

   * - Spack version
     - ``v1``
     - ``v2``
     - ``v3``
     - ``v4``
   * - ``v0.12:0.14``
     - ✅
     -
     -
     -
   * - ``v0.15:0.16``
     - ✅
     - ✅
     -
     -
   * - ``v0.17``
     - ✅
     - ✅
     - ✅
     -
   * - ``v0.18:``
     - ✅
     - ✅
     - ✅
     - ✅

Version 1
---------

When lockfiles were first created, there was only one hash in Spack: the DAG hash. This
DAG hash (we'll call it the old DAG hash) did *not* include build dependencies -- it
only included transitive link and run dependencies.

The spec format at this time was keyed by name. Each spec started with a key for its
name, whose value was a dictionary of other spec attributes. The lockfile put these
name-keyed specs into dictionaries keyed by their DAG hash, and the spec records did not
actually have a "hash" field in the lockfile -- you have to associate the hash from the
key with the spec record after the fact.

Dependencies in original lockfiles were keyed by ``"hash"``, i.e. the old DAG hash.

.. code-block:: json

    {
        "_meta": {
            "file-type": "spack-lockfile",
            "lockfile-version": 1
        },
        "roots": [
            {
                "hash": "<old_dag_hash 1>",
                "spec": "<abstract spec 1>"
            },
            {
                "hash": "<old_dag_hash 2>",
                "spec": "<abstract spec 2>"
            }
        ],
        "concrete_specs": {
            "<old_dag_hash 1>": {
                "... <spec dict attributes> ...": { },
                "dependencies": {
                    "depname_1": {
                        "hash": "<old_dag_hash for depname_1>",
                        "type": ["build", "link"]
                    },
                    "depname_2": {
                        "hash": "<old_dag_hash for depname_3>",
                        "type": ["build", "link"]
                    }
                },
                "hash": "<old_dag_hash 1>"
            },
            "<old_dag_hash 2>": {
                "... <spec dict attributes> ...": { },
                "dependencies": {
                    "depname_3": {
                        "hash": "<old_dag_hash for depname_3>",
                        "type": ["build", "link"]
                    },
                    "depname_4": {
                        "hash": "<old_dag_hash for depname_4>",
                        "type": ["build", "link"]
                    },
                },
                "hash": "<old_dag_hash 2>"
            },
        }
    }


Version 2
---------

Version 2 changes one thing: specs in the lockfile are now keyed by ``build_hash``
instead of the old ``dag_hash``. Specs have a ``hash`` attribute with their real DAG
hash, so you can't go by the dictionary key anymore to identify a spec -- you have to
read it in and look at ``"hash"``. Dependencies are still keyed by old DAG hash.

Even though we key lockfiles by ``build_hash``, specs in Spack were still deployed with
the old, coarser DAG hash. This means that in v2 and v3 lockfiles (which are keyed by
build hash), there may be multiple versions of the same spec with different build
dependencies, which means they will have different build hashes but the same DAG hash.
Spack would only have been able to actually install one of these.

.. code-block:: json

    {
        "_meta": {
            "file-type": "spack-lockfile",
            "lockfile-version": 2
        },
        "roots": [
            {
                "hash": "<build_hash 1>",
                "spec": "<abstract spec 1>"
            },
            {
                "hash": "<build_hash 2>",
                "spec": "<abstract spec 2>"
            }
        ],
        "concrete_specs": {
            "<build_hash 1>": {
                "... <spec dict attributes> ...": { },
                "dependencies": {
                    "depname_1": {
                        "hash": "<old_dag_hash for depname_1>",
                        "type": ["build", "link"]
                    },
                    "depname_2": {
                        "hash": "<old_dag_hash for depname_3>",
                        "type": ["build", "link"]
                    }
                },
                "hash": "<old_dag_hash 1>",
            },
            "<build_hash 2>": {
                "... <spec dict attributes> ...": { },
                "dependencies": {
                    "depname_3": {
                        "hash": "<old_dag_hash for depname_3>",
                        "type": ["build", "link"]
                    },
                    "depname_4": {
                        "hash": "<old_dag_hash for depname_4>",
                        "type": ["build", "link"]
                    }
                },
                "hash": "<old_dag_hash 2>"
            }
        }
    }


Version 3
---------

Version 3 doesn't change the top-level lockfile format, but this was when we changed the
specfile format. Specs in ``concrete_specs`` are now keyed by the build hash, with no
inner dictionary keyed by their package name. The package name is in a ``name`` field
inside each spec dictionary. The ``dependencies`` field in the specs is a list instead
of a dictionary, and each element of the list is a record with the name, dependency
types, and hash of the dependency. Instead of a key called ``hash``, dependencies are
keyed by ``build_hash``. Each spec still has a ``hash`` attribute.

Version 3 adds the ``specfile_version`` field to ``_meta`` and uses the new JSON spec
format.

.. code-block:: json

    {
        "_meta": {
            "file-type": "spack-lockfile",
            "lockfile-version": 3,
            "specfile-version": 2
        },
        "roots": [
            {
                "hash": "<build_hash 1>",
                "spec": "<abstract spec 1>"
            },
            {
                "hash": "<build_hash 2>",
                "spec": "<abstract spec 2>"
            },
        ],
        "concrete_specs": {
            "<build_hash 1>": {
                "... <spec dict attributes> ...": { },
                "dependencies": [
                    {
                        "name": "depname_1",
                        "build_hash": "<build_hash for depname_1>",
                        "type": ["build", "link"]
                    },
                    {
                        "name": "depname_2",
                        "build_hash": "<build_hash for depname_2>",
                        "type": ["build", "link"]
                    },
                ],
                "hash": "<old_dag_hash 1>",
            },
            "<build_hash 2>": {
                "... <spec dict attributes> ...": { },
                "dependencies": [
                    {
                        "name": "depname_3",
                        "build_hash": "<build_hash for depname_3>",
                        "type": ["build", "link"]
                    },
                    {
                        "name": "depname_4",
                        "build_hash": "<build_hash for depname_4>",
                        "type": ["build", "link"]
                    },
                ],
                "hash": "<old_dag_hash 2>"
            }
        }
    }


Version 4
---------

Version 4 removes build hashes and is keyed by the new DAG hash (``hash``). The ``hash``
now includes build dependencies and a canonical hash of the ``package.py`` file.
Dependencies are keyed by ``hash`` (DAG hash) as well. There are no more ``build_hash``
fields in the specs, and there are no more issues with lockfiles being able to store
multiple specs with the same DAG hash (because the DAG hash is now finer-grained).
An optional ``spack`` property may be included to track version information, such as
the commit or version.


.. code-block:: json

    {
        "_meta": {
            "file-type": "spack-lockfile",
            "lockfile-version": 4,
            "specfile-version": 3
        },
        "roots": [
            {
                "hash": "<dag_hash 1>",
                "spec": "<abstract spec 1>"
            },
            {
                "hash": "<dag_hash 2>",
                "spec": "<abstract spec 2>"
            }
        ],
        "concrete_specs": {
            "<dag_hash 1>": {
                "... <spec dict attributes> ...": { },
                "dependencies": [
                    {
                        "name": "depname_1",
                        "hash": "<dag_hash for depname_1>",
                        "type": ["build", "link"]
                    },
                    {
                        "name": "depname_2",
                        "hash": "<dag_hash for depname_2>",
                        "type": ["build", "link"]
                    }
                ],
                "hash": "<dag_hash 1>",
            },
            "<daghash 2>": {
                "... <spec dict attributes> ...": { },
                "dependencies": [
                    {
                        "name": "depname_3",
                        "hash": "<dag_hash for depname_3>",
                        "type": ["build", "link"]
                    },
                    {
                        "name": "depname_4",
                        "hash": "<dag_hash for depname_4>",
                        "type": ["build", "link"]
                    }
                ],
                "hash": "<dag_hash 2>"
            }
        }
    }
"""

from .environment import (
    TOP_LEVEL_KEY,
    Environment,
    SpackEnvironmentConfigError,
    SpackEnvironmentError,
    SpackEnvironmentViewError,
    activate,
    active,
    active_environment,
    all_environment_names,
    all_environments,
    create,
    create_in_dir,
    deactivate,
    default_manifest_yaml,
    default_view_name,
    display_specs,
    environment_dir_from_name,
    exists,
    initialize_environment_dir,
    installed_specs,
    is_env_dir,
    is_latest_format,
    lockfile_name,
    manifest_file,
    manifest_name,
    no_active_environment,
    read,
    root,
    spack_env_var,
    spack_env_view_var,
    update_yaml,
)

__all__ = [
    "TOP_LEVEL_KEY",
    "Environment",
    "SpackEnvironmentConfigError",
    "SpackEnvironmentError",
    "SpackEnvironmentViewError",
    "activate",
    "active",
    "active_environment",
    "all_environment_names",
    "all_environments",
    "create",
    "create_in_dir",
    "deactivate",
    "default_manifest_yaml",
    "default_view_name",
    "display_specs",
    "environment_dir_from_name",
    "exists",
    "initialize_environment_dir",
    "installed_specs",
    "is_env_dir",
    "is_latest_format",
    "lockfile_name",
    "manifest_file",
    "manifest_name",
    "no_active_environment",
    "read",
    "root",
    "spack_env_var",
    "spack_env_view_var",
    "update_yaml",
]