Derived modules
A derived module is a module of a project that has no file under src/. An
Acton function returns its source, and acton build calls the function
when the module is out of date. Other modules import a derived module like
any other module.
Use a derived module for source that follows from other files: classes that are generated from a schema, tables that are computed from data files, or files that are embedded in the program.
The table in Build.act
The table derived_modules in Build.act lists the derived modules of a
project. The table below has one entry, for the module schema:
derived_modules = {
schema: (
function=gen.schema,
inputs=["data/schema.txt"],
args=(prefix="S")
)
}
- The name to the left of the colon is the name of the module, written as
an
importstatement writes it.schemais the module thatsrc/schema.actwould be, andmodel.schemathe module thatsrc/model/schema.actwould be. A name can also end with.*and stand for every module below it: see Every module below a name. functionis a module name followed by the name of a function in that module. The module name is written as animportstatement in the project writes it.gen.schemais the functionschemain the modulegenof the project.tools.gen.schemais the functionschemain the modulegenof the dependencytools.std.build.derive.embed_filesis the functionembed_filesin the modulestd.build.deriveof the standard library. The function of an entry cannot be in a derived module of the same project.inputslists the input files of the function, as paths relative to the project root. A path names a file or a directory inside the project, and a directory stands for the files below it:inputs=["data/schema.txt", "data/types/"]names the filedata/schema.txtand the files belowdata/types/. The section Input paths has the rules.inputscan be left out.argsholds arguments for the function, written as the keyword arguments of a call, with commas between them. A name is the name of a parameter of the function. A value is a literal: a string, a number,True,False,None, a list, or a dictionary with strings as keys. The function gets the keys of a dictionary in sorted order, not in the order in which they are written. A number whose fraction is zero is passed as an integer:2.0is passed as2. A parameter of typefloattakes an integer too.argscan be left out.
The module name and the value of function are written as names, without
quotes. Each path in inputs is a string.
A module cannot be both a derived module and a file under src/, and a
name that ends with .* has no .act file under src/ below it. The
table cannot name a module twice. With a dependency named tools, the
table cannot name the module tools or a module such as tools.schema:
an import statement in the project takes these names for modules of
the dependency. A build stops in each of these cases.
Input paths
A path in inputs is written with / between its components. A build
stops with a message for a path that breaks one of these rules:
- The path is relative and has no
.or..component, so it stays inside the project. - Its first component is not
out. A build writes the directoryout/, so a file there is no input of a build. - It names a file or a directory.
- Each component is spelled as its directory lists it. With a directory
datain the project,inputs=["Data/"]is an error, also on a file system that findsdataunder the nameData. The sameBuild.actthen builds on every file system.
A path that names a directory stands for every file below it, in the directories below it too, with three exceptions:
- a file whose name starts with a dot, such as
.DS_Store, - a file below a directory whose name starts with a dot, such as
.git/, - a
--tydbcopy. A build with--tydbwrites a copy of the interface of a module beside its source file, as a directory with the extension.tydb. A file in a directory with that extension, and a file with that extension, is no input file.
A file of these three kinds is no input file: the function does not get
it, and a change to it runs no function. Every other file is an input
file, also a backup file such as app.js~. A path that is written in
inputs is taken as it is written: inputs=["web/.well-known/"] names
the files below web/.well-known/.
A build stops with a message when an input file cannot be read, when it is not a regular file, or when its name has bytes that are not text in the encoding of the locale. Below a directory, a symbolic link that names no file is an input file that cannot be read.
The function
The function of the entry above is an ordinary function in src/gen.act:
import std.build.derive as derive
def schema(inputs: dict[str, derive.InputFile], prefix: str) -> str:
lines = []
for name in inputs["data/schema.txt"].read().decode().splitlines():
lines.append("class " + prefix + name + "(object):")
lines.append(" def __init__(self):")
lines.append(' self.kind = "' + name + '"')
return "\n".join(lines) + "\n"
With the lines Device and Link in data/schema.txt, the module schema
has the classes SDevice and SLink. A module of the project imports
schema and uses the class SDevice:
import schema
actor main(env):
proc def __startup__():
print(schema.SDevice().kind)
env.exit(0)
The first parameter is a dictionary from the path of each input file to a
description of the file, an InputFile of the module std.build.derive.
A path is relative to the project root, with / between the components,
and the dictionary holds the paths in sorted order. The result is the
source of the module.
Write the type of the first parameter, dict[str, derive.InputFile].
Without it the compiler infers a type from the body of the function. For
a function that calls read() the inferred type does not fit, and the
build stops at the call.
A description has three attributes and one method:
pathis the path of the file, as in the dictionary.sizeis the size of the content in bytes.sha256is the SHA-256 of the content, as 64 lowercase hexadecimal digits.read()returns the content asbytes. The text of a file isread().decode().
A description holds no content. The function reads a file when it calls
read(), and a function that works from the paths, the sizes and the
SHA-256 values alone reads no file. read() raises ValueError when the
content of the file is not the content that the description is of, which
is so when the file changed while the function ran.
The other parameters take the arguments of the entry, by name: prefix
gets "S". A parameter with a default value needs no argument. The build
calls the function as
gen.schema(inputs, prefix="S")
and the compiler checks that call. A build stops when an argument has another type than its parameter, when an argument has no parameter, or when a parameter without a default value has no argument.
The function gets no Env, so it cannot connect to a host or start a
process, and the input files are the only files that it can read. It can
create actors and call them, and they get no Env either. Everything the
function reads comes from its arguments and from the modules it imports.
That is how the build knows when the function has to run again. The
section In a build lists the cases.
The module of the function can import modules of the standard library,
modules of a dependency, and modules of the project that have a file under
src/. A module of a dependency can be a derived module of that
dependency. A build stops when the module of the function imports a
derived module of the project, directly or through other modules.
A library that libraries in Build.act declares is compiled as a whole:
when it holds the module of the function, or a module that this module
imports, a build compiles every module of the library before the function
runs. Keep the derived modules of the project, and the modules that import
them, out of such a library. Otherwise a derived module is compiled as the
last build left it, before the function runs, and the build stops when its
source is missing or has an error.
A function reports an error by raising an exception. The build then stops
and shows the exception. With raise ValueError("data/schema.txt is empty")
in the function gen.schema, the message is:
The function gen.schema of the entry schema of derived_modules in /home/user/app/Build.act failed:
ValueError: data/schema.txt is empty
What the function printed before the exception is shown after the first line of this message. When the function returns without an exception, what it printed is not shown.
A test of the function
A test calls the function with descriptions of content that the test makes
up. derive.input_of takes a path and the content, and returns the
description of a file with that path and that content. No file is read.
The test module src/test_gen.act tests the function gen.schema:
import std.build.derive as derive
import testing
import gen
def _test_schema():
inputs = {"data/schema.txt": derive.input_of("data/schema.txt", b"Device\nLink\n")}
source = gen.schema(inputs, prefix="S")
testing.assertIn("class SLink(object):", source)
Several modules from one function
A function can return several modules. It then returns a dictionary from
module name to source. Its entry in Build.act names every one of these
modules: to the left of the colon it has their names in braces, with
commas between them. The names can be written on one line or on several.
A command that writes Build.act, such as acton pkg add, writes them
as below: one name per line, in sorted order.
In Build.act:
derived_modules = {
{
layers.base,
layers.top,
tables
}: (
function=gen.modules,
inputs=["data/layers/"]
)
}
The function, in src/gen.act:
import std.build.derive as derive
def modules(inputs: dict[str, derive.InputFile]) -> dict[str, str]:
res = {"tables": "names = []\n"}
for path, f in inputs.items():
name = path.split("/")[-1].split(".")[0]
res["layers." + name] = "size = " + str(f.size) + "\n"
return res
With base.txt and top.txt as the only files below data/layers/, the
function returns the modules tables, layers.base and layers.top. It
takes the size of each file from the description and reads no file.
A module name stands for one module, and not for the modules below it. A
build stops when the function returns a module that its entry does not
name, or does not return a module that its entry names. The message lists
those modules. With a file data/layers/extra.txt in place of
data/layers/top.txt, the message is:
The function gen.modules of the entry layers.base of derived_modules in /home/user/app/Build.act returned other modules than the entry names.
Returned and not named: layers.extra
Named and not returned: layers.top
The function returns every module that its entry names with a module name, and no module that its entry does not name. Change the names of the entry or the function.
The message calls the entry the entry layers.base of derived_modules.
Where a message of the build says the entry, one name and
of derived_modules, the name is the first of the names of the entry in
sorted order.
The order of the names has no meaning. A name that is written twice is an error.
A function whose result type is str has an entry with one name, a module
name. A function for any other entry has the result type dict[str, str].
Two names are an error when they differ only in the case of letters, or
start with names that do: Foo and foo, or Layers.base and
layers.top, which start with Layers and layers. This holds in the
table and among the modules that a function returns: a file system that
ignores the case of letters has one file or directory for two such names.
Every module below a name
The modules that a function returns can follow from its input files: one
module for each file of a directory, or three for each layer of a model.
Build.act cannot list such modules by name. For them, a name of an entry
can end with .*.
layers.* stands for every module below layers. A module is below
layers when its name starts with layers.: layers.base is below
layers, and so is layers.core.types. These are the modules whose files
would be in src/layers/ or in a directory below it. layers.* does not
stand for the module layers itself, the module that src/layers.act
would be.
In Build.act:
derived_modules = {
{
layers.*,
tables
}: (
function=gen.modules,
inputs=["data/"]
)
}
The function is gen.modules of the section above. With the files
data/base.txt and data/top.txt it returns the modules tables,
layers.base and layers.top. When the file data/edge.txt is added, it
also returns layers.edge, and Build.act stays as it is. When
data/top.txt is removed, the module layers.top is removed.
An entry with one such name is written without braces. In Build.act:
derived_modules = {
layers.*: (
function=gen.layers,
inputs=["data/"]
)
}
The function, in src/gen.act:
import std.build.derive as derive
def layers(inputs: dict[str, derive.InputFile]) -> dict[str, str]:
res = {}
for path, f in inputs.items():
name = path.split("/")[-1].split(".")[0]
res["layers." + name] = "size = " + str(f.size) + "\n"
return res
For a name that ends with .*:
- The function returns any number of modules below
layers, also none. A module that it returned in the last build and does not return in this build is removed. - The function still returns every module that its entry names with a
module name,
tablesabove, and no module that its entry does not name. Otherwise the build stops with the message of the section above. Write a module name for a module that the function always returns: the build then checks that it is returned. - The result type of the function is
dict[str, str]. - Every module below
layersis a derived module. The project has no.actfile undersrc/layers/, and a build that finds one stops before it runs the function of any entry of the project. src/layers.actcan be a file that is written by hand, sincelayers.*does not stand for the modulelayers. For a function that returns the modulelayerstoo, write both names,layersandlayers.*.- No module is named twice in the table, in one entry or in two.
layers.*andlayers.basename the modulelayers.basetwice.layers.*andlayers.core.*name the modules belowlayers.coretwice. .*is written once, at the end of a name and without quotes.layers.*.typesandlayers.b*are not names..*is read only in a name to the left of the colon. A path ininputsis taken as it is written, and a*in it is not a pattern.
The modules that the function returned in the last build are the .act
files under out/derived/src/layers/. A build removes a .act file there
that the function did not return.
The files of a directory
The standard library has a function for an entry that embeds the files of a
directory: embed_files of the module std.build.derive. The module that
it returns has one name, files, a dict[str, bytes] with the content of
each input file by the path of the file. This entry makes the module
assets from the files below web/:
derived_modules = {
assets: (function=std.build.derive.embed_files, inputs=["web/"],
args=(strip="web"))
}
A module of the project imports assets, and assets.files["/index.html"]
is the content of the file web/index.html. strip is a prefix that is
left out of each key of files. Without strip the key is
web/index.html. The result type of embed_files is str, so the name
of its entry is a module name, and not a name that ends with .*.
The source that embed_files returns has one embed
expression for each input file and no content, so the function reads no
file. What a build does depends on what changed below web/:
- A file has new content: the build calls the function, since an input file
changed. The function returns the same source, and the build writes no
module. It compiles
assetsagain because a file that the module embeds has new content. - A file is added or removed: the function returns other source. The build writes the module and compiles it.
The section
The files of a directory of the
page on embed shows the source of the module and says for which files a
build stops. It also shows a function of a project that returns another
module for the same files, with embed_call and str_literal of
std.build.derive.
In a build
acton build, acton test and a build of a single file bring the derived
modules that they need up to date before they compile them. A build needs
an entry when a module that it compiles imports a module that the entry
names, in the project or in a dependency. acton build and acton test
compile every module of the project, so they need every entry of the
project, also one whose modules no module imports. A build of a single
file needs only the entries that the file and the modules it imports reach.
An entry that a build does not need is left alone: its function does not run and its input files are not looked at, so an input path that names nothing is no error in such a build. The modules that a function returns can import other derived modules, and the module of a function can import the derived modules of a dependency. A build brings these up to date too.
While another build holds the compiler lock of a dependency, the build
prints Waiting for compiler lock in and the directory of the dependency,
and waits.
The source of a derived module is written to out/derived/src/, at the
path that the module would have under src/: out/derived/src/schema.act
for the module schema. Do not commit this file, and do not edit it: a
build that finds the file edited runs the function and writes the file
again. An error in a derived module points to this file, and a note after
the error names the function that returned the source. A build removes
every file under out/derived/src/ that no entry names, except a --tydb
copy, and the source file of a module below a name that ends with .*
that the function did not return. With the source file of a module, it
removes what was compiled from the module and the --tydb copy of the
module.
The derived modules of an entry are out of date when the function has not run for the entry yet, or when one of these changed since the function last ran:
- an input file: its content changed, or a file was added or removed below an input path that names a directory. This holds for every input file, also for one that the function does not read: its description holds the SHA-256 of its content,
- the entry in
Build.act: its names, its function, its input paths or its arguments, - the function, or anything that the function uses, in any module,
- the compiler version,
- the file of one of these modules under
out/derived/src/: it was edited or removed, or a file was added for a module below a name that ends with.*.
A change to other code leaves the modules up to date, even code in the
function's own module: another function, a constant that the function does
not read, or a comment. With --verbose, a build says for each entry
whether it is up to date, and the reason when it is not.
A build keeps a record of each entry, with the SHA-256 of the content of
each input file, and metadata of the file such as its size and its
modification time. To tell whether an input file changed, a later build
first compares the metadata. When the metadata is the same and the file
was last modified before the record was written, the build takes the file
as unchanged and does not read it, whatever its size. Otherwise the build
reads the file and compares the SHA-256, so a file that was written again
with the same content does not run the function. The build then records
the new metadata, so that a later build does not read the file again. The
description of an input file holds this size and this SHA-256, and the
function reads the file only when it calls read().
A build stops when an input file changes while the build runs:
data/schema.txt changed while the build ran. Run the build again
The build looks at each input file before it runs the function and again
when the function has returned. A file that is not the same file both
times stops the build before a module of the entry is written. So a
module is never made from one version of a file and recorded with
another. A function that reads the changed file stops earlier, with the
ValueError of read().
A line in the build log shows each function that ran:
schema Derived by gen.schema 0.412 s
The line of an entry starts with the first of its names in sorted order. When the entry is not one module name, the line gives the number of modules that the function returned:
layers.* Derived 3 modules by gen.modules 0.398 s
To call the function of an entry, a build writes a program under
out/derived/runner/ and builds it. A later build runs the program
without building it again when only an input file changed. It builds the
program again when the function, anything that the function uses, an
argument of the entry or the compiler version changed. The time on the
line of an entry is the time to run the program. In a build that built
the program, it is the time to build the program and to run it.
acton build --watch and acton test --watch also build when an input
file is saved, added or removed: a file that inputs names, or a file
below a directory in inputs that the directory stands for. They watch
the input paths of the project and of its local dependencies, and read
them from the Build.act files again when a Build.act changes. A file
that no input path stands for, such as web/.DS_Store, starts no build.
A change of an input file starts a build of the whole project, and
acton test --watch then runs every selected test whose cached result no
longer applies. After a build that a saved source file started,
acton test --watch runs the tests of the modules that changed and of the
modules that import them, directly or through other modules. A derived
module is among the modules that changed when its source is not the
source that the tests last ran with.
acton build --skip-build and acton test list run no function: they use
the derived modules when they are up to date and fail otherwise.
acton sig runs no function and does not look at the input files. It
shows a derived module as the last build wrote it, and it does not know a
derived module that no build has written.
The language server runs no function either. It reads a derived module as the last build wrote it.
acton test looks for tests in the modules that have a file under src/.
It does not list or run a test that is in a derived module.