18.2 Libraries and Collections (original) (raw)

18.2 Libraries and Collections🔗

A library is module declaration for use by multiple programs. Racket further groups libraries into collections. Typically, collections are added via packages (seePackage Management in Racket); the package manager works outside of the Racket core, but it configures the core run-time system through collection links files.

Libraries in collections are referenced through lib paths (see require) or symbolic shorthands. For example, the following module uses the "getinfo.rkt" library module from the "setup" collection, and the "cards.rkt" library module from the "games" collection’s "cards"subcollection:

This example is more compactly and more commonly written using symbolic shorthands:

When an identifier id is used in a require form, it is converted to (lib rel-string) where rel-stringis the string form of id.

A rel-string in (lib rel-string) consists of one or more path elements that name collections, and then a final path element that names a library file; the path elements are separated by/. If rel-string contains no /s, then/main.rkt is implicitly appended to the path. Ifrel-string contains / but does not end with a file suffix, then .rkt is implicitly appended to the path.

Libraries also can be distributed via PLaneT packages. Such libraries are referenced through a planet module path (seerequire) and are downloaded by Racket on demand, instead of referenced through collections.

The translation of a planet or lib path to amodule declaration is determined by the module name resolver, as specified by the current-module-name-resolverparameter.

18.2.1 Collection Search Configuration🔗

For the default module name resolver, the search path for collections is determined by thecurrent-library-collection-links parameter and thecurrent-library-collection-paths parameter:

To resolve a module reference rel-string, the defaultmodule name resolver searches collection links incurrent-library-collection-links from first to last to locate the first directory that contains rel-string, splicing a search through in current-library-collection-paths where incurrent-library-collection-links contains #f. The filesystem tree for each element in the link table and search path is effectively spliced together with the filesystem trees of other path elements that correspond to the same collection. Some Racket tools rely on unique resolution of module path names, so an installation and configuration should not allow multiple files to match the same collection and file combination.

The value of the current-library-collection-links parameter is initialized by the racket executable to the result of(find-library-collection-links), and the value of thecurrent-library-collection-paths parameter is initialized to the result of (find-library-collection-paths).

18.2.2 Collection Links🔗

Collection links files are used bycollection-file-path, collection-path, and the default module name resolver to locate collections before trying the (current-library-collection-paths) search path. The collection links files to use are determined by thecurrent-library-collection-links parameter, which is initialized to the result of find-library-collection-links.

A collection links file is read with default reader parameter settings to obtain a list. Every element of the list must be a link specification with one of the forms (list string encoded-path), (list string encoded-path regexp), (list 'root encoded-path), (list 'root encoded-path regexp), (list 'static-root encoded-path), (list 'static-root encoded-path regexp). A string names a top-level collection, in which case encoded-path describes a path that can be used as the collection’s path (directly, as opposed to a subdirectory of encoded-path named by string). A'root entry, in contrast, acts like an path in(current-library-collection-paths). A'static-root entry is like a 'root entry, but where the immediate content of the directory is assumed not to change unless thecollection links file changes. Each encoded-path is either a string, a byte string that is converted to a path with bytes->path, or a list of relative path-element byte strings, 'up, and 'sameindicators that are combined with build-path with the byte strings converted with bytes->path-element. If encoded-path describes a relative path, it is relative to the directory containing thecollection links file. If regexp is specified in a link, then the link is used only if (regexp-match? regexp (version)) produces a true result.

A single top-level collection can have multiple links in acollection links file, and any number of 'root entries can appear. The corresponding paths are effectively spliced together, since the paths are tried in order to locate a file or sub-collection.

The raco link command-link tool can display, install, and remove links in a collection links file. See raco link: Library Collection Links in raco: Racket Command-Line Tools for more information.

Changed in version 8.1.0.6 of package base: Changed encoded-path to allow bytes strings and lists.

18.2.3 Collection Paths and Parameters🔗

Produces a list of paths, which is normally used to initializecurrent-library-collection-paths, as follows:

Changed in version 8.4.0.3 of package base: Added the config andname arguments.

Produces a list of paths and #f, which is normally used to initialize current-library-collection-links, as follows:

Changed in version 8.4.0.3 of package base: Added the config andname arguments.

Returns the path to the file indicated by file in the collection specified by the collections, where the secondcollection (if any) names a sub-collection, and so on. The search uses the values of current-library-collection-linksand current-library-collection-paths.

See also collection-search insetup/collection-search.

If file is not found, but file ends in".rkt" and a file with the suffix ".ss" exists, then the directory of the ".ss" file is used. If file is not found and the ".rkt"/".ss" conversion does not apply, but a directory corresponding to the collections is found, then a path using the first such directory is returned.

If check-compiled? is true, then the search also depends onuse-compiled-file-paths andcurrent-compiled-file-roots; if file is not found, then a compiled form of file with the suffix ".zo"is checked in the same way as the default compiled-load handler. If a compiled file is found, the result fromcollection-file-path reports the location that fileitself would occupy (if it existed) for the found compiled file.

Finally, if the collection is not found, and if fail-proc is provided, then fail-proc is applied to an error message (that does not start "collection-file-path:" or otherwise claim a source), and its result is the result ofcollection-file-path. If fail-proc is not provided and the collection is not found, then theexn:fail:filesystem exception is raised.

Examples:

> (collection-file-path "main.rkt" "racket" "base")
#path:path/to/collects/racket/base/main.rkt
> (collection-file-path "sandwich.rkt" "bologna")
collection-file-path: collection not found
collection: "bologna"
in collection directories:
/home/scheme/pltbuild/racket/racket/collects/
... [246 additional linked and package directories]

Changed in version 6.0.1.12 of package base: Added the check-compiled? argument.

NOTE: This function is deprecated; use collection-file-path, instead. Collection splicing implies that a given collection can have multiple paths, such as when multiple packages provide modules for a collection.

Like collection-file-path, but without a specified file name, so that a directory indicated by collections is returned.

When multiple directories correspond to the collection, the first one found in the search sequence (see Collection Search Configuration) is returned.

Parameter that determines a list of complete directory paths for finding libraries (as referenced in require, for example) through the default module name resolver and for finding paths through collection-path andcollection-file-path. See Collection Search Configuration for more information.

Parameter that determines collection links files, additional paths, and the relative search order ofcurrent-library-collection-paths for finding libraries (as referenced in require, for example) through the defaultmodule name resolver and for finding paths throughcollection-path and collection-file-path. SeeCollection Search Configuration for more information.

Parameter that determines whether user-specific paths, which are in the directory produced by (find-system-path 'addon-dir), are included in search paths for collections and other files. For example, the initial value of find-library-collection-paths omits the user-specific collection directory when this parameter’s value is#f.

If -U or --no-user-path argument to racket, thenuse-user-specific-search-paths is initialized to#f.

Parameter that determines whether collection links files are included in the result of find-library-collection-links.

If this parameter’s value is #f on start-up, thencollection links files are effectively disabled permanently for the Racket process. In particular, if an empty string is provided as the -X or --collects argument to racket, then not only is current-library-collection-paths initialized to the empty list, but use-collection-link-paths is initialized to#f.

Added in version 8.4.0.3 of package base.