.\" Made by website/plain/build.mjs from website/words/sg-add.md; change that file. .TH SG-ADD 1 2026-11-28 "SG" "SG commands" .SH NAME sg add \- adds a package to sg.project and fetches it; with sg update, sg remove and sg lock, which keep sg.lock .SH SYNOPSIS .TP \fBsg add example.com/ada/xml\fR the newest version of the package, on a require line of its own, fetched .TP \fBsg add example.com/ada/xml@1, @1.4, @1.4.2\fR the newest version whose numbers start so .TP \fBsg update\fR each package the program names, to the newest version of the same major version .TP \fBsg update xml\fR the one package with that name .TP \fBsg remove xml\fR takes its require line and its local line away .TP \fBsg lock\fR writes sg.lock for what sg.project says, raising no version .TP \fBrequire xml example.com/ada/xml 1.4.2\fR a line of sg.project: the name use writes, the address, the version .TP \fBrequire ... unsafe\fR the same, allowing the package unsafe and extern .TP \fBlocal xml ../xml\fR the package of that require line taken from a folder .TP \fBpackage example.com/ada/shop\fR the project's own address, for a project that is a package .TP \fBSG_CACHE=folder\fR where the packages fetched are kept .TP \fBSG_MIRROR=folder\fR packages taken from a folder of bare repositories in place of the network .TP \fBsg add \-\-help, sg update \-\-help, sg remove \-\-help, sg lock \-\-help\fR prints this page .SH DESCRIPTION A package is a library kept in its own repository. Its address, a host and a path, names the repository at \fBhttps://\fR and that address, and a version is a tag of it, \fBv1.4.2\fR; there is no registry. A program names the packages it uses in \fBsg.project\fR, read beside the program (the folder given, or the file's folder), a line each. \fBsg.lock\fR, beside it, holds the SHA-256 of each package's files, so the program builds from the same files on every machine. \fB//\fR starts a comment in both. .PP The four commands work on the \fBsg.project\fR and \fBsg.lock\fR of the current folder. \fBsg add\fR makes \fBsg.project\fR when there is none and appends a line, \fBrequire name address version\fR, the name being the last part of the address, made one that can be written (\fButf_8\fR for \fBexample.com/bob/utf\-8\fR). It looks for the newest tag that fits what is written after \fB@\fR, and with nothing written, the newest of all. \fBsg update\fR raises each package the program names, or the one named, to the newest version of its major version: a new major version is another package, added with a name of its own. \fBsg remove\fR takes a package's lines away. \fBsg lock\fR makes the lock fit \fBsg.project\fR without raising a version, after \fBsg.project\fR was changed by hand. Each writes the lock again, fetching what it needs, and writes a line for what changed, \fBadded\fR, the version before, \fBother files than before\fR or \fBtaken away\fR, and says when a package uses \fBunsafe\fR or links libraries of C. A lock left with nothing is taken away. The files are written whole into a new file put in the old one's place, never through a link, and nothing is written when a command fails. .PP A package used by a package is fetched too, and the program need not name it; it cannot \fBuse\fR it either. The versions are chosen as the least each \fBrequire\fR line asks for, so a new release changes no build until \fBsg update\fR\&. Running or building a program never changes the lock: one that does not fit \fBsg.project\fR stops the program, with exit code 2 and the line, and says to run \fBsg lock\fR\&. .PP A package with \fBunsafe\fR or \fBextern\fR in it is built only when its own \fBrequire\fR line ends in \fBunsafe\fR, also for a package used through another. \fBsg add\fR says so when it adds one; \fBsg update\fR to a version that starts to use \fBunsafe\fR stops and writes nothing until the line allows it. .PP A package is fetched by the system's \fBgit\fR alone, over \fBhttps://\fR or what the user's git setting puts in its place, and no code of it runs while it is fetched or built. git runs in an empty folder of \fBsg\fR\&'s own, with no \fBGIT_\fR variable of the user's. A package may have at most 64 MB, 16 MB in a file and 10,000 files; a link, a submodule, a name that not every system can hold and two names that differ only in case are refused. The files are kept once for the user, read only, under the address and the version: in \fBSG_CACHE\fR, else in \fB~/Library/Caches/sg\fR on macOS, \fB$XDG_CACHE_HOME/sg\fR or \fB~/.cache/sg\fR on Linux, and \fB%LOCALAPPDATA%\esg\fR on Windows. To remove the cache, it is made writable first (\fBchmod \-R u+w\fR). A program whose lock names a package the cache has not fetches it when it runs; a tag moved to other files is refused, since its SHA-256 is not the lock's. \fBSG_MIRROR\fR names a folder of bare repositories, one at the path of each address, from which the packages are taken in place of the network. .PP The exit code is 0 when the files were written, 1 for an error, and 2 for a command line that is not a usage. .SH EXAMPLES .SS "A mirror, in place of the network" The file publish.sh: .PP .RS 4 .nf # publish.sh folder address version: the files of the folder as that version of the package, # a commit tagged v in the bare repository mirror/
. set -e export GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_NOSYSTEM=1 repository=mirror/$2 [ -d "$repository" ] || git -c init.defaultBranch=main init -q --bare "$repository" git="git --git-dir=$repository --work-tree=$1 -c user.name=Ada -c user.email=ada@example.com" $git add -A $git commit -q --allow-empty -m "$3" $git tag "v$3" .fi .RE .PP The file letters/sg.project: .PP .RS 4 .nf package example.com/bob/letters .fi .RE .PP The file letters/letters.sg: .PP .RS 4 .nf public string shout(read string text) { return text.toUpper() } .fi .RE .PP The file xml/sg.project: .PP .RS 4 .nf package example.com/ada/xml require letters example.com/bob/letters 1.0.0 .fi .RE .PP The file xml/xml.sg: .PP .RS 4 .nf use letters public string tag(read string name) { return $"<{letters.shout(name)}/>".toString() } .fi .RE .PP .RS 4 .nf $ sh publish.sh letters example.com/bob/letters 1.0.0 $ sh publish.sh letters example.com/bob/letters 1.1.0 $ sh publish.sh xml example.com/ada/xml 1.4.0 $ sh publish.sh xml example.com/ada/xml 1.4.2 $ sh publish.sh xml example.com/ada/xml 1.5.0 $ sh publish.sh xml example.com/ada/xml 2.0.0 .fi .RE .PP The examples on this page need no network. \fBpublish.sh\fR makes the repositories of two packages, as their authors would push them: \fBexample.com/bob/letters\fR at the versions 1.0.0 and 1.1.0, and \fBexample.com/ada/xml\fR, which uses \fBletters\fR, at four. Each package has an \fBsg.project\fR whose \fBpackage\fR line is its address. The commands below are given \fBSG_MIRROR=../mirror\fR, so they read these repositories in place of \fBhttps://\fR, and \fBSG_CACHE=../cache\fR, so they keep what they fetch beside them. Without the two, the same commands reach the network. .SS "Adding a package" The file shop/main.sg: .PP .RS 4 .nf use xml void main() { print(xml.tag("item")) } .fi .RE .PP .RS 4 .nf $ cd shop $ sg main.sg error: there is no module named `xml` a module is a folder with source files, next to the files of the program: `xml/` --> main.sg:1:5 1 | use xml | ^^^ hint: a package is named on a `require` line of `sg.project`, beside the program, which `sg add
` writes $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/xml@1.4 example.com/ada/xml 1.4.2 (added) example.com/bob/letters 1.0.0 (added) $ cat sg.project require xml example.com/ada/xml 1.4.2 $ cat sg.lock // Written by `sg`; `sg lock` writes it again from `sg.project`. example.com/ada/xml 1.4.2 sha256-8adbb6675ac1b1f16b50ef5d1c676d20d34dc0fbd13836e2718eea9872910c25 example.com/bob/letters 1.0.0 sha256-5ab0d7b94c153128e2afdf1725d60855027f097ddb32274fda997e7492fcb300 $ SG_CACHE=../cache sg main.sg $ SG_CACHE=../cache sg build main.sg -o shop $ ./shop .fi .RE .PP \fB@1.4\fR chose 1.4.2, the newest 1.4 version, and \fBletters\fR came with it at the version \fBxml\fR asks for. The line's name, \fBxml\fR, is what \fBmain.sg\fR writes after \fBuse\fR\&. The program then runs and builds from the cache with no fetch; compiled, the package's modules are modules, and nothing of it is looked for when the program runs. .SS "Updating" .RS 4 .nf $ cd shop $ SG_MIRROR=../mirror SG_CACHE=../cache sg update example.com/ada/xml 1.5.0 (1.4.2 before) $ SG_MIRROR=../mirror SG_CACHE=../cache sg update every package is at its newest version $ cat sg.project require xml example.com/ada/xml 1.5.0 $ grep letters sg.lock example.com/bob/letters 1.0.0 sha256-5ab0d7b94c153128e2afdf1725d60855027f097ddb32274fda997e7492fcb300 $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/bob/letters example.com/bob/letters 1.1.0 (1.0.0 before) $ cat sg.project require xml example.com/ada/xml 1.5.0 require letters example.com/bob/letters 1.1.0 .fi .RE .PP \fBsg update\fR raised \fBxml\fR to 1.5.0 and not to 2.0.0, another major version. It left \fBletters\fR at 1.0.0, though 1.1.0 is out: the program does not name it, and \fBxml\fR asks for 1.0.0. Named on a line of its own, it is 1.1.0, which \fBxml\fR takes too, since a version of the same major version keeps what the older one had. .SS "Removing" .RS 4 .nf $ cd shop $ SG_CACHE=../cache sg remove xml example.com/ada/xml 1.5.0 (taken away) $ SG_CACHE=../cache sg remove letters example.com/bob/letters 1.1.0 (taken away) $ test -e sg.lock || echo "no sg.lock" no sg.lock .fi .RE .PP The lock is taken away with its last package; \fBsg.project\fR stays, empty. A package no line names any more is left in the cache, where another program may use it. .SS "A line written by hand, and a local folder" The file notes/main.sg: .PP .RS 4 .nf use letters void main() { print(letters.shout("quiet please")) } .fi .RE .PP .RS 4 .nf $ cd notes $ echo 'require letters example.com/bob/letters 1.0.0' > sg.project $ SG_MIRROR=../mirror SG_CACHE=../cache sg main.sg error: ./sg.project:1: ./sg.lock has no example.com/bob/letters 1.0.0: `sg lock` writes it again from `sg.project` $ SG_MIRROR=../mirror SG_CACHE=../cache sg lock example.com/bob/letters 1.0.0 (added) $ SG_CACHE=../cache sg main.sg QUIET PLEASE $ echo 'local letters ../letters' >> sg.project $ SG_CACHE=../cache sg lock example.com/bob/letters 1.0.0 (taken away) $ sg main.sg QUIET PLEASE $ SG_MIRROR=../mirror SG_CACHE=../cache sg update letters error: `letters` is taken from a folder by its `local` line, and has no version to update .fi .RE .PP A program never writes the lock itself: the line added by hand stops it, and \fBsg lock\fR fetches what the line asks for. A \fBlocal\fR line takes the package of a \fBrequire\fR line from a folder, written from the folder of \fBsg.project\fR, while one works on both; the lock has nothing of it then, and its files are read as they are on the disk. .SS "A package with unsafe" The file digits/sg.project: .PP .RS 4 .nf package example.com/ada/digits .fi .RE .PP The file digits/digits.sg: .PP .RS 4 .nf extern { int abs(int value) } public int distance(int a, int b) { return unsafe abs(a - b) } .fi .RE .PP The file trip/main.sg: .PP .RS 4 .nf use digits void main() { print(digits.distance(3, 10)) } .fi .RE .PP .RS 4 .nf $ sh publish.sh digits example.com/ada/digits 1.0.0 $ cd trip $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/digits example.com/ada/digits 1.0.0 (added, uses `unsafe`) the program builds with it once a `require` line of it ends in `unsafe`: `require digits example.com/ada/digits 1.0.0 unsafe` $ SG_CACHE=../cache sg main.sg 2>&1 | head -n 1 error: `extern` is in the package example.com/ada/digits, and the program's `sg.project` does not allow it $ echo 'require digits example.com/ada/digits 1.0.0 unsafe' > sg.project $ SG_CACHE=../cache sg main.sg 7 $ grep digits sg.lock | cut -d ' ' -f 1,2,4 example.com/ada/digits 1.0.0 unsafe .fi .RE .PP \fBunsafe\fR and \fBextern\fR are found in the source of every package read, and allowed only by the word at the end of the package's line, so a program sees which of its packages leave the checks of the language. The lock keeps \fBunsafe\fR beside the SHA-256 too, and \fBc=\fR and the libraries of C when the package links some. .SH "MISTAKES IT TELLS" .SS "No sg.project" .RS 4 .nf $ mkdir -p empty $ cd empty $ sg update; echo $? error: there is no `sg.project` in this folder 1 $ sg remove xml error: there is no `sg.project` in this folder $ sg lock error: there is no `sg.project` in this folder .fi .RE .PP The commands work on the folder they run in: they are run beside the program's \fBsg.project\fR\&. Only \fBsg add\fR makes one. .SS "An address that is not one" .RS 4 .nf $ mkdir -p empty $ cd empty $ sg add https://example.com/ada/xml error: `https://example.com/ada/xml` is not the address of a package: the address is written without a scheme or a user; it is written as `github.com/ada/xml` $ sg add xml error: `xml` is not the address of a package: it starts with the name of a host, then a path; it is written as `github.com/ada/xml` $ sg add example.com/ada/xml@v1.4 error: `v1.4` is not a version to look for: it is `1`, `1.4` or `1.4.2` $ test -e sg.project || echo "no sg.project" no sg.project .fi .RE .PP An address has a host with a dot in it and a path of names, with no scheme; a version after \fB@\fR is one to three numbers, without the \fBv\fR of the tag. .SS "No version that fits" .RS 4 .nf $ cd shop $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/xml@1.9 error: example.com/ada/xml has no version that fits `example.com/ada/xml@1.9`: its versions are 1.4.0, 1.4.2, 1.5.0, 2.0.0 .fi .RE .SS "A name taken" .RS 4 .nf $ cd shop $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/xml@1.4 example.com/ada/xml 1.4.2 (added) example.com/bob/letters 1.0.0 (added) $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/xml@1.5 error: sg.project:1: example.com/ada/xml 1 is required there already; `sg update xml` takes a newer version $ SG_MIRROR=../mirror SG_CACHE=../cache sg add example.com/ada/xml@2 error: sg.project:1: `xml` is the name of another package; write the line with a name of your own, `require xml2 example.com/ada/xml 2.0.0`, and run `sg lock` $ SG_CACHE=../cache sg remove letters error: there is no package `letters` in sg.project .fi .RE .PP A major version is added once; a newer one of it comes by \fBsg update\fR\&. Another major version is another package, which needs a name of its own, written by hand. \fBsg remove\fR takes a name, the one \fBuse\fR writes, and \fBletters\fR here is only used through \fBxml\fR\&. .SS "Not a usage" .RS 4 .nf $ sg add > /dev/null 2>&1; echo $? 2 $ sg add example.com/ada/xml example.com/bob/letters > /dev/null 2>&1; echo $? 2 $ sg lock now > /dev/null 2>&1; echo $? 2 .fi .RE .PP \fBsg add\fR takes one address, \fBsg remove\fR one name, \fBsg update\fR one name or none, and \fBsg lock\fR nothing. Anything else prints the usage of every command, and changes nothing. .SH "SEE ALSO" .BR sg-use (7), .BR sg-public (7), .BR sg-unsafe (7), .BR sg-extern (7), .BR sg (1), .BR sg-build (1) .PP https://sg\-lang.org/reference/sg-add.html