Skip to main content
Version: Next

Working Directory

Control where your commands execute with the workdir setting. This is especially useful for monorepos and projects with complex directory structures.

Default Behavior​

By default, commands from the root invowkfile run in the directory where the invowkfile is located. For modules, commands run in the module's root directory. Relative workdir paths are resolved from these locations.

Setting Working Directory​

Command Level​

{
name: "build frontend"
workdir: "./frontend" // Run in frontend subdirectory
implementations: [{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

Implementation Level​

{
name: "build"
implementations: [
{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
workdir: "./web" // This implementation runs in ./web
},
{
script: {content: "go build ./..."}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
workdir: "./api" // This implementation runs in ./api
}
]
}

Root Level​

workdir: "./src" // All commands default to ./src

cmds: [
{
name: "build"
// Inherits workdir: ./src
implementations: [...]
},
{
name: "test"
workdir: "./tests" // Override to ./tests
implementations: [...]
}
]

CLI Override​

Override the working directory at runtime:

invowk cmd build --ivk-workdir ./frontend

Path Types​

Relative Paths​

Relative to the invowkfile location:

workdir: "./frontend"
workdir: "../shared"
workdir: "src/app"

Absolute Paths​

Full system paths:

workdir: "/opt/myapp"
workdir: "/home/user/projects/myapp"
note

workdir paths are resolved literally and do not expand environment variables such as ${HOME} or ${PROJECT_ROOT}. Use relative paths, absolute literal paths, or pass --ivk-workdir on the command line.

Precedence​

CLI override (--ivk-workdir) overrides implementation, which overrides command, which overrides root:

// --ivk-workdir (CLI) > implementation > command > root
workdir: "./root" // Lowest: ./root

cmds: [
{
name: "build"
workdir: "./command" // Override: ./command
implementations: [
{
script: {content: "make"}
workdir: "./implementation" // Override: ./implementation
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}
]
}
]

Monorepo Pattern​

Perfect for monorepos with multiple packages:

cmds: [
{
name: "web build"
workdir: "./packages/web"
implementations: [{
script: {content: "npm run build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
},
{
name: "api build"
workdir: "./packages/api"
implementations: [{
script: {content: "go build ./..."}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
},
{
name: "mobile build"
workdir: "./packages/mobile"
implementations: [{
script: {content: "flutter build"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
]

Container Working Directory​

For containers, the current directory is mounted to /workspace:

{
name: "build"
implementations: [{
script: {content: """
pwd # /workspace
ls # Shows your project files
"""}
runtimes: [{name: "container", image: "debian:stable-slim"}]
platforms: [{name: "linux"}]
}]
}

With workdir, that subdirectory becomes the container's working directory:

{
name: "build package"
workdir: "./frontend"
implementations: [{
script: {content: """
pwd # /workspace/frontend
./build.sh
"""}
runtimes: [{name: "container", image: "debian:stable-slim"}]
platforms: [{name: "linux"}]
}]
}

Virtual Runtimes​

Under the default restricted filesystem access of the virtual-sh and virtual-lua runtimes, the workdir is also an allowed root for the script's file access, with one exception: a workdir that a module's own invowkfile declares (at the root, command or implementation level) and that resolves outside the module directory. Links and junctions are followed, and a workdir that cannot be resolved, or that lies on another drive, counts as outside. Here a module's invowkfile is one that Invowk loads as part of a module.

Such a workdir is still the script's working directory, and @work, INVOWK_ANCHOR_WORK and invowk.path("@work") still name it, but reads and writes there are refused with virtual path denied unless another allowed root covers them. The same check covers cd and the shell's file tests: cd into a directory there fails, and a test such as [ -f settings.json ] is false even when the file exists. Host binaries listed in allowed_binaries are not checked: they run as native host processes with that directory as their working directory and keep their host access, because the virtual runtimes do not isolate host binaries (use the container runtime for isolation). To keep the directory readable and writable for the script itself:

  • declare it in the selected platform's virtual.filesystem.paths, for example WORK: "@work";
  • or pass --ivk-workdir with that directory: a workdir chosen on the command line is always an allowed root.

These workdirs stay allowed roots as before: the --ivk-workdir override, any workdir of an invowkfile that Invowk does not load as part of a module (such as ../packages/x in a monorepo), a module's default workdir, and a module workdir inside the module. virtual.filesystem.access: "full", the native runtime and the container runtime are not affected. See File Access for the other roots.

Cross-Platform Paths​

Use forward slashes for cross-platform compatibility:

// Good - works everywhere
workdir: "./src/app"

// Avoid - Windows-specific
workdir: ".\\src\\app"

Invowk™ automatically converts to native path separators at runtime.

Real-World Examples​

Frontend/Backend Split​

cmds: [
{
name: "start frontend"
workdir: "./frontend"
implementations: [{
script: {content: "npm run dev"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
},
{
name: "start backend"
workdir: "./backend"
implementations: [{
script: {content: "go run ./cmd/server"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
]

Test Organization​

cmds: [
{
name: "test unit"
workdir: "./tests/unit"
implementations: [{
script: {content: "pytest"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
},
{
name: "test integration"
workdir: "./tests/integration"
implementations: [{
script: {content: "pytest"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}]
}]
},
{
name: "test e2e"
workdir: "./tests/e2e"
implementations: [{
script: {content: "cypress run"}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}
]

Build in Subdirectory​

{
name: "build"
workdir: "./src"
implementations: [{
script: {content: """
# Now in ./src
go build -o ../bin/app ./...
"""}
runtimes: [{name: "native"}]
platforms: [{name: "linux"}, {name: "macos"}, {name: "windows"}]
}]
}

Best Practices​

  1. Use relative paths: More portable across machines
  2. Forward slashes: Cross-platform compatible
  3. Root level for defaults: Override where needed
  4. Keep paths short: Easier to understand

Next Steps​