Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* 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
* 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

Expand Down
201 changes: 181 additions & 20 deletions src/FSharp.DependencyManager.Paket/ReferenceLoading.PaketHandler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -41,35 +41,196 @@ 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
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<System.Text.RegularExpressions.Match>
// 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
Some {
Version = version.Groups.[1].Value
// 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
Expand Down
Loading