// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using Progmasoft.Tazel.Dsl.Common; using Progmasoft.Tazel.Dsl.Custom; using Progmasoft.Tazel.Dsl.Custom.Lexer; namespace Progmasoft.Tazel.Dsl.Options; /// How much of something a MicroVM gets: as much as Tazel chooses, or exactly an amount. /// The amount, or null for automatic. public sealed record Allocation(int? Amount) { /// As much as Tazel chooses for this machine. public static Allocation Auto { get; } = new((int?)null); } /// The options of tazel_options.txt; each one not written keeps its default. public sealed record TazelOptions { /// The file of the options of Tazel, in a directory a command runs in or above it. public static string FileName { get; } = "tazel_options.txt"; /// /// Whether what a project brings to run, custom commands and the build among it, runs inside a MicroVM. /// public bool SandboxingWithMicroVM { get; init; } /// Whether the MicroVM reaches the network. public bool MicroVMNetwork { get; init; } /// The memory of the MicroVM, in MiB. public Allocation MicroVMMemory { get; init; } = Allocation.Auto; /// The processors of the MicroVM. public Allocation MicroVMCpus { get; init; } = Allocation.Auto; /// The platform commands build for unless --platform names one; default: the host's. public Platform? Platform { get; init; } /// /// dotnet-settings: [ "Name=Value" ... ];: settings of the .NET runtime of the Tazel process, applied before /// those of --dotnet-settings; none by default. /// public IReadOnlyList DotnetSettings { get; init; } = []; } /// The options that hold in a directory, with the layer each came from and the layers read. /// The options. /// The file of the layer that asked for the sandbox, or null. /// For each option set somewhere, the layer that decided it. /// The layers read, lowest first. public sealed record LayeredOptions( TazelOptions Options, string? SandboxFile, IReadOnlyDictionary Origins, IReadOnlyList Layers); /// What is wrong with a tazel_options.txt, with where it is. public sealed class TazelOptionsException : Exception { /// A problem of the file. public TazelOptionsException(string message) : base(message) { } /// A problem of the file, with all it takes to show it well. public TazelOptionsException(Diagnostic diagnostic) : base((diagnostic ?? throw new ArgumentNullException(nameof(diagnostic))).Plain) { Diagnostic = diagnostic; } /// The place, the line, the help and the joke, when the problem has a place. public Diagnostic? Diagnostic { get; } /// Nothing to say. public TazelOptionsException() { } /// A problem and its cause. public TazelOptionsException(string message, Exception inner) : base(message, inner) { } } /// Reads and finds tazel_options.txt. public static class TazelOptionsFile { private const int MinMemory = 256; private const int MaxMemory = 1_048_576; private const int MaxCpus = 256; private static readonly string[] Keys = ["sandboxing-with-microvm", "microvm-network", "microvm-memory", "microvm-cpus", "platform", "dotnet-settings"]; /// /// Reads the text of a tazel_options.txt, with the comments and strings of tazel_custom.txt. /// public static TazelOptions Parse(string text) => Parse(text, TazelOptions.FileName); /// /// The options of ; names it in messages and a quoted include is /// found beside path. It is read twice: first without what depends on target_system, which /// chooses the platform (requested, else the file's, else this machine's), then for that /// platform. A platform chosen inside a condition on target_system is an error. /// public static TazelOptions Parse(string text, string file) => Parse(text, file, null, null); /// , with includes found beside . public static TazelOptions Parse(string text, string file, string? path) => Parse(text, file, path, null); /// , for the platform when it is not null. public static TazelOptions Parse(string text, string file, string? path, Platform? requested) { var machine = Configuration.ConfigurationContext.ForThisMachine(); var chosen = Read(text, file, path, machine).Options; var platform = requested ?? chosen.Platform ?? Driver.Host.Platform; return Read(text, file, path, machine with { Target = Configuration.SystemFacts.Of(platform) }).Options; } /// The options of one file and the names it sets, read for . internal static (TazelOptions Options, IReadOnlySet Keys) Read( string text, string file, string? path, Configuration.ConfigurationContext context) { IReadOnlyList tokens; try { tokens = Configuration.ConfigurationResolver.Resolve(path ?? file, file, text, Configuration.ConfigurationFamily.Options, context); } catch (CustomCommandsException error) { throw error.Diagnostic is { } diagnostic ? new TazelOptionsException(diagnostic) : new TazelOptionsException(error.Message, error); } var reader = new Reader(tokens, file, text); return (reader.Options(), reader.SetNames); } /// Every tazel_options.txt at or above , the nearest first. public static IReadOnlyList Find(string directory) { var found = new List(); for (var current = new DirectoryInfo(Path.GetFullPath(directory)); current is not null; current = current.Parent) { var file = Path.Combine(current.FullName, TazelOptions.FileName); // A file that cannot be inspected is an error, never a file that is not there, so a sandbox is not skipped. FileAttributes attributes; try { attributes = File.GetAttributes(file); } catch (FileNotFoundException) { continue; } catch (DirectoryNotFoundException) { continue; } catch (IOException problem) { throw new TazelOptionsException($"{file} could not be inspected: {problem.Message}", problem); } catch (UnauthorizedAccessException problem) { throw new TazelOptionsException($"{file} could not be inspected: {problem.Message}", problem); } if ((attributes & FileAttributes.Directory) == 0) { found.Add(file); } } return found; } /// /// The options that hold in , from the layers of /// : each option from the highest layer that sets it, an explicit /// false or auto as much as any value; dotnet-settings name by name. A sandbox asked for in any /// layer stays asked for, so no layer above can turn off what one below requires. The platform is chosen first, from /// the platform asked for or the highest layer that sets it outside conditions on target_system, and /// every layer is then read for it. /// public static (TazelOptions Options, string? SandboxFile) Effective(string directory) => Effective(directory, null); /// , for the platform when it is not null. public static (TazelOptions Options, string? SandboxFile) Effective(string directory, Platform? requested) { var layered = Layered(directory, requested); return (layered.Options, layered.SandboxFile); } /// , with the file each option came from. public static LayeredOptions Layered(string directory) => Layered(directory, null); /// , with the file each option came from. public static LayeredOptions Layered(string directory, Platform? requested) { IReadOnlyList layers; try { layers = Configuration.ConfigurationLayers.For(TazelOptions.FileName, directory); } catch (Configuration.ConfigurationHomeException error) { throw new TazelOptionsException(error.Message, error); } var texts = layers.Select(layer => (Layer: layer, Text: ReadText(layer.File))).ToList(); var machine = Configuration.ConfigurationContext.ForThisMachine(); var platform = requested; if (platform is null) { foreach (var (layer, text) in texts) { var (options, keys) = Read(text, layer.File, layer.File, machine); platform = keys.Contains("platform") ? options.Platform : platform; } } var context = machine with { Target = Configuration.SystemFacts.Of(platform ?? Driver.Host.Platform) }; var merged = new TazelOptions(); var origins = new Dictionary(StringComparer.Ordinal); var settings = new List(); Configuration.Layer? sandbox = null; foreach (var (layer, text) in texts) { var (options, keys) = Read(text, layer.File, layer.File, context); foreach (var key in keys) { origins[key] = layer; merged = key switch { "sandboxing-with-microvm" => merged with { SandboxingWithMicroVM = merged.SandboxingWithMicroVM || options.SandboxingWithMicroVM }, "microvm-network" => merged with { MicroVMNetwork = options.MicroVMNetwork }, "microvm-memory" => merged with { MicroVMMemory = options.MicroVMMemory }, "microvm-cpus" => merged with { MicroVMCpus = options.MicroVMCpus }, "platform" => merged with { Platform = options.Platform }, _ => merged, }; } settings.AddRange(options.DotnetSettings); sandbox = options.SandboxingWithMicroVM ? layer : sandbox; } if (sandbox is not null) { // The layer that asked keeps the say, whatever a layer above wrote. origins["sandboxing-with-microvm"] = sandbox; } return new LayeredOptions(merged with { DotnetSettings = settings }, sandbox?.File, origins, layers); } private static string ReadText(string file) { try { return File.ReadAllText(file); } catch (Exception problem) when (problem is IOException or UnauthorizedAccessException) { throw new TazelOptionsException($"{file} could not be read: {problem.Message}", problem); } } private sealed class Reader(IReadOnlyList tokens, string file, string text) { private int _position; /// The names the file set, once has read it. public HashSet SetNames { get; } = new(StringComparer.Ordinal); public TazelOptions Options() { var options = new TazelOptions(); var seen = SetNames; while (_position < tokens.Count) { var key = Take(TokenKind.Name, "the name of an option"); if (!Keys.Contains(key.Text, StringComparer.Ordinal)) { var near = Cli.Suggestions.Nearest(key.Text, Keys); throw Fail(Output.ErrorCodes.OptionsUnknown, key, $"'{key.Text}' is not an option", "this one", near is null ? $"the options are {string.Join(", ", Keys)}" : $"did you mean {near}?", $"Tazel read its own manual twice; '{key.Text}' is not in it."); } if (!seen.Add(key.Text)) { throw Fail(Output.ErrorCodes.OptionsTwice, key, $"'{key.Text}' is set twice", "the second one", "keep one of them", "Saying it twice does not make it twice as true."); } Take(TokenKind.Colon, $"':' after {key.Text}"); if (key.Text == "dotnet-settings") { options = options with { DotnetSettings = SettingsList() }; if (_position < tokens.Count) { Take(TokenKind.Semicolon, "';' after the list of dotnet-settings"); } continue; } if (_position >= tokens.Count || tokens[_position].Kind is not (TokenKind.Name or TokenKind.String)) { throw new TazelOptionsException(Diagnostic.After(Output.ErrorCodes.OptionsNoValue, text, file, tokens[_position - 1], $"expected a value for {key.Text}", "the value goes here", $"for example {key.Text}: {Example(key.Text)};", "A name, a colon, and then... silence.")); } var value = tokens[_position++]; options = key.Text switch { "sandboxing-with-microvm" => options with { SandboxingWithMicroVM = Boolean(value) }, "microvm-network" => options with { MicroVMNetwork = Boolean(value) }, "microvm-memory" => options with { MicroVMMemory = Amount(value, MinMemory, MaxMemory, "MiB") }, "microvm-cpus" => options with { MicroVMCpus = Amount(value, 1, MaxCpus, "processors") }, _ => options with { Platform = PlatformOf(value) }, }; // The last option may end the file without a ';'. if (_position < tokens.Count) { Take(TokenKind.Semicolon, $"';' after the value of {key.Text}"); } } return options; } // [ "Name=Value" 'Name=Value' `Name=Value` ]: strings in any of the quotes, no commas between them. private IReadOnlyList SettingsList() { var open = Take(TokenKind.BracketOpen, "'[' to open the list of dotnet-settings"); var settings = new List(); while (_position < tokens.Count && tokens[_position].Kind == TokenKind.String) { var entry = tokens[_position++]; try { settings.Add(Runtime.DotnetSettings.Parse(entry.Text, $"{file}:{entry.Line}")); } catch (Runtime.DotnetSettingsException error) { throw Fail(Output.ErrorCodes.OptionsDotnetSetting, entry, error.Message, "this setting", "write \"Name=Value\" with one of the names of tazel help dotnet --long", "Tazel knows thirteen .NET settings by heart; this was not one of them."); } } if (_position >= tokens.Count || tokens[_position].Kind != TokenKind.BracketClose) { var at = _position < tokens.Count ? tokens[_position] : tokens[_position - 1]; throw Fail(Output.ErrorCodes.OptionsDotnetSetting, at, $"the list of dotnet-settings, opened at line {open.Line}, holds strings and ends with ]", "here", "put each setting in quotes, with no commas, and close the list with ]", "A list without its ] is a sentence without its full stop."); } _position++; return settings; } private TazelOptionsException Fail(string code, Token token, string message, string label, string help, string joke) => new(Diagnostic.AtToken(code, text, file, token, message, label, help, joke)); private static string Example(string key) => key switch { "platform" => "linux-amd64", "microvm-memory" => "2048", "microvm-cpus" => "2", _ => "true", }; private Token Take(TokenKind kind, string wanted) { if (_position < tokens.Count && tokens[_position].Kind == kind) { return tokens[_position++]; } var code = kind switch { TokenKind.Semicolon => Output.ErrorCodes.OptionsNoSemicolon, TokenKind.Colon => Output.ErrorCodes.OptionsNoColon, _ => Output.ErrorCodes.OptionsUnexpected, }; var help = kind switch { TokenKind.Semicolon => "end the value with ';': platform: linux-amd64;", TokenKind.Colon => "an option and its value are joined by ':': platform: linux-amd64;", _ => "an option is: name: value;", }; if (_position > 0 && (_position >= tokens.Count || kind is TokenKind.Semicolon or TokenKind.Colon)) { throw new TazelOptionsException(Diagnostic.After(code, text, file, tokens[_position - 1], $"expected {wanted}", kind == TokenKind.Semicolon ? "a ';' goes here" : kind == TokenKind.Colon ? "a ':' goes here" : "something goes here", help, Jokes.Missing(kind))); } throw Fail(code, tokens[_position], $"expected {wanted}", "not this", help, Jokes.Missing(kind)); } private bool Boolean(Token value) => value is { Kind: TokenKind.Name, Text: "true" } ? true : value is { Kind: TokenKind.Name, Text: "false" } ? false : throw Fail(Output.ErrorCodes.OptionsNotBoolean, value, "expected true or false", "this one", "write true or false, bare: sandboxing-with-microvm: true;", "It is a yes-or-no question. Tazel accepts no 'maybe'."); // "auto" is a string, in any of the three quotes; a number stands bare. private Allocation Amount(Token value, int min, int max, string unit) { if (value.Kind == TokenKind.String) { return value.Text == "auto" ? Allocation.Auto : throw Fail(Output.ErrorCodes.OptionsStringNotAuto, value, "the only string here is \"auto\"", "this string", $"write a number of {unit}, bare, or \"auto\"", "Tazel speaks only one word in quotes here, and it is \"auto\"."); } if (value.Text.Length == 0 || value.Text[0] == '0' || value.Text.Length > 9 || !value.Text.All(char.IsAsciiDigit)) { throw Fail(Output.ErrorCodes.OptionsNotANumber, value, $"expected a number of {unit} or \"auto\"", "this one", $"for example {min * 2} or \"auto\"", "Digits only, and no zero up front: this is a number, not a phone number."); } var amount = int.Parse(value.Text, CultureInfo.InvariantCulture); return amount < min || amount > max ? throw Fail(Output.ErrorCodes.OptionsOutOfRange, value, $"{amount} is outside {min} to {max} {unit}", "this number", $"pick a number from {min} to {max}", amount > max ? "Ambitious! A little too ambitious." : "Tazel needs a bit more than that to work with.") : new Allocation(amount); } private Platform PlatformOf(Token value) => value.Kind == TokenKind.Name && Platforms.TryParse(value.Text, out var platform) ? platform : throw Fail(Output.ErrorCodes.OptionsNotAPlatform, value, $"'{value.Text}' is not a platform", "this one", Cli.Suggestions.Nearest(value.Text, Platforms.Names) is { } near ? $"did you mean {near}?" : $"the platforms are {string.Join(", ", Platforms.Names)}", "Tazel looked on every map; there is no such place."); } }