From d23c6f6ae6f382aba2662147de86bef5c5c151aa Mon Sep 17 00:00:00 2001 From: fpellet Date: Mon, 23 Nov 2026 10:53:41 +0100 Subject: [PATCH 1/2] fix: find the local tool when NuGet normalizes the paket version of the manifest in #r "paket:" dotnet tool restore records and restores the tool under its version as the NuGet of the SDK normalizes it: 12.0-alpha003 is restored in paket/12.0.0-alpha003, and 12.0.0+sha in paket/12.0.0. NuGet also removes the white space of String.Trim around the version and around each of its numbers: "12. 0.0" is 12.0.0, and a no-break space before the "-" of a prerelease is dropped. The local tool was looked for under the version as the manifest writes it, so it was taken as not restored. --- RELEASE_NOTES.md | 1 + .../ReferenceLoading.PaketHandler.fs | 46 ++++++++++++++++++- tests/Paket.Tests/FsiExtension.fs | 32 +++++++++++++ 3 files changed, 78 insertions(+), 1 deletion(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 13e3c76753..6b27c08dfa 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -11,6 +11,7 @@ * fix: When neither `HOME` nor `USERPROFILE` is set, `#r "paket:"` in F# scripts no longer fails with an `ArgumentNullException` while looking for a global tool, and with an empty `HOME` it no longer looks for one in the current folder * fix: When the tool manifest declares paket but its local tool isn't restored, `#r "paket:"` in F# scripts warns and runs a global tool, or else the most recent Paket .NET tool restored in the NuGet packages folder, instead of failing, and when it finds neither, it fails asking to run `dotnet tool restore`; it still skips a Paket installed with `--tool-path`. The Paket that runs instead may be another version: when it fails, the error ends by repeating that the local tool isn't restored, and when it succeeds, the next run runs Paket again, with the local tool once it is restored, which installs the versions written in `paket.lock` by the Paket that ran instead, as `paket install` does * fix: When the tool manifest says `"rollForward": true` for paket, as `dotnet tool install --allow-roll-forward` writes it, `#r "paket:"` in F# scripts runs the local tool with `dotnet --roll-forward Major`, as `dotnet tool run` does: whatever `DOTNET_ROLL_FORWARD` says, it takes the first `dotnet` that has the .NET version of the tool or a later one, and runs on a later major version when that `dotnet` lacks the one of the tool. It ran the tool as its `runtimeconfig.json` and `DOTNET_ROLL_FORWARD` said: with `DOTNET_ROLL_FORWARD` set to `Minor`, `LatestMinor` or `LatestPatch`, it failed where `dotnet paket` worked when no `dotnet` had the .NET version of the tool, such as .NET 10 for Paket 12, with `Disable` when none had the exact version it asks for, such as .NET 10.0.0, and with `LatestMajor` it ran on the latest major version of the `dotnet` +* fix: `#r "paket:"` in F# scripts finds the local tool when the tool manifest gives its version in a form that NuGet normalizes, such as `12.0-alpha003` or `12.0.0+sha`: it looked for the tool under that form, while `dotnet tool restore` records and restores it under the normalized version, `12.0.0-alpha003` or `12.0.0`, so it took the local tool as not restored #### 12.0.0 - 2026-11-11 diff --git a/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs b/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs index 647af7ae79..485a196724 100644 --- a/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs +++ b/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs @@ -41,8 +41,49 @@ module Logic = //|> Seq.concat //|> Seq.map fst + /// The version of a tool in a tool manifest as NuGet normalizes it when dotnet reads it: three numbers, or + /// four when the last isn't 0, without their leading zeros, without the white space around the version and + /// around each number, then the prerelease labels with their case, without the build metadata. None when + /// NuGet refuses it. + /// `dotnet tool restore` records the tool under that version in the toolResolverCache, and restores it + /// in the folder of that version in lower case. + let normalizedNuGetVersion (version: string) = + let isPart allowLeadingZeros (part: string) = + part <> "" + && part |> Seq.forall (fun c -> (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '-') + // a number with leading zeros isn't a prerelease label + && (allowLeadingZeros || part.Length = 1 || part.[0] <> '0' || not (part |> Seq.forall (fun c -> c >= '0' && c <= '9'))) + let version = version.Trim() + let release, metadata = + match version.IndexOf '+' with + | -1 -> version, None + | i -> version.Substring(0, i), Some(version.Substring(i + 1)) + let numbers, labels = + match release.IndexOf '-' with + | -1 -> release, None + | i -> release.Substring(0, i), Some(release.Substring(i + 1)) + let numbers = + numbers.Split('.') + |> Array.map (fun number -> + // NuGet parses each number as an Int32 after String.Trim, and refuses the NUL characters that + // Int32.TryParse ignores at the end: the digits are checked first + let number = number.Trim() + if number = "" || not (number |> Seq.forall (fun c -> c >= '0' && c <= '9')) then None else + match System.Int32.TryParse(number, System.Globalization.NumberStyles.None, System.Globalization.CultureInfo.InvariantCulture) with + | true, number -> Some number + | false, _ -> None) + let arePartsOf allowLeadingZeros (parts: string option) = + parts |> Option.forall (fun parts -> parts.Split('.') |> Array.forall (isPart allowLeadingZeros)) + if numbers.Length > 4 || Array.contains None numbers || not (arePartsOf false labels) || not (arePartsOf true metadata) then None else + let numbers = Array.choose id numbers + let number i = if i < numbers.Length then numbers.[i] else 0 + let revision = if number 3 > 0 then $".%d{number 3}" else "" + let labels = labels |> Option.map (fun labels -> "-" + labels) |> Option.defaultValue "" + Some $"%d{number 0}.%d{number 1}.%d{number 2}%s{revision}%s{labels}" + /// The paket tool that a tool manifest declares type ManifestPaket = { + /// As NuGet normalizes it, or as written when NuGet refuses it Version: string /// "rollForward": true, as `dotnet tool install --allow-roll-forward` writes it, for which /// `dotnet tool run` runs the tool with --roll-forward Major @@ -62,8 +103,11 @@ module Logic = let tool = paketTool.Groups.[1].Value let version = System.Text.RegularExpressions.Regex.Match(tool, "\"version\"\\s*:\\s*\"([^\"]+)\"", options) if not version.Success then None else + let version = version.Groups.[1].Value Some { - Version = version.Groups.[1].Value + // as NuGet normalizes it, the version of the toolResolverCache and of the folder of the tool; a version + // that NuGet refuses is kept as written: dotnet fails on it, and no folder is restored for it + Version = normalizedNuGetVersion version |> Option.defaultValue version // with its case, as dotnet reads it, so that a "RollForward", which dotnet ignores, doesn't add // --roll-forward Major; the version and isRoot are matched without case, as in the zsh completion RollForward = System.Text.RegularExpressions.Regex.IsMatch(tool, "\"rollForward\"\\s*:\\s*true") diff --git a/tests/Paket.Tests/FsiExtension.fs b/tests/Paket.Tests/FsiExtension.fs index ea7137bd45..238ad5ba08 100644 --- a/tests/Paket.Tests/FsiExtension.fs +++ b/tests/Paket.Tests/FsiExtension.fs @@ -71,6 +71,38 @@ module FsiExtension = let manifest = """{ "version": 1, "isRoot": true, "tools": { "paket": { "version": "12.0.0", "commands": [ "paket" ] } } }""" Assert.AreEqual((Some (manifestPaket "12.0.0" false), true), paketOf manifest) + [] + let ``a tool manifest gives the version of paket as NuGet normalizes it`` () = + // the version that `dotnet tool restore` records in the toolResolverCache and restores the tool under + let manifest = """{ "version": 1, "isRoot": true, "tools": { "paket": { "version": "12.0-alpha003", "commands": [ "paket" ] } } }""" + Assert.AreEqual((Some (manifestPaket "12.0.0-alpha003" false), true), paketOf manifest) + + let normalizedNuGetVersion = ReferenceLoading.Internals.Logic.normalizedNuGetVersion + + [] + let ``a version is normalized as NuGet does`` () = + Assert.AreEqual(Some "12.0.0-alpha003", normalizedNuGetVersion "12.0-alpha003") + Assert.AreEqual(Some "12.0.0", normalizedNuGetVersion "12.0.0.0") + Assert.AreEqual(Some "12.0.1", normalizedNuGetVersion "012.0.1") + Assert.AreEqual(Some "12.0.0", normalizedNuGetVersion "12.0.0+sha") + Assert.AreEqual(Some "12.0.0", normalizedNuGetVersion "12") + Assert.AreEqual(Some "1.2.3.4", normalizedNuGetVersion "1.2.3.4") + // the labels keep their case, a label can start with 0 when it isn't a number, and the metadata can have + // leading zeros + Assert.AreEqual(Some "12.0.0-Beta.0a.0", normalizedNuGetVersion " 12.0.0-Beta.0a.0+sha.01 ") + Assert.AreEqual(Some "12.0.0-rc-1", normalizedNuGetVersion "12.0.0-rc-1+build-2") + // the white space around the version and its numbers, as String.Trim removes it + Assert.AreEqual(Some "1.0.0", normalizedNuGetVersion "1. 0") + Assert.AreEqual(Some "12.0.0-beta", normalizedNuGetVersion "12 .0\t.0 -beta") + Assert.AreEqual(Some "12.0.0", normalizedNuGetVersion "\u00a012.0.0\u3000") + Assert.AreEqual(Some "12.0.0-alpha003", normalizedNuGetVersion "12.0\u00a0-alpha003") + Assert.AreEqual(Some "12.0.0", normalizedNuGetVersion "12\u2028.\u00850.0") + + [] + let ``a version that NuGet refuses has no normalized form`` () = + for version in [ ""; "abc"; "1.2.3.4.5"; "1..0"; "12.0.0-"; "12.0.0-a..b"; "12.0.0-01"; "12.0.0-a_b"; "12.0.0+"; "12.0.0+a_b"; "2147483648.0.0"; "1.-0"; "1 0"; "12.0.0- beta"; "12\u0000.0.0"; "12.0.0\u0000"; "12\u200b.0.0"; "12\ufeff.0.0"; "+1.0" ] do + Assert.AreEqual(None, normalizedNuGetVersion version, version) + [] let ``a tool manifest gives the rollForward of paket`` () = // as `dotnet tool install --allow-roll-forward` writes it From 0d96837d67f1009623f0a8ac18962a0f349abb6e Mon Sep 17 00:00:00 2001 From: fpellet Date: Mon, 23 Nov 2026 11:09:16 +0100 Subject: [PATCH 2/2] fix: read the tool manifest as JSON in #r "paket:" The regular expressions of #r "paket:" looked for a "paket" object and for "isRoot" anywhere in the manifest, and for the version of paket before the first "}", without case: an "IsRoot", a property in a comment, or the "paket" object of another tool when it had a version counted, and an object in the paket tool hid what follows it. As the extension targets netstandard2.0, without System.Text.Json, a small JSON reader now reads the manifest by its structure, as dotnet does: only the tools of "tools" count, the first with the ID paket, in any case, and a version, and "version", "rollForward" and "isRoot" are read with their case, the last of each when it is written twice. It is lenient: it skips comments and trailing commas, which dotnet refuses. A manifest that isn't JSON, which dotnet refuses too, now declares no paket, where the regular expressions could still find one in it. The reader goes 64 levels deep at most, so that a manifest can't overflow the stack. --- RELEASE_NOTES.md | 1 + .../ReferenceLoading.PaketHandler.fs | 163 +++++++++++++++--- tests/Paket.Tests/FsiExtension.fs | 74 ++++++++ 3 files changed, 215 insertions(+), 23 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 6b27c08dfa..29542cdbac 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -12,6 +12,7 @@ * fix: When the tool manifest declares paket but its local tool isn't restored, `#r "paket:"` in F# scripts warns and runs a global tool, or else the most recent Paket .NET tool restored in the NuGet packages folder, instead of failing, and when it finds neither, it fails asking to run `dotnet tool restore`; it still skips a Paket installed with `--tool-path`. The Paket that runs instead may be another version: when it fails, the error ends by repeating that the local tool isn't restored, and when it succeeds, the next run runs Paket again, with the local tool once it is restored, which installs the versions written in `paket.lock` by the Paket that ran instead, as `paket install` does * fix: When the tool manifest says `"rollForward": true` for paket, as `dotnet tool install --allow-roll-forward` writes it, `#r "paket:"` in F# scripts runs the local tool with `dotnet --roll-forward Major`, as `dotnet tool run` does: whatever `DOTNET_ROLL_FORWARD` says, it takes the first `dotnet` that has the .NET version of the tool or a later one, and runs on a later major version when that `dotnet` lacks the one of the tool. It ran the tool as its `runtimeconfig.json` and `DOTNET_ROLL_FORWARD` said: with `DOTNET_ROLL_FORWARD` set to `Minor`, `LatestMinor` or `LatestPatch`, it failed where `dotnet paket` worked when no `dotnet` had the .NET version of the tool, such as .NET 10 for Paket 12, with `Disable` when none had the exact version it asks for, such as .NET 10.0.0, and with `LatestMajor` it ran on the latest major version of the `dotnet` * fix: `#r "paket:"` in F# scripts finds the local tool when the tool manifest gives its version in a form that NuGet normalizes, such as `12.0-alpha003` or `12.0.0+sha`: it looked for the tool under that form, while `dotnet tool restore` records and restores it under the normalized version, `12.0.0-alpha003` or `12.0.0`, so it took the local tool as not restored +* fix: `#r "paket:"` in F# scripts reads the tool manifest as JSON: only the tools of `"tools"` count, `"isRoot"` and `"version"` are case-sensitive, and the last `"isRoot"`, `"version"` or `"rollForward"` counts when one is written twice, as dotnet reads them. It skips comments and trailing commas, which dotnet refuses, and a manifest that isn't JSON declares no paket. It looked for a `"paket"` object and for `"isRoot"` anywhere in the manifest, and for the `"version"` of paket before the first `}`, without case, so that an `"IsRoot"`, a property in a comment, or the `"paket"` object of another tool when it had a `"version"` counted, and an object in the paket tool hid the properties after it #### 12.0.0 - 2026-11-11 diff --git a/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs b/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs index 485a196724..ea99855e4e 100644 --- a/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs +++ b/src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs @@ -90,30 +90,147 @@ module Logic = RollForward: bool } - /// What the content of a tool manifest says of paket: the tool it declares, if any, and - /// whether it is "isRoot", which dotnet doesn't look above + /// A JSON value, with the properties of an object in their order, those written twice included + type JsonValue = + | JsonObject of (string * JsonValue) list + | JsonArray of JsonValue list + | JsonString of string + | JsonNumber of string + | JsonBool of bool + | JsonNull + + exception private InvalidJson + + /// The JSON value of a text, or None when it isn't JSON. It is lenient, as for a tool manifest that people + /// edit: it skips comments and trailing commas, which dotnet refuses, and doesn't check the form of the + /// numbers or the control characters in strings. It reads objects and arrays 64 deep at most, as + /// System.Text.Json does, so that a text can't overflow the stack. + let parseJson (text: string) = + // where the reading is, in a cell as closures can't capture a mutable variable + let i = ref 0 + let fail () = raise InvalidJson + let isAt (c: char) = i.Value < text.Length && text.[i.Value] = c + let isAtText (s: string) = System.String.CompareOrdinal(text, i.Value, s, 0, s.Length) = 0 + let advance (count: int) = i.Value <- i.Value + count + let skipSpaceAndComments () = + let mutable more = true + while more do + if isAt ' ' || isAt '\t' || isAt '\n' || isAt '\r' then advance 1 + elif isAtText "//" then + while i.Value < text.Length && not (isAt '\n') do + advance 1 + elif isAtText "/*" then + match text.IndexOf("*/", i.Value + 2, System.StringComparison.Ordinal) with + | -1 -> fail () + | close -> i.Value <- close + 2 + else more <- false + let expect (c: char) = if isAt c then advance 1 else fail () + let parseString () = + expect '"' + let value = System.Text.StringBuilder() + while not (isAt '"') do + if i.Value >= text.Length then fail () + if text.[i.Value] <> '\\' then + value.Append text.[i.Value] |> ignore + advance 1 + else + let escaped = if i.Value + 1 < text.Length then text.[i.Value + 1] else fail () + if escaped = 'u' then + let hex = if i.Value + 6 <= text.Length then text.Substring(i.Value + 2, 4) else fail () + if not (hex |> Seq.forall System.Uri.IsHexDigit) then fail () + value.Append(char (System.Convert.ToInt32(hex, 16))) |> ignore + advance 6 + else + let unescaped = + match escaped with + | '"' | '\\' | '/' -> escaped + | 'b' -> '\b' + | 'f' -> '\f' + | 'n' -> '\n' + | 'r' -> '\r' + | 't' -> '\t' + | _ -> fail () + value.Append unescaped |> ignore + advance 2 + advance 1 + value.ToString() + // the items of an object or an array, separated by commas, a trailing one included, up to its end + let parseItems (closing: char) (parseItem: unit -> 'item) = + advance 1 + let items = ResizeArray<'item>() + skipSpaceAndComments () + while not (isAt closing) do + items.Add(parseItem ()) + skipSpaceAndComments () + if isAt ',' then + advance 1 + skipSpaceAndComments () + elif not (isAt closing) then fail () + advance 1 + List.ofSeq items + let rec parseValue (depth: int) = + skipSpaceAndComments () + if i.Value >= text.Length then fail () + match text.[i.Value] with + | '{' | '[' when depth > 64 -> fail () + | '{' -> + parseItems '}' (fun () -> + let name = parseString () + skipSpaceAndComments () + expect ':' + name, parseValue (depth + 1)) + |> JsonObject + | '[' -> parseItems ']' (fun () -> parseValue (depth + 1)) |> JsonArray + | '"' -> JsonString(parseString ()) + | _ when isAtText "true" -> advance 4; JsonBool true + | _ when isAtText "false" -> advance 5; JsonBool false + | _ when isAtText "null" -> advance 4; JsonNull + | _ -> + let start = i.Value + while i.Value < text.Length && "+-.0123456789eE".IndexOf(text.[i.Value]) >= 0 do + advance 1 + if i.Value = start then fail () + JsonNumber(text.Substring(start, i.Value - start)) + try + let value = parseValue 1 + skipSpaceAndComments () + if i.Value < text.Length then None else Some value + with InvalidJson -> None + + /// The value of a property of a JSON object: the last one when it is written twice, as System.Text.Json + /// gives it + let private jsonProperty (name: string) (properties: (string * JsonValue) list) = + properties |> List.tryFindBack (fst >> (=) name) |> Option.map snd + + /// What the content of a tool manifest says of paket: the tool it declares, if any, and whether it is + /// "isRoot", which dotnet doesn't look above. It reads the manifest as JSON, by its structure, as dotnet + /// does: only the tools of "tools" count, the first with the ID "paket", in any case, and a version; + /// "version", "rollForward" and "isRoot" are read with their case, the last of each when it is written + /// twice. Unlike dotnet, it doesn't check the rest: it reads a manifest that dotnet refuses, and one that + /// isn't a JSON object declares no paket. let paketOfToolManifest (content: string) = - let options = System.Text.RegularExpressions.RegexOptions.IgnoreCase - let paket = - System.Text.RegularExpressions.Regex.Matches(content, "\"paket\"\\s*:\\s*\\{([^}]*)\\}", options) - |> Seq.cast - // the first "paket" object that has a version, as a "paket" property of another tool, which dotnet - // ignores, has none - |> Seq.tryPick (fun paketTool -> - let tool = paketTool.Groups.[1].Value - let version = System.Text.RegularExpressions.Regex.Match(tool, "\"version\"\\s*:\\s*\"([^\"]+)\"", options) - if not version.Success then None else - let version = version.Groups.[1].Value - Some { - // as NuGet normalizes it, the version of the toolResolverCache and of the folder of the tool; a version - // that NuGet refuses is kept as written: dotnet fails on it, and no folder is restored for it - Version = normalizedNuGetVersion version |> Option.defaultValue version - // with its case, as dotnet reads it, so that a "RollForward", which dotnet ignores, doesn't add - // --roll-forward Major; the version and isRoot are matched without case, as in the zsh completion - RollForward = System.Text.RegularExpressions.Regex.IsMatch(tool, "\"rollForward\"\\s*:\\s*true") - }) - let isRoot = System.Text.RegularExpressions.Regex.IsMatch(content, "\"isRoot\"\\s*:\\s*true", options) - paket, isRoot + match parseJson content with + | Some (JsonObject manifest) -> + let tools = match jsonProperty "tools" manifest with Some (JsonObject tools) -> tools | _ -> [] + let paket = + tools + |> List.tryPick (fun (id, tool) -> + match tool with + // as dotnet compares the IDs in lower case + | JsonObject properties when id.ToLowerInvariant() = "paket" -> + match jsonProperty "version" properties with + | Some (JsonString version) -> + Some { + // as NuGet normalizes it, the version of the toolResolverCache and of the folder of the + // tool; a version that NuGet refuses is kept as written: dotnet fails on it, and no + // folder is restored for it + Version = normalizedNuGetVersion version |> Option.defaultValue version + RollForward = (jsonProperty "rollForward" properties = Some (JsonBool true)) + } + | _ -> None + | _ -> None) + paket, (jsonProperty "isRoot" manifest = Some (JsonBool true)) + | _ -> None, false /// Where to look for dotnet, in order: the host that runs the script (DOTNET_HOST_PATH, the /// running process when it is dotnet, the root of the running runtime), then DOTNET_ROOT, the diff --git a/tests/Paket.Tests/FsiExtension.fs b/tests/Paket.Tests/FsiExtension.fs index 238ad5ba08..5519499451 100644 --- a/tests/Paket.Tests/FsiExtension.fs +++ b/tests/Paket.Tests/FsiExtension.fs @@ -149,6 +149,80 @@ module FsiExtension = let manifest = """{ "version": 1, "isRoot": false, "tools": {} }""" Assert.AreEqual((None, false), paketOf manifest) + [] + let ``only the tools of "tools" count`` () = + // a "paket" object with a version in another tool, or out of "tools" + let manifest = """{ "version": 1, "isRoot": false, "paket": { "version": "8.0.0" }, "tools": { "fantomas": { "version": "7.0.0", "commands": [ "fantomas" ], "paket": { "version": "9.0.0" } }, "paket": { "version": "12.0.0", "commands": [ "paket" ] } } }""" + Assert.AreEqual((Some (manifestPaket "12.0.0" false), false), paketOf manifest) + // an object in the paket tool doesn't hide the properties after it + let manifest = """{ "version": 1, "isRoot": false, "tools": { "paket": { "version": "12.0.0", "commands": [ "paket" ], "settings": { "a": [ {} ] }, "rollForward": true } } }""" + Assert.AreEqual((Some (manifestPaket "12.0.0" true), false), paketOf manifest) + + [] + let ``the property names are case-sensitive, the tool ID isn't`` () = + Assert.AreEqual((None, false), paketOf """{ "version": 1, "IsRoot": true, "tools": { "paket": { "Version": "12.0.0", "commands": [ "paket" ] } } }""") + Assert.AreEqual((None, false), paketOf """{ "version": 1, "isRoot": false, "Tools": { "paket": { "version": "12.0.0", "commands": [ "paket" ] } } }""") + Assert.AreEqual( + (Some (manifestPaket "12.0.0" false), false), + paketOf """{ "version": 1, "isRoot": false, "tools": { "Paket": { "version": "12.0.0", "commands": [ "paket" ] } } }""") + // of two IDs that differ only by case, the first counts; the first, "pa\u006bet", is paket written with an escape + Assert.AreEqual( + (Some (manifestPaket "12.0.0" false), false), + paketOf """{ "version": 1, "isRoot": false, "tools": { "pa\u006bet": { "version": "12.0.0", "commands": [ "paket" ] }, "Paket": { "version": "12.1.0", "commands": [ "paket" ] } } }""") + + [] + let ``the last of a property written twice counts`` () = + let manifest = """{ "isRoot": true, "version": 1, "isRoot": false, "tools": { "paket": { "version": "12.0.0", "rollForward": true, "commands": [ "paket" ], "version": "12.1.0", "rollForward": false } } }""" + Assert.AreEqual((Some (manifestPaket "12.1.0" false), false), paketOf manifest) + + [] + let ``comments and trailing commas are skipped`` () = + let manifest = """{ + // the tools of the repository + "version": 1, + "isRoot": true, /* "isRoot": false, */ + "tools": { + "paket": { + "version": "12.0.0", + "commands": [ "paket", ], + // "rollForward": true, + }, + }, +}""" + Assert.AreEqual((Some (manifestPaket "12.0.0" false), true), paketOf manifest) + + [] + let ``a tool manifest that isn't a JSON object declares no paket`` () = + // not JSON, as a manifest cut in a string or after a backslash, JSON that isn't an object, or an object + // followed by more text, such as an extra } or an unclosed comment + for manifest in [ ""; "{"; """{ "tools": { "paket": { "version": "12.0"""; """{ "tools": { "paket": { "version": "12.0\"""; "[]"; "\"paket\""; """{ "tools": { "paket": { "version": "12.0.0" } } } }"""; """{ "isRoot": true } /* """ ] do + Assert.AreEqual((None, false), paketOf manifest, manifest) + + let parseJson = ReferenceLoading.Internals.Logic.parseJson + + [] + let ``JSON is read with its values and escapes`` () = + let expected = + ReferenceLoading.Internals.Logic.JsonObject + [ "a", ReferenceLoading.Internals.Logic.JsonArray [ ReferenceLoading.Internals.Logic.JsonNumber "-0.5e+10"; ReferenceLoading.Internals.Logic.JsonBool true; ReferenceLoading.Internals.Logic.JsonBool false; ReferenceLoading.Internals.Logic.JsonNull ] + "a", ReferenceLoading.Internals.Logic.JsonString "\u00e9\"\\/\b\f\n\r\t" ] + Assert.AreEqual(Some expected, parseJson " \r\n\t{ \"a\" : [ -0.5e+10, true, false, null ], \"a\": \"\\u00e9\\\"\\\\\\/\\b\\f\\n\\r\\t\" } ") + // an escape that isn't JSON + Assert.AreEqual(None, parseJson "\"\\x\"") + Assert.AreEqual(None, parseJson "\"\\u12\"") + Assert.AreEqual(None, parseJson "\"\\u12zz\"") + + [] + let ``JSON is read 64 objects and arrays deep at most`` () = + let nested depth = String.replicate depth "[" + String.replicate depth "]" + Assert.IsTrue(Option.isSome (parseJson (nested 64))) + Assert.AreEqual(None, parseJson (nested 65)) + Assert.AreEqual(None, parseJson (nested 100000)) + let nestedObjects depth = String.replicate depth "{\"a\":" + "1" + String.replicate depth "}" + Assert.IsTrue(Option.isSome (parseJson (nestedObjects 64))) + Assert.AreEqual(None, parseJson (nestedObjects 65)) + Assert.AreEqual(None, parseJson (nestedObjects 100000)) + let root = System.IO.Path.GetFullPath(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "dotnet-lookup")) let inRoot (parts: string list) = System.IO.Path.Combine(Array.ofList (root :: parts))