KScript docs
EN

Installing KScript

Introduction

KScript is one program, kscript, that runs scripts, serves sites, checks code and builds apps. It is a native program, so it doesn't need .NET or anything else installed first. An install script picks the right build for your computer and puts it on your PATH.

Installing

On Linux and macOS, run:

Shell
curl -fsSL https://raw.githubusercontent.com/kylepantall/kscript/master/install.sh | sh

On Windows, run this in PowerShell:

PowerShell
irm https://raw.githubusercontent.com/kylepantall/kscript/master/install.ps1 | iex

The scripts pick the build for your operating system and CPU and install it to ~/.kscript/bin (on Windows, %LOCALAPPDATA%\Programs\kscript). They then add that folder to your PATH. On macOS they also clear the quarantine flag, since the binary isn't notarized.

Then check it worked:

CommandWhat it does
kscript --versionThe version you have.

Install options

The install scripts read a few environment variables:

  • KSCRIPT_VERSION=v0.34.0 installs a specific release instead of the latest.
  • KSCRIPT_INSTALL chooses the folder.
  • KSCRIPT_NO_MODIFY_PATH=1 leaves your PATH alone.
Shell
curl -fsSL https://raw.githubusercontent.com/kylepantall/kscript/master/install.sh | KSCRIPT_VERSION=v0.34.0 sh

Updating

kscript update installs the latest release when it is newer than yours:

CommandWhat it does
kscript updateInstall the latest release if it's newer.
kscript update --checkOnly report whether there is one.
kscript update --version v0.34.0Install a specific release.
kscript update --no-ideLeave the editor extension alone.

It replaces the binary in place. On Windows, the running kscript.exe is renamed to kscript.exe.old, and deleted the next time kscript starts. Set GITHUB_TOKEN to avoid GitHub's rate limit, such as on a shared CI runner.

kscript update also brings the editor extension up to the same release in every editor that already has it (VS Code, Insiders, Cursor, VSCodium and Windsurf), and tells you which ones it updated. Editors without the extension are left alone. This happens even when the binary is already current, so an extension that fell behind is caught up. --check reports stale extensions too, and --force reinstalls them. Reload the editor window afterwards.

Checking downloads

Each release has a SHA256SUMS file that lists the SHA-256 of every file in it. The install scripts, kscript update and kscript compile (when it fetches kscript for another platform) check each download against that list before unpacking it, and install nothing when it doesn't match. Releases before 0.26 have no list, so installing one of those says the download wasn't checked.

To check a download you made by hand, put SHA256SUMS beside it and run:

Shell
sha256sum --check --ignore-missing SHA256SUMS            # Linux
shasum -a 256 --check --ignore-missing SHA256SUMS        # macOS

Installing by hand

You may also download a build from the Releases page. There are builds for linux-x64, linux-arm64, osx-arm64 (Apple Silicon), osx-x64 and win-x64.

Each kscript-<platform> archive holds kscript and the two libraries desktop windows draw with (libSkiaSharp and libHarfBuzzSharp). Keep the three files in the same folder. Everything except windows works without the libraries:

Shell
mkdir kscript && tar -xzf kscript-osx-arm64.tar.gz -C kscript   # unpack the archive
xattr -dr com.apple.quarantine kscript                          # macOS only: the files aren't notarized
./kscript/kscript --version                                     # check it runs

The kscript-runtime-<platform> archives are what kscript compile builds programs from. You don't need them to run kscript.

Building from source

To build kscript yourself, you need the .NET 8 SDK and a C++ toolchain for Native AOT: clang and zlib1g-dev on Linux, Xcode on macOS, or Visual Studio's C++ tools on Windows. Then publish for your platform:

Shell
dotnet publish KScriptApp -c Release -r osx-arm64 -p:PublishAot=true -o out

Running scripts

Run a script with kscript and its file name. Anything after the name is in $args, a list:

CommandWhat it does
kscript hello.kscriptRun a script.
kscript greet.kscript Joan$args is ['Joan'].

The exit code is 0, the code given to exit, or 3 when the script stops with an error, printed as file:line:column: Type: message. 2 means the command line itself was wrong (see exit codes). Ctrl+C stops a script, and its finally blocks still run.

Reading the docs

kscript docs opens these docs in your browser. They are built into kscript, so they work offline and match the version you have:

CommandWhat it does
kscript docsOpen the docs.
kscript docs replaceOpen them with a search for "replace".

Docs without kscript running

To read the docs somewhere kscript docs can't run, such as a computer without KScript, a USB stick or your own web server, export them. --export-to writes the whole site into a folder as plain files:

CommandWhat it does
kscript docs --export-to kscript-docsWrite the docs into kscript-docs.
kscript docs --export-to kscript-docs --syntax xmlWith the examples shown as XML first.

Open kscript-docs/index.html in a browser. Every page is its own .html file and every link is relative, so the folder works straight from the disk, with no server and no network, and keeps working when you copy or upload it. Search works too: its index comes in assets/search.js. Exporting into the same folder again replaces the pages and leaves any other files there alone.

To put several versions on one website, export each into a folder named after its version, side by side (docs/0.55.0/, docs/0.54.0/). --versions url turns the version in the header into a picker that reads a list such as {"latest": "0.55.0", "versions": ["0.55.0", "0.54.0"]} and opens the same page in the version picked, and --stylesheet url loads a stylesheet of your own after the docs' own, so they match the rest of the site. The KScript website publishes every release's docs this way at /docs/<version>/.

Docs for your website

A copy you put on a website is read as published documentation, not as a local tool. --published makes it one: the playground is left out, with every link to it, pages end without an "Edit this page on GitHub" link, and the home page doesn't talk about how the copy was made. kscript docs on your computer keeps all of them.

--hide leaves out pages and reference entries, with every link to them and their search entries, as a list separated by commas:

Shell
kscript docs --export-to public/docs --published --hide /guide/roadmap,/reference/methods/*,/reference/ai#cmd-chat
In --hide Leaves out
/guide/roadmap (or guide/roadmap) that page, and its topic pages
/reference/methods/* every page under the path
/reference/ai#cmd-chat one reference entry: the anchor in its address
playground the playground
edit-links the "Edit this page on GitHub" links

To add your own content, such as illustrations or a note about your support, or to replace a page, put the settings in a JSON file and pass it with --config:

JSON
{
  "published": true,
  "hide": ["/guide/roadmap"],
  "pages": { "/guide/install": "install.md" },
  "before": { "/guide/language": "language-intro.md" },
  "after": { "/": "support.md" },
  "files": "docs-files",
  "versions": "/docs/versions.json",
  "stylesheet": "/assets/docs-theme.css",
  "syntax": "kscript"
}
  • pages replaces a page with your Markdown (a guide's topic pages go with it).
  • before adds Markdown under a page's title, and after at its end. Their headings join the page's "On this page".
  • files is a folder copied into the top of the copy, for the pictures and other files your Markdown uses: docs-files/illustrations/flow.svg is ![How a script runs](/illustrations/flow.svg).
  • Paths in the file are found from the file's own folder. The command line adds to the file: --hide adds to hide, and --published, --versions, --stylesheet and --syntax win over the file.

A path that matches no page or entry is an error, and nothing is written, so a typo never leaves a page in by accident.

The examples can still be read, copied and switched between .kscript and XML, but they can't run, because running them needs kscript docs. So the Run boxes and the playground's editor are left out, and each runnable example leaves a hidden placeholder where its box was:

HTML page
<div class="kscript-example" data-example="guide-getting-started-1">
  <!-- the example's code, to read -->
  <template class="kscript-try" data-example="guide-getting-started-1" data-format="kscript"
            data-kscript="echo 'Hello'" data-xml="&lt;echo&gt;Hello&lt;/echo&gt;"></template>
</div>

data-example names the example: the page's file and its number on that page, so it stays the same from one export to the next. data-format is the format the example was written in, data-kscript and data-xml hold its code in each format, and data-input and data-title are there when the example has them. A <template> is never shown, so the placeholders change nothing until a script of your own uses them. To run the examples with your own runner, add a script to the pages that swaps each one for it:

js
document.querySelectorAll('template.kscript-try').forEach(placeholder => {
  const example = placeholder.closest('.kscript-example');
  // The example's code in the reader's format, falling back to the one it was written in.
  const format = document.documentElement.dataset.format;
  const code = placeholder.dataset[format] ?? placeholder.dataset[placeholder.dataset.format];
  example.replaceWith(myRunner(code, format));   // myRunner is yours: an editor and a Run button
});

Editor support

The KScript extension adds errors as you type, completion, hover, go to definition, rename and highlighting for scripts and kscript serve pages. Install it into VS Code, Cursor, VSCodium or Windsurf:

CommandWhat it does
kscript ide installEvery editor it finds.
kscript ide install cursorJust one.
kscript ideList the editors it found.
kscript ide uninstallRemove the extension again.

The extension works out types as it goes. $person. and $name-> offer the fields and methods of that value, including what your own functions return. Inlay hints show the types you didn't write, and thin lines divide a script into blocks like a notebook.

Press F5 in a script to debug it: it stops at your breakpoints, steps through a line at a time and shows every variable. See Debugging.

It runs kscript lsp from your installed kscript, so keep that current with kscript update. To use a different kscript, set kscript.server.path in the editor's settings. Every command and setting of the extension is listed in VS Code commands and settings.

Other editors

Other editors with LSP support work too. kscript ide config prints the settings to add:

CommandWhat it does
kscript ide config neovimAlso helix, sublime or emacs.

kscript check <folder> reports the same problems on the command line, for CI. See the language server for how it works.