Os.cwd
func Os.cwd(): Result[string, Error]Os.cwd returns the working directory as an absolute path: the directory the ant command was started in, not the script's. Nothing in Ant changes it. It is Err when the host cannot name it, as when it has been removed.
Details
- The working directory is the one
antwas started in, not the directory of the script. EveryFilefunction, and Path.abs, takes a relative path against it. - The result is absolute. It is
Errwhen the host cannot name the directory, as when it has been removed. - No function changes the working directory. Join paths to the directory you need, or pass
dir:to Cmd.new for a child process.
Basic examples
Print the working directory:
println(try Os.cwd())Started from /home/ann/site, it prints /home/ann/site.
Os.cwd returns a Result, so a program can report a failure:
let cwd = Os.cwd()
if cwd is Err {
eprintln("no working directory: ${cwd.error.message}")
Os.exit(1)
}
println(cwd.value == (try Path.abs(".")))trueApplications
Name a file by its absolute path, so a log or message that mentions it still finds it from another directory:
let cwd = try Os.cwd()
try File.write("report.txt", "all green")
let saved = Path.join([cwd, "report.txt"])
println(Path.isAbs(saved))
println(try File.read(saved))true
all greenShow a path relative to the working directory, as a tool shows the files it touched. Path.rel turns an absolute path back into the short one:
let cwd = try Os.cwd()
let found = [Path.join([cwd, "src", "main.ant"]), Path.join([cwd, "README.md"])]
for path in found {
println(try Path.rel(cwd, path))
}src/main.ant
README.mdFind the nearest directory that holds a marker such as .git, from the working directory up to the root. Path.dir of the root is the root, which ends the search:
func findUp(start: string, marker: string): string | null {
var dir = start
while !File.exists(Path.join([dir, marker])) {
let parent = Path.dir(dir)
if parent == dir { return null }
dir = parent
}
dir
}
let cwd = try Os.cwd()
try File.mkdir("repo/.git")
try File.mkdir("repo/src/app")
let root = findUp(Path.join([cwd, "repo/src/app"]), ".git")
if root != null {
println(try Path.rel(cwd, root))
}
println(findUp(cwd, "no-such-marker"))repo
nullProperties and relations
Path.abs resolves a relative path against the working directory:
let cwd = try Os.cwd()
assert(Path.abs(".") == Os.cwd())
assert((try Path.abs("notes.txt")) == Path.join([cwd, "notes.txt"]))A relative path and the same path joined to Os.cwd() name the same file:
let cwd = try Os.cwd()
try File.write("notes.txt", "hello")
assert(File.exists(Path.join([cwd, "notes.txt"])))
assert((try File.read(Path.join([cwd, "notes.txt"]))) == (try File.read("notes.txt")))A child process starts in the same working directory unless dir: gives another. Here pwd prints it, and File.realPath settles links such as /tmp on macOS:
let cwd = try Os.cwd()
try File.mkdir("sub")
let here = try Cmd.read(Cmd.new("pwd"))
let there = try Cmd.read(Cmd.new("pwd", dir: "sub"))
assert((try File.realPath(here.trim())) == (try File.realPath(cwd)))
assert((try File.realPath(there.trim())) == (try File.realPath(Path.join([cwd, "sub"]))))Possible issues
Os.cwd returns a Result, not a string. Unwrap it with try:
let cwd: string = Os.cwd()
// error: cannot use Result[string, Error] where string is expectedIn a string, the Result shows as it is:
println("working in ${Os.cwd()}")
println("working in ${try Os.cwd()}")The first line prints working in Ok{ value: "/home/ann/site" }, the second working in /home/ann/site.
The working directory is not the script's directory. Run from its parent, tools/where.ant does not find itself by its bare name; Os.program leads to it:
println(Os.program())
println(File.exists("where.ant"))
println(File.exists(Path.join([Path.dir(Os.program()), "where.ant"])))tools/where.ant
false
trueClimbing a relative path with Path.dir stops at ".", the working directory, and never looks above it. Start from Os.cwd() to reach the parents:
println(Path.dir("repo/src"))
println(Path.dir("repo"))
println(Path.dir("."))
let cwd = try Os.cwd()
println(Path.dir(cwd) == cwd)repo
.
.
falseSee also
Path.abs · Path.rel · Path.join · Os.program · Os.home · File.realPath · Cmd.new
